Update to also do part 2

This commit is contained in:
Leon Grünewald 2024-12-12 22:12:49 +01:00
parent 91ac0a0688
commit 61d540ed86

View file

@ -57,27 +57,48 @@ fn sum_middle_page_nums(updates: Vec<Vec<&str>>) -> i64 {
.sum() .sum()
} }
fn correct_update(update: &mut Vec<&str>, before_index: usize) {
let elem = update.remove(before_index);
update.insert(0, elem);
}
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let data = get_data()?; let data = get_data()?;
let (rules, updates) = parse_rules_and_updates(&data); let (rules, updates) = parse_rules_and_updates(&data);
let mut correct_updates = Vec::new(); let mut correct_updates = Vec::new();
let mut corrected_updates = Vec::new();
'update: for update in updates { 'update: for mut update in updates {
for rule in &rules { let mut fixed = false;
if let Some(_) = check_rule_compliance(&update, rule) { let mut keep_fixing = true;
/*println!(
"Rule issue: {:?} ({before_index:?}) {:?} ({after_index:?})", while keep_fixing {
update[before_index], keep_fixing = false;
update[after_index] for rule in &rules {
); if let Some((before_index, after_index)) = check_rule_compliance(&update, rule) {
println!("Elements: {update:?}");*/ fixed = true;
continue 'update; keep_fixing = true;
println!("Elements: {update:?}");
println!(
"Rule issue: {:?} ({before_index:?}) {:?} ({after_index:?})",
update[before_index],
update[after_index]
);
correct_update(&mut update, before_index);
println!("Elements: {update:?}");
}
} }
} }
correct_updates.push(update);
if fixed {
corrected_updates.push(update);
} else {
correct_updates.push(update);
}
} }
println!("{}", sum_middle_page_nums(correct_updates)); println!("{}", sum_middle_page_nums(correct_updates));
println!("{}", sum_middle_page_nums(corrected_updates));
Ok(()) Ok(())
} }