-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday20.rs
187 lines (168 loc) · 6.67 KB
/
day20.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
use std::collections::{HashMap, VecDeque};
use std::fs;
use crate::utils::toolbox::lcm_64;
pub(crate) fn day20() {
println!("{}", part_a(fs::read_to_string("input/2023/day20/input.txt").unwrap()));
println!("{}", part_b(fs::read_to_string("input/2023/day20/input.txt").unwrap()));
}
fn part_a(input: String) -> usize {
let mut modules: HashMap<&str, Type> = HashMap::new();
let mut destinations: HashMap<&str, Vec<&str>> = HashMap::new();
let mut flip_flop_states: HashMap<&str, bool> = HashMap::new();
let mut conjunction_states: HashMap<&str, HashMap<&str, bool>> = HashMap::new();
input.lines().for_each(|line| {
let (name, destinations_str) = line.split_once(" -> ").unwrap();
let tp = match name.chars().nth(0).unwrap() {
'%' => Type::FlipFlop,
'&' => Type::Conjunction,
'b' => Type::Broadcaster,
c => panic!("unknown type {}", c)
};
let name_fr = &name[1..name.len()];
modules.insert(name_fr, tp);
let dests: Vec<&str> = destinations_str.split(", ").collect();
destinations.insert(name_fr, dests);
});
// Pre-populate conjunction states
destinations.iter().for_each(|(from, dests)| {
dests.iter().for_each(|dest| {
if let Some(tp) = modules.get(dest) {
if *tp == Type::Conjunction {
conjunction_states.entry(dest).or_insert(HashMap::new()).insert(from, false);
}
}
})
});
let mut ans = (0, 0); // High, low
for _ in 0..1000 {
let mut stack = VecDeque::new();
stack.push_back((false, "roadcaster", "button"));
while let Some((high, key, from)) = stack.pop_front() {
if high { ans = (ans.0 + 1, ans.1) } else { ans = (ans.0, ans.1 + 1) }
if !modules.contains_key(key) { continue }
let send_high: bool;
match *modules.get(key).unwrap() {
Type::FlipFlop => {
if high { continue; }
let state = *flip_flop_states.entry(key).or_insert(false);
flip_flop_states.insert(key, !state);
send_high = !state;
}
Type::Conjunction => {
let state = conjunction_states.entry(key).or_insert(HashMap::new());
state.insert(from, high);
if state.values().all(|v| *v) { send_high = false } else { send_high = true }
}
Type::Broadcaster => { send_high = high }
}
for dest in destinations.get(key).unwrap() {
stack.push_back((send_high, dest, key));
}
}
}
return ans.0 * ans.1;
}
fn part_b(input: String) -> i64 {
let mut modules: HashMap<&str, Type> = HashMap::new();
let mut destinations: HashMap<&str, Vec<&str>> = HashMap::new();
let mut flip_flop_states: HashMap<&str, bool> = HashMap::new();
let mut conjunction_states: HashMap<&str, HashMap<&str, bool>> = HashMap::new();
input.lines().for_each(|line| {
let (name, destinations_str) = line.split_once(" -> ").unwrap();
let tp = match name.chars().nth(0).unwrap() {
'%' => Type::FlipFlop,
'&' => Type::Conjunction,
'b' => Type::Broadcaster,
c => panic!("unknown type {}", c)
};
let name_fr = &name[1..name.len()];
modules.insert(name_fr, tp);
let dests: Vec<&str> = destinations_str.split(", ").collect();
destinations.insert(name_fr, dests);
});
// Pre-populate conjunction states
destinations.iter().for_each(|(from, dests)| {
dests.iter().for_each(|dest| {
if let Some(tp) = modules.get(dest) {
if *tp == Type::Conjunction {
conjunction_states.entry(dest).or_insert(HashMap::new()).insert(from, false);
}
}
})
});
let mut ans = 0;
let mut lcms = HashMap::new();
loop {
let mut stack = VecDeque::new();
ans += 1;
stack.push_back((false, "roadcaster", "button"));
while let Some((high, key, from)) = stack.pop_front() {
// &vt -> &tj -> rx
// &sk -> &tj -> rx
// &xc -> &tj -> rx
// &kk -> &tj -> rx
if key == "vt" && !high {
lcms.insert("vt", ans);
}
if key == "sk" && !high {
lcms.insert("sk", ans);
}
if key == "xc" && !high {
lcms.insert("xc", ans);
}
if key == "kk" && !high {
lcms.insert("kk", ans);
}
if lcms.contains_key("vt") && lcms.contains_key("sk") && lcms.contains_key("xc") && lcms.contains_key("kk") {
let vt = *lcms.get("vt").unwrap();
let sk = *lcms.get("sk").unwrap();
let xc = *lcms.get("xc").unwrap();
let kk = *lcms.get("kk").unwrap();
return lcm_64(lcm_64(lcm_64(vt, sk), xc), kk);
}
if key == "rx" && !high {
return ans;
}
if !modules.contains_key(key) { continue }
let send_high: bool;
match *modules.get(key).unwrap() {
Type::FlipFlop => {
if high { continue; }
let state = *flip_flop_states.entry(key).or_insert(false);
flip_flop_states.insert(key, !state);
send_high = !state;
}
Type::Conjunction => {
let state = conjunction_states.entry(key).or_insert(HashMap::new());
state.insert(from, high);
if state.values().all(|v| *v) { send_high = false } else { send_high = true }
}
Type::Broadcaster => { send_high = high }
}
for dest in destinations.get(key).unwrap() {
stack.push_back((send_high, dest, key));
}
}
}
}
#[derive(Copy, Clone, Eq, PartialEq)]
enum Type {
FlipFlop,
Conjunction,
Broadcaster,
}
#[cfg(test)]
mod day20_tests {
use std::fs;
use crate::y2023::day20::{part_a, part_b};
#[test]
fn test_works() {
assert_eq!(32000000, part_a(fs::read_to_string("input/2023/day20/test.txt").unwrap()));
assert_eq!(11687500, part_a(fs::read_to_string("input/2023/day20/test_2.txt").unwrap()));
}
#[test]
fn input_works() {
assert_eq!(818649769, part_a(fs::read_to_string("input/2023/day20/input.txt").unwrap()));
assert_eq!(246313604784977, part_b(fs::read_to_string("input/2023/day20/input.txt").unwrap()));
}
}