Skip to content

Commit ff81535

Browse files
committed
submit day 20 part 2
1 parent b0c05ba commit ff81535

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

src/bin/20.rs

+37-16
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,46 @@ pub fn part_one(input: &str) -> Option<u64> {
66
let mut program = Program::from_input(input);
77
let mut pulse_count = PulseCount::new();
88

9+
let mut add_to_pulse_count = |pulse_packet: &PulsePacket| pulse_count.add(pulse_packet.pulse);
10+
911
for _ in 0..1000 {
10-
program.run(&mut pulse_count);
12+
program.run(&mut add_to_pulse_count);
1113
}
1214

1315
Some(pulse_count.high * pulse_count.low)
1416
}
1517

16-
pub fn part_two(input: &str) -> Option<u32> {
17-
None
18+
pub fn part_two(input: &str) -> Option<u64> {
19+
let mut program = Program::from_input(input);
20+
21+
// Due to the puzzle input shape we know the rx module has a single conjunction input
22+
let receiver_module = program.module_inputs.get("rx").unwrap()[0];
23+
24+
let mut runtimes = HashMap::new();
25+
26+
for index in 0..2_u64.pow(12) {
27+
program.run(&mut |pulse_packet| {
28+
if pulse_packet.to == receiver_module && pulse_packet.pulse == Pulse::High {
29+
runtimes
30+
.entry(pulse_packet.from.to_owned())
31+
.or_insert(index + 1);
32+
}
33+
})
34+
}
35+
36+
Some(runtimes.values().product::<u64>())
1837
}
1938

2039
struct Program<'a> {
2140
modules: HashMap<&'a str, Module<'a>>,
41+
module_inputs: HashMap<&'a str, Vec<&'a str>>,
2242
}
2343

2444
impl Program<'_> {
2545
fn from_input(input: &str) -> Program {
2646
let mut module_outputs = Vec::new();
2747
let mut modules = HashMap::new();
48+
let mut module_inputs: HashMap<&str, Vec<&str>> = HashMap::new();
2849

2950
for line in input.lines() {
3051
let module = Module::from_line(line);
@@ -38,17 +59,25 @@ impl Program<'_> {
3859
if let Some(module) = modules.get_mut(output_module) {
3960
module.add_input(module_name);
4061
}
62+
63+
module_inputs
64+
.entry(output_module)
65+
.or_default()
66+
.push(module_name);
4167
}
4268
}
4369

44-
Program { modules }
70+
Program {
71+
modules,
72+
module_inputs,
73+
}
4574
}
4675

47-
fn run(&mut self, pulse_count: &mut PulseCount) {
76+
fn run(&mut self, on_pulse_packet: &mut impl FnMut(&PulsePacket)) {
4877
let mut pulse_packets = VecDeque::from([PulsePacket::start()]);
4978

5079
while let Some(pulse_packet) = pulse_packets.pop_front() {
51-
pulse_count.add(pulse_packet.pulse);
80+
on_pulse_packet(&pulse_packet);
5281

5382
if let Some(module) = self.modules.get_mut(pulse_packet.to) {
5483
if let Some(pulse) = module.process(pulse_packet) {
@@ -165,7 +194,7 @@ impl<'a> Logic<'a> {
165194
}
166195
}
167196

168-
#[derive(PartialEq, Copy, Clone)]
197+
#[derive(Debug, PartialEq, Copy, Clone)]
169198
enum Pulse {
170199
High,
171200
Low,
@@ -198,7 +227,7 @@ struct PulsePacket<'a> {
198227
impl<'a> PulsePacket<'a> {
199228
fn start() -> PulsePacket<'a> {
200229
PulsePacket {
201-
from: "",
230+
from: "button",
202231
to: "broadcaster",
203232
pulse: Pulse::Low,
204233
}
@@ -224,12 +253,4 @@ mod tests {
224253
));
225254
assert_eq!(result, Some(11687500));
226255
}
227-
228-
#[test]
229-
fn test_part_two() {
230-
let result = part_two(&advent_of_code::template::read_file_part(
231-
"examples", DAY, 3,
232-
));
233-
assert_eq!(result, None);
234-
}
235256
}

0 commit comments

Comments
 (0)