Skip to content

Commit 98b2efe

Browse files
2024: Improve day 24
1 parent cdee940 commit 98b2efe

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

2024/day24/src/main.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,29 +93,27 @@ fn run<'a>(
9393

9494
let mut queue = VecDeque::from_iter(gates);
9595

96-
while !queue.is_empty() {
97-
while let Some(g) = queue.pop_front() {
98-
let out = renames.get(&g.out).unwrap_or(&g.out);
99-
100-
// get values of a and b or push them back into the queue if
101-
// their values aren't available yet
102-
let Some(&a) = wires.get(g.a) else {
103-
queue.push_back(g);
104-
continue;
105-
};
106-
let Some(&b) = wires.get(g.b) else {
107-
queue.push_back(g);
108-
continue;
109-
};
96+
while let Some(g) = queue.pop_front() {
97+
let out = renames.get(&g.out).unwrap_or(&g.out);
98+
99+
// get values of a and b or push them back into the queue if
100+
// their values aren't available yet
101+
let Some(&a) = wires.get(g.a) else {
102+
queue.push_back(g);
103+
continue;
104+
};
105+
let Some(&b) = wires.get(g.b) else {
106+
queue.push_back(g);
107+
continue;
108+
};
110109

111-
let v = match g.logic {
112-
Logic::And => a && b,
113-
Logic::Or => a || b,
114-
Logic::Xor => a != b,
115-
};
110+
let v = match g.logic {
111+
Logic::And => a && b,
112+
Logic::Or => a || b,
113+
Logic::Xor => a != b,
114+
};
116115

117-
wires.insert(out, v);
118-
}
116+
wires.insert(out, v);
119117
}
120118

121119
get_value(&wires, "z")
@@ -211,7 +209,7 @@ fn main() {
211209
broken_nodes.sort();
212210
println!("{}", broken_nodes.join(","));
213211

214-
// hard-coded answer for my puzzle input
212+
// hard-coded answer for my puzzle input (just to check if the code above works)
215213
let renames = HashMap::from([
216214
// 1
217215
("kqh", "ddn"),
@@ -233,7 +231,10 @@ fn main() {
233231
if total2 == x + y {
234232
let mut broken_nodes2 = renames.keys().copied().collect::<Vec<_>>();
235233
broken_nodes2.sort();
236-
assert_eq!(broken_nodes, broken_nodes2);
234+
assert_eq!(
235+
broken_nodes, broken_nodes2,
236+
"This check is just for my puzzle input"
237+
);
237238
} else {
238239
panic!("Unsolvable");
239240
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy