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 {
let mut fixed = false;
let mut keep_fixing = true;
while keep_fixing {
keep_fixing = false;
for rule in &rules { for rule in &rules {
if let Some(_) = check_rule_compliance(&update, rule) { if let Some((before_index, after_index)) = check_rule_compliance(&update, rule) {
/*println!( fixed = true;
keep_fixing = true;
println!("Elements: {update:?}");
println!(
"Rule issue: {:?} ({before_index:?}) {:?} ({after_index:?})", "Rule issue: {:?} ({before_index:?}) {:?} ({after_index:?})",
update[before_index], update[before_index],
update[after_index] update[after_index]
); );
println!("Elements: {update:?}");*/ correct_update(&mut update, before_index);
continue 'update; println!("Elements: {update:?}");
} }
} }
}
if fixed {
corrected_updates.push(update);
} else {
correct_updates.push(update); 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(())
} }