Skip to content

Commit

Permalink
2024/05/p2
Browse files Browse the repository at this point in the history
  • Loading branch information
jstuczyn committed Dec 11, 2024
1 parent 19d7d67 commit 9c67fd7
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions 2024/day05/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl PrintingRules {
};

for next in after {
if next == &page {
continue;
}

if !rules.contains(next) {
return false;
}
Expand All @@ -55,8 +59,30 @@ impl PrintingRules {
}

pub fn fix_update(&self, update: &PrintingUpdate) -> PrintingUpdate {
todo!()
//
let mut fixed = Vec::with_capacity(update.pages_to_produce.len());

let mut pages_to_insert = update.pages_to_produce.clone();
// first page is the one that comes before **all** remaining ones (there can only be one)
// second page includes all but the first, etc...
// is this the most efficient? lol, no, insertion sort is one of the worst ones,
// but given the size of the input, it's a perfectly valid solution to this problem
while !pages_to_insert.is_empty() {
if pages_to_insert.len() == 1 {
fixed.push(pages_to_insert[0]);
break;
}
for (i, page) in pages_to_insert.iter().enumerate() {
if self.can_be_printed_before(*page, &pages_to_insert) {
fixed.push(*page);
pages_to_insert.remove(i);
break;
}
}
}

PrintingUpdate {
pages_to_produce: fixed,
}
}

pub fn updates(&self) -> &[PrintingUpdate] {
Expand Down

0 comments on commit 9c67fd7

Please sign in to comment.