This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20.rs
190 lines (154 loc) · 5.47 KB
/
20.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use std::collections::{HashMap, VecDeque};
use num::integer::lcm;
advent_of_code::solution!(20);
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
enum Strength {
Low,
High
}
#[derive(Debug)]
struct Pulse<'a> {
from: &'a str,
to: &'a str,
strength: Strength
}
#[derive(Debug)]
enum Module<'a> {
Broadcaster {
outputs: Vec<&'a str>
},
FlipFlop {
state: bool,
outputs: Vec<&'a str>
},
Conjunction {
memory: HashMap<&'a str, Strength>,
outputs: Vec<&'a str>
}
}
impl<'a> Module<'a> {
fn outputs(&self) -> &Vec<&'a str> {
match self {
Module::Broadcaster { outputs } => outputs,
Module::FlipFlop { outputs, .. } => outputs,
Module::Conjunction { outputs, .. } => outputs,
}
}
}
impl<'a> Pulse<'a> {
fn process(self, modules: &mut HashMap<&'a str, Module<'a>>, queue: &mut VecDeque<Pulse<'a>>) {
if !modules.contains_key(&self.to) {
return;
}
let dest = modules.get_mut(&self.to).unwrap();
let pulse = match dest {
Module::Broadcaster { .. } => Some(self.strength),
Module::FlipFlop { state, .. } => {
match self.strength {
Strength::High => None,
Strength::Low => {
*state = !*state;
if *state { Strength::High } else { Strength::Low }.into()
}
}
},
Module::Conjunction { memory, .. } => {
*memory.get_mut(self.from).unwrap() = self.strength;
let all_high = memory.values().all(|s| *s == Strength::High);
if all_high { Strength::Low } else { Strength::High }.into()
}
};
if let Some(new_pulse) = pulse {
for d in dest.outputs() {
queue.push_back(Pulse { from: self.to, to: d, strength: new_pulse })
}
}
}
}
fn parse(input: &str) -> HashMap<&str, Module> {
input.lines()
.fold(HashMap::new(), |mut hm, l| {
let (name_str, output_str) = l.split_once(" -> ").unwrap();
let outputs: Vec<&str> = output_str.split(", ").collect();
let name = if &name_str[0..1] == "b" { &name_str[..] } else { &name_str[1..] };
let module = match &name_str[0..1] {
"b" => Module::Broadcaster { outputs },
"%" => Module::FlipFlop { state: false, outputs },
"&" => Module::Conjunction { memory: HashMap::new(), outputs },
_ => panic!("Invalid gate")
};
hm.insert(name, module);
hm
})
}
fn initialize_memories<'a>(modules: &mut HashMap<&'a str, Module<'a>>) {
let destinations: HashMap<&str, Vec<&str>> = modules.iter()
.fold(HashMap::new(), |hm, (name, module)| {
module.outputs().iter().fold(hm, |mut hm2, dest| {
if !hm2.contains_key(dest) {
hm2.insert(&dest, vec![]);
}
hm2.get_mut(dest).unwrap().push(&name);
hm2
})
});
for (name, module) in modules.iter_mut() {
if let Module::Conjunction { memory, .. } = module {
for d in destinations[name].iter() {
memory.insert(&d, Strength::Low);
}
}
};
}
pub fn part_one(input: &str) -> Option<u32> {
let mut modules = parse(input);
initialize_memories(&mut modules);
let mut low = 0;
let mut high = 0;
for _ in 0..1000 {
let mut queue: VecDeque<Pulse> = VecDeque::new();
queue.push_back(Pulse { from: "button", to: "broadcaster", strength: Strength::Low });
while let Some(pulse) = queue.pop_front() {
match pulse.strength {
Strength::Low => { low += 1; },
Strength::High => { high += 1; },
}
pulse.process(&mut modules, &mut queue);
}
}
Some(low * high)
}
pub fn part_two(input: &str) -> Option<u64> {
let mut modules = parse(input);
initialize_memories(&mut modules);
let (prior, Module::Conjunction { memory, .. }) = modules.iter()
.find(|(_, module)| module.outputs().contains(&"rx"))
.unwrap()
else { panic!("Should be a conjunction") };
let prior_name = &prior[..];
let mut feeder: HashMap<&str, Option<u64>> = memory.keys().map(|&m| (m, None)).collect();
for p in 1.. {
let mut queue: VecDeque<Pulse> = VecDeque::new();
queue.push_back(Pulse { from: "button", to: "broadcaster", strength: Strength::Low });
while let Some(pulse) = queue.pop_front() {
if pulse.to == prior_name && pulse.strength == Strength::High {
feeder.insert(pulse.from, Some(p));
if feeder.values().all(|v| v.is_some()) {
return feeder.values().fold(1, |acc, v| lcm(v.unwrap(), acc)).into();
}
}
pulse.process(&mut modules, &mut queue);
}
}
panic!("No result");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(32000000));
}
// No tests for part 2.
}