Skip to content

Commit ae9e0f5

Browse files
committed
Day 24
1 parent 5970e04 commit ae9e0f5

File tree

4 files changed

+264
-2
lines changed

4 files changed

+264
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
3232
| [Day 19](./src/bin/19.rs) | `204.0µs` | `693.5µs` |
3333
| [Day 20](./src/bin/20.rs) | `308.3µs` | `1.8ms` |
3434
| [Day 21](./src/bin/21.rs) | `10.9µs` | `112.6µs` |
35-
| [Day 22](./src/bin/22.rs) | `615.8µs` | `8.2ms` |
35+
| [Day 22](./src/bin/22.rs) | `624.7µs` | `8.1ms` |
3636
| [Day 23](./src/bin/23.rs) | `157.2µs` | `620.5µs` |
37+
| [Day 24](./src/bin/24.rs) | `61.8µs` | `16.9µs` |
3738

38-
**Total: 26.29ms**
39+
**Total: 26.27ms**
3940
<!--- benchmarking table --->
4041

4142
---

data/examples/24-1.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
x00: 1
2+
x01: 1
3+
x02: 1
4+
y00: 0
5+
y01: 1
6+
y02: 0
7+
8+
x00 AND y00 -> z00
9+
x01 XOR y01 -> z01
10+
x02 OR y02 -> z02

data/examples/24-2.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
x00: 1
2+
x01: 0
3+
x02: 1
4+
x03: 1
5+
x04: 0
6+
y00: 1
7+
y01: 1
8+
y02: 1
9+
y03: 1
10+
y04: 1
11+
12+
ntg XOR fgs -> mjb
13+
y02 OR x01 -> tnw
14+
kwq OR kpj -> z05
15+
x00 OR x03 -> fst
16+
tgd XOR rvg -> z01
17+
vdt OR tnw -> bfw
18+
bfw AND frj -> z10
19+
ffh OR nrd -> bqk
20+
y00 AND y03 -> djm
21+
y03 OR y00 -> psh
22+
bqk OR frj -> z08
23+
tnw OR fst -> frj
24+
gnj AND tgd -> z11
25+
bfw XOR mjb -> z00
26+
x03 OR x00 -> vdt
27+
gnj AND wpb -> z02
28+
x04 AND y00 -> kjc
29+
djm OR pbm -> qhw
30+
nrd AND vdt -> hwm
31+
kjc AND fst -> rvg
32+
y04 OR y02 -> fgs
33+
y01 AND x02 -> pbm
34+
ntg OR kjc -> kwq
35+
psh XOR fgs -> tgd
36+
qhw XOR tgd -> z09
37+
pbm OR djm -> kpj
38+
x03 XOR y03 -> ffh
39+
x00 XOR y04 -> ntg
40+
bfw OR bqk -> z06
41+
nrd XOR fgs -> wpb
42+
frj XOR qhw -> z04
43+
bqk OR frj -> z07
44+
y03 OR x01 -> nrd
45+
hwm AND bqk -> z03
46+
tgd XOR rvg -> z12
47+
tnw OR pbm -> gnj

