Skip to content

Commit

Permalink
Simple 2024 day 24 optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ictrobot committed Dec 24, 2024
1 parent 3904bcf commit 264f620
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion crates/year2024/src/day24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,16 @@ impl Day24 {

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

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

0 comments on commit 264f620

Please sign in to comment.