-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_24.rs
262 lines (224 loc) Β· 6.18 KB
/
day_24.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use std::{
cell::RefCell,
collections::{hash_map::Keys, HashMap, HashSet},
fs,
};
use common::{solution, Answer};
use itertools::Itertools;
solution!("Crossed Wires", 24);
fn part_a(input: &str) -> Answer {
let problem = Problem::parse(input);
let keys = problem.inner_keys();
let keys = keys.filter(|x| x.starts_with('z')).sorted().rev();
let mut out = 0;
for key in keys {
let val = problem.get(key);
out <<= 1;
out |= val as u64;
}
out.into()
}
fn part_b(input: &str) -> Answer {
let problem = Problem::parse(input);
let all_keys = problem.keys();
let keys = problem.inner_keys();
// input
let mut bits;
let x = {
let all_keys = all_keys.clone();
let x = all_keys
.iter()
.filter(|x| x.starts_with('x'))
.sorted()
.rev();
let mut out = 0;
bits = x.len();
for key in x {
let val = problem.get(key);
out <<= 1;
out |= val as u64;
}
out
};
let y = {
let x = all_keys
.iter()
.filter(|x| x.starts_with('y'))
.sorted()
.rev();
let mut out = 0;
bits = bits.max(x.len());
for key in x {
let val = problem.get(key);
out <<= 1;
out |= val as u64;
}
out
};
let correct = x + y;
let keys = keys.clone().filter(|x| x.starts_with('z')).sorted().rev();
let mut to_swap = HashSet::new();
let mut bit = dbg!(bits);
let mut out = 0;
for key in keys.clone() {
let val = problem.get(key);
let correct_bit = (correct >> bit) & 1 != 0;
if val != correct_bit {
// println!("wrong output: {key}");
to_swap.insert(key);
}
out <<= 1;
out |= val as u64;
bit = bit.saturating_sub(1);
}
if out == correct {
println!("CORRECT!");
}
let mut vis = String::new();
vis.push_str("digraph G {\n");
for (key, (a, op, b)) in problem.connections.iter() {
vis.push_str(&format!("{a} -> {key}\n"));
vis.push_str(&format!("{b} -> {key}\n"));
vis.push_str(&format!("{key} [label=\"{key} {op:?}\"]\n"));
if key.starts_with("z") {
vis.push_str(&format!("{key} -> out{key}\n"));
vis.push_str(&format!(
"out{key} [label=\"{key}\",style=filled,fillcolor=\"{}\"]\n",
if key.starts_with('z') && *op != Operation::Xor {
"green"
} else {
if to_swap.contains(key) {
"red"
} else {
"blue"
}
}
));
}
}
vis.push_str("}");
fs::write("out.txt", vis).unwrap();
todo!()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Operation {
And,
Or,
Xor,
}
#[derive(Clone)]
struct Problem<'a> {
// output ->
connections: HashMap<&'a str, (&'a str, Operation, &'a str)>,
values: RefCell<HashMap<&'a str, bool>>,
}
impl<'a> Problem<'a> {
fn parse(input: &'a str) -> Self {
let mut connections = HashMap::new();
let mut values = HashMap::new();
let (raw_values, raw_connections) = input.split_once("\n\n").unwrap();
for value in raw_values.lines() {
let (key, val) = value.split_once(": ").unwrap();
let num = val == "1";
values.insert(key, num);
}
for connection in raw_connections.lines() {
let parts = connection.split_ascii_whitespace().collect::<Vec<_>>();
let (a, op, b, out) = (parts[0], parts[1], parts[2], parts[4]);
let op = match op {
"AND" => Operation::And,
"OR" => Operation::Or,
"XOR" => Operation::Xor,
_ => panic!(),
};
if connections.insert(out, (a, op, b)).is_some() {
dbg!(a, op, b, out);
panic!("uhh");
}
}
Self {
connections,
values: RefCell::new(values),
}
}
fn keys(&self) -> HashSet<&str> {
let mut out = HashSet::new();
out.extend(self.connections.keys());
out.extend(self.values.borrow().keys());
out
}
fn inner_keys(&self) -> Keys<'_, &str, (&str, Operation, &str)> {
self.connections.keys()
}
fn get(&self, key: &'a str) -> bool {
if let Some(&val) = self.values.borrow().get(key) {
return val;
}
let (a, op, b) = &self.connections[key];
let a = self.get(a);
let b = self.get(b);
let out = match op {
Operation::And => a && b,
Operation::Or => a || b,
Operation::Xor => a ^ b,
};
self.values.borrow_mut().insert(key, out);
out
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
x00: 1
x01: 0
x02: 1
x03: 1
x04: 0
y00: 1
y01: 1
y02: 1
y03: 1
y04: 1
ntg XOR fgs -> mjb
y02 OR x01 -> tnw
kwq OR kpj -> z05
x00 OR x03 -> fst
tgd XOR rvg -> z01
vdt OR tnw -> bfw
bfw AND frj -> z10
ffh OR nrd -> bqk
y00 AND y03 -> djm
y03 OR y00 -> psh
bqk OR frj -> z08
tnw OR fst -> frj
gnj AND tgd -> z11
bfw XOR mjb -> z00
x03 OR x00 -> vdt
gnj AND wpb -> z02
x04 AND y00 -> kjc
djm OR pbm -> qhw
nrd AND vdt -> hwm
kjc AND fst -> rvg
y04 OR y02 -> fgs
y01 AND x02 -> pbm
ntg OR kjc -> kwq
psh XOR fgs -> tgd
qhw XOR tgd -> z09
pbm OR djm -> kpj
x03 XOR y03 -> ffh
x00 XOR y04 -> ntg
bfw OR bqk -> z06
nrd XOR fgs -> wpb
frj XOR qhw -> z04
bqk OR frj -> z07
y03 OR x01 -> nrd
hwm AND bqk -> z03
tgd XOR rvg -> z12
tnw OR pbm -> gnj
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 2024.into());
}
}