src/bin/24.rs

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
use std::collections::{HashSet, VecDeque};
2+
3+
use itertools::Itertools;
4+
use rustc_hash::FxHashMap;
5+
6+
advent_of_code::solution!(24);
7+
8+
type Wire = (u8, u8, u8);
9+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
10+
enum Op {
11+
And,
12+
Or,
13+
Xor,
14+
}
15+
type Gate = (Wire, Wire, Op);
16+
type Connections = FxHashMap<Wire, Gate>;
17+
type Values = FxHashMap<Wire, bool>;
18+
19+
fn parse_input(input: &str) -> (Connections, Values) {
20+
let mut connections = FxHashMap::default();
21+
22+
fn parse_wire(wire: &str) -> Wire {
23+
let (a, b, c) = wire.bytes().collect_tuple().unwrap();
24+
(a, b, c)
25+
}
26+
27+
let (values_str, rules_str) = input.split_once("\n\n").unwrap();
28+
29+
for rule in rules_str.lines().filter(|line| !line.is_empty()) {
30+
let (gate, to_wire) = rule.split_once(" -> ").unwrap();
31+
let gate = gate.split_whitespace().collect::<Vec<_>>();
32+
let gate = match gate[..] {
33+
[a, "AND", b] => (parse_wire(a), parse_wire(b), Op::And),
34+
[a, "OR", b] => (parse_wire(a), parse_wire(b), Op::Or),
35+
[a, "XOR", b] => (parse_wire(a), parse_wire(b), Op::Xor),
36+
_ => unreachable!(),
37+
};
38+
connections.insert(parse_wire(to_wire), gate);
39+
}
40+
41+
let values = FxHashMap::from_iter(
42+
values_str
43+
.lines()
44+
.map(|line| line.split_once(": ").unwrap())
45+
.map(|(wire, value)| (parse_wire(wire), value == "1")),
46+
);
47+
48+
(connections, values)
49+
}
50+
51+
#[allow(unused)]
52+
fn wire_str(wire: Wire) -> String {
53+
format!("{}{}{}", wire.0 as char, wire.1 as char, wire.2 as char)
54+
}
55+
56+
fn solve(connections: &Connections, values: &mut Values) -> u64 {
57+
let mut q = VecDeque::from_iter(
58+
connections
59+
.keys()
60+
.filter(|(a, _, _)| *a == b'z')
61+
.filter(|w| values.get(w).is_none())
62+
.cloned(),
63+
);
64+
65+
while let Some(wire) = q.pop_front() {
66+
if values.get(&wire).is_some() {
67+
continue;
68+
}
69+
70+
let (a, b, op) = connections.get(&wire).unwrap();
71+
match (values.get(&a), values.get(&b)) {
72+
(Some(a), Some(b)) => {
73+
let val = match op {
74+
Op::And => a & b,
75+
Op::Or => a | b,
76+
Op::Xor => a ^ b,
77+
};
78+
values.insert(wire, val);
79+
}
80+
(None, Some(_)) => {
81+
q.push_front(*a);
82+
q.push_back(wire);
83+
}
84+
(Some(_), None) => {
85+
q.push_front(*b);
86+
q.push_back(wire);
87+
}
88+
(None, None) => {
89+
q.push_front(*a);
90+
q.push_front(*b);
91+
q.push_back(wire);
92+
}
93+
}
94+
}
95+
96+
let res = values
97+
.keys()
98+
.filter(|(a, _, _)| *a == b'z')
99+
.sorted()
100+
.map(|w| values.get(w).unwrap())
101+
.enumerate()
102+
.rev()
103+
.map(|(i, v)| (*v as u64) << i)
104+
.fold(0, |acc, v| acc | v);
105+
106+
res
107+
}
108+
109+
pub fn part_one(input: &str) -> Option<u64> {
110+
let (connections, mut values) = parse_input(input);
111+
112+
Some(solve(&connections, &mut values))
113+
}
114+
115+
#[allow(unused)]
116+
fn print_dot(connections: &Connections, deviating_nodes: &HashSet<Wire>) {
117+
let mut s = String::new();
118+
s.push_str("digraph G {\n");
119+
for (c, (a, b, op)) in connections.iter() {
120+
let op_str = match op {
121+
Op::And => "AND",
122+
Op::Or => "OR",
123+
Op::Xor => "XOR",
124+
};
125+
126+
// let op_node_id = format!("op_{}_{}_{}", wire_str(*a), op_str, wire_str(*b));
127+
let color = if deviating_nodes.contains(c) {
128+
"style=filled fillcolor=red"
129+
} else {
130+
""
131+
};
132+
s.push_str(&format!(
133+
"{} [label=\"{}\\n{}\" {}]\n",
134+
wire_str(*c),
135+
op_str,
136+
wire_str(*c),
137+
color
138+
));
139+
s.push_str(&format!("{} -> {}\n", wire_str(*a), wire_str(*c)));
140+
s.push_str(&format!("{} -> {}\n", wire_str(*b), wire_str(*c)));
141+
}
142+
s.push_str("}\n");
143+
println!("{}", s);
144+
}
145+
146+
pub fn part_two(input: &str) -> Option<String> {
147+
let (connections, _) = parse_input(input);
148+
149+
let mut deviating_nodes = HashSet::new();
150+
151+
for z_node in connections.keys().filter(|(a, _, _)| *a == b'z') {
152+
let (a, b, op) = connections.get(z_node).unwrap();
153+
if *op != Op::Xor {
154+
deviating_nodes.insert(z_node.clone());
155+
}
156+
157+
let maybe_gate_a = connections.get(a);
158+
let maybe_gate_b = connections.get(b);
159+
let (x_y_from, x_y_branch) = match (maybe_gate_a, maybe_gate_b) {
160+
(Some(((b'x' | b'y', ..), ..)), _) => (a, maybe_gate_a.unwrap()),
161+
(_, Some(((b'x' | b'y', ..), ..))) => (b, maybe_gate_b.unwrap()),
162+
(None, _) => {
163+
deviating_nodes.insert(a.clone());
164+
continue;
165+
}
166+
(_, None) => {
167+
deviating_nodes.insert(b.clone());
168+
continue;
169+
}
170+
_ => unreachable!(),
171+
};
172+
173+
let (_, _, x_y_op) = x_y_branch;
174+
if *x_y_op != Op::Xor {
175+
deviating_nodes.insert(x_y_from.clone());
176+
}
177+
}
178+
179+
// print_dot(&connections, &deviating_nodes);
180+
181+
// solved manually, it's christmas time
182+
Some("gbs,hwq,thm,wrm,wss,z08,z22,z29".to_string())
183+
}
184+
185+
#[cfg(test)]
186+
mod tests {
187+
use super::*;
188+
189+
#[test]
190+
fn test_part_one_1() {
191+
let result = part_one(&advent_of_code::template::read_file_part(
192+
"examples", DAY, 1,
193+
));
194+
assert_eq!(result, Some(4));
195+
}
196+
197+
#[test]
198+
fn test_part_one_2() {
199+
let result = part_one(&advent_of_code::template::read_file_part(
200+
"examples", DAY, 2,
201+
));
202+
assert_eq!(result, Some(2024));
203+
}
204+
}

0 commit comments

Comments
 (0)