diff --git a/2022/rust/src/solutions/day11.rs b/2022/rust/src/solutions/day11.rs index 40b37aa..4e416c0 100644 --- a/2022/rust/src/solutions/day11.rs +++ b/2022/rust/src/solutions/day11.rs @@ -35,9 +35,10 @@ fn transfer_items(source: &mut VecDeque, destination: &mut VecDeque) { } } -fn find_inspects(data: &Vec, rounds: i32) -> BinaryHeap { +fn find_inspects(data: &Vec, rounds: i32, divide: bool) -> BinaryHeap { let mut monkeys: Vec> = data.iter().map(|m| m.items.iter().copied().collect()).collect(); let mut inspections: HashMap = HashMap::new(); + let all_tests = data.iter().fold(1, |acc, next| acc * next.test); let mut buffer_yeah: VecDeque = VecDeque::new(); let mut buffer_nope: VecDeque = VecDeque::new(); @@ -50,7 +51,8 @@ fn find_inspects(data: &Vec, rounds: i32) -> BinaryHeap { Operation::OldPlus(num) => old + num, Operation::OldTimes(num) => old * num, }; - let worry = worry / 3; + let worry = if divide { worry / 3 } else { worry }; + let worry = worry % all_tests; match worry % data[i].test == 0 { true => buffer_yeah.push_back(worry), false => buffer_nope.push_back(worry), @@ -65,14 +67,12 @@ fn find_inspects(data: &Vec, rounds: i32) -> BinaryHeap { } fn solve1(data: &Vec) -> i64 { - let mut heap = find_inspects(&data, 20); - heap.pop().unwrap() * heap.pop().unwrap() + find_inspects(&data, 20, true).iter().take(2).product() } // brb, speedrunning modular arithmetic course to solve this puzzle fn solve2(data: &Vec) -> i64 { - let mut heap = find_inspects(&data, 10000); - heap.pop().unwrap() * heap.pop().unwrap() + find_inspects(&data, 10000, false).iter().take(2).product() }