From 73201811944118f7394bb5dda688687efd0ec911 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Wed, 20 Dec 2023 17:36:21 +0100 Subject: [PATCH] Properly solve day 20 part 2 --- 2023/src/bin/day20.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/2023/src/bin/day20.rs b/2023/src/bin/day20.rs index 5f4fcfd..a41f05e 100644 --- a/2023/src/bin/day20.rs +++ b/2023/src/bin/day20.rs @@ -89,6 +89,24 @@ struct Pulse<'a> { high: bool, } +// Copied from day 8 +fn gcd(a: usize, b: usize) -> usize { + if b == 0 { + return a; + } + + gcd(b, a % b) +} + +// Copied from day 8 +fn lcm(a: usize, b: usize) -> usize { + if a > b { + (a / gcd(a, b)) * b + } else { + (b / gcd(a, b)) * a + } +} + // -- Solution -- pub struct Day; impl aoc::Solver for Day { @@ -215,6 +233,7 @@ impl aoc::Solver for Day { let final_name = r#final.0.to_owned(); let final_module = r#final.1.clone(); + let mut frequencies = HashMap::new(); println!("{final_name}: {final_module:?}"); @@ -229,9 +248,12 @@ impl aoc::Solver for Day { while let Some(pulse) = pulses.pop_front() { if let Some(module) = modules.get_mut(pulse.destination) { if pulse.destination == final_name && pulse.high { - // TODO: Instead of printing this, keep track of it - // Once all inputs have received a pulse calculate the LCM and output that - println!("{} high after {} presses", pulse.source, i + 1); + frequencies.insert(pulse.source, i + 1); + if let ModuleType::Conjunction(ref inputs) = final_module.module_type { + if frequencies.len() == inputs.len() { + return frequencies.values().copied().fold(1, lcm); + } + } } if let Some(high) = module.process(pulse.source, pulse.high) { @@ -247,6 +269,6 @@ impl aoc::Solver for Day { } } - 0 + unreachable!("No solution found"); } }