-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_20.rs
219 lines (182 loc) Β· 5.6 KB
/
day_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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use std::collections::{HashMap, VecDeque};
use common::{solution, Answer};
solution!("Pulse Propagation", 20);
fn part_a(input: &str) -> Answer {
let connections = parse_input(input);
let mut low_pulse = 999;
let mut high_pulse = 0;
simulate(&connections, |i, _source, _target, pulse| {
match pulse {
Pulse::Low => low_pulse += 1,
Pulse::High => high_pulse += 1,
}
i < 1000
});
(low_pulse * high_pulse).into()
}
fn part_b(input: &str) -> Answer {
let connections = parse_input(input);
let important_connections = important_connections(&connections);
let mut last_cycle = [0; 4];
let mut cycle_length = [0; 4];
simulate(&connections, |i, source, target, pulse| {
if target == "rg" && pulse == Pulse::High {
if let Some(idx) = important_connections.iter().position(|s| s == &source) {
let last = last_cycle[idx];
last_cycle[idx] = i;
cycle_length[idx] = (i - last + 1) as u64;
if cycle_length.iter().all(|&i| i != 0) {
return false;
}
}
}
true
});
cycle_length.iter().product::<u64>().into()
}
fn simulate(
connections: &HashMap<&str, Connection<'_>>,
mut hook: impl FnMut(u32, &str, &str, Pulse) -> bool,
) {
let mut flop_memory = HashMap::new();
let mut conjunction_memory = HashMap::new();
for i in connections.values() {
match i.connection_type {
ConnectionType::FlipFlop => {
flop_memory.insert(i.source, false);
}
ConnectionType::Conjunction => {
let mut map = HashMap::new();
// Add map of all items mapping to i
for j in connections.values() {
if j.target.contains(&i.source) {
map.insert(j.source, Pulse::Low);
}
}
conjunction_memory.insert(i.source, map);
}
_ => continue,
}
}
for i in 0.. {
let mut next = VecDeque::new();
const BASE: &str = "broadcaster";
for i in connections[BASE].target.iter() {
next.push_back((BASE, i, Pulse::Low));
}
while let Some((source, target, pulse)) = next.pop_front() {
if !hook(i, source, target, pulse) {
return;
}
let Some(connection) = connections.get(target) else {
continue;
};
let pulse = match connection.connection_type {
ConnectionType::Normal => continue,
ConnectionType::FlipFlop => {
if pulse == Pulse::High {
continue;
}
let mem = flop_memory.get_mut(target).unwrap();
*mem ^= true;
if *mem {
Pulse::High
} else {
Pulse::Low
}
}
ConnectionType::Conjunction => {
let mem = conjunction_memory.get_mut(target).unwrap();
let this = mem.get_mut(source).unwrap();
*this = pulse;
if mem.values().all(|&l| l == Pulse::High) {
Pulse::Low
} else {
Pulse::High
}
}
};
for i in connection.target.iter() {
next.push_back((target, i, pulse));
}
}
}
}
fn important_connections<'a>(connections: &'a HashMap<&'a str, Connection<'a>>) -> Vec<&'a str> {
// Find node that connects to rx
let drain = connections
.values()
.find(|i| i.target.contains(&"rx"))
.unwrap();
assert_eq!(drain.connection_type, ConnectionType::Conjunction);
// Find all nodes that connect to drain
connections
.values()
.filter(|i| i.target.contains(&drain.source))
.map(|i| i.source)
.collect()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Pulse {
Low,
High,
}
#[derive(Debug, PartialEq, Eq)]
enum ConnectionType {
Normal,
FlipFlop,
Conjunction,
}
#[derive(Debug)]
struct Connection<'a> {
connection_type: ConnectionType,
source: &'a str,
target: Vec<&'a str>,
}
fn parse_input(input: &str) -> HashMap<&str, Connection<'_>> {
let mut out = HashMap::new();
for line in input.lines() {
let (source, target) = line.split_once(" -> ").unwrap();
let connection_type = match source.chars().next() {
Some('%') => ConnectionType::FlipFlop,
Some('&') => ConnectionType::Conjunction,
_ => ConnectionType::Normal,
};
let source = source.trim_start_matches(['%', '&']);
out.insert(
source,
Connection {
connection_type,
source,
target: target.split(", ").collect(),
},
);
}
out
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
broadcaster -> a, b, c
%a -> b
%b -> c
%c -> inv
&inv -> a
"};
const CASE_2: &str = indoc! {"
broadcaster -> a
%a -> inv, con
&inv -> b
%b -> con
&con -> output
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 32000000.into());
}
#[test]
fn part_a_2() {
assert_eq!(super::part_a(CASE_2), 11687500.into());
}
}