Skip to content

Commit 264f620

Browse files
committed
Simple 2024 day 24 optimization
1 parent 3904bcf commit 264f620

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

crates/year2024/src/day24.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,16 @@ impl Day24 {
263263

264264
(wires[c1], wires[c2]) = (wires[c2], wires[c1]);
265265

266+
// Check swap didn't create a loop
266267
if !self.loops(wires) {
267-
if let ControlFlow::Break(()) = self.find_swaps(test_cases, wires, n + 1) {
268+
// Check swap fixed this test case before recursively calling and checking
269+
// all cases from the start
270+
cache.fill(None);
271+
let b = Self::evaluate(self.z_indexes[n], wires, x, y, &mut cache);
272+
if ((sum >> n) & 1 != 0) == b
273+
&& self.find_swaps(test_cases, wires, n + 1).is_break()
274+
{
275+
// This and future swaps work, found working combination
268276
return ControlFlow::Break(());
269277
}
270278
}

0 commit comments

Comments
 (0)