From da61b8d5413558d2896f4b66b4454f087b7c15db Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 21 Dec 2023 19:03:17 +0100 Subject: [PATCH] Brute force part 2 --- 2023/src/day20.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/2023/src/day20.rs b/2023/src/day20.rs index 2cfec46..80f4e8c 100644 --- a/2023/src/day20.rs +++ b/2023/src/day20.rs @@ -141,8 +141,52 @@ pub fn part1(input: &[u8]) -> anyhow::Result { Ok((low_pulses * high_pulses).to_string()) } -pub fn part2(_input: &[u8]) -> anyhow::Result { - anyhow::bail!("Not implemented") +pub fn part2(input: &[u8]) -> anyhow::Result { + let mut cables = parse_input(input, parse_cables)?; + + let mut todo = VecDeque::new(); + let mut last_pulse: HashMap = HashMap::new(); + + for press in 1u64.. { + todo.push_back((false, convert_name(b"broadcaster"))); + + while let Some((pulse, pos)) = todo.pop_front() { + let Some(cable) = cables.get_mut(&pos) else { + // Sometimes cables aren't real, and that's okay + continue; + }; + + if !pulse && pos == convert_name(b"rx") { + return Ok(press.to_string()); + } + + let next_pulse = match &mut cable.node { + Node::FlipFlop(state) => { + if pulse { + // Ignore, nothing to be done since it's a high pulse + continue; + } else { + *state = !*state; + *state + } + } + Node::Conjunction(inwards) => { + // Need to deal with the check outside the match otherwise lifetime issues :( + !inwards + .iter() + .all(|source| *last_pulse.get(source).unwrap_or(&false)) + } + Node::Broadcaster => pulse, + }; + + last_pulse.insert(pos, next_pulse); + for &other in &cable.dest { + todo.push_back((next_pulse, other)); + } + } + } + + anyhow::bail!("Somehow counted to infinity") } #[cfg(test)]