Skip to content

Commit da83c25

Browse files
committed
Early exit
1 parent 304fe23 commit da83c25

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

src/day24.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ pub fn part2_inner(s: &[u8]) -> &'static str {
435435
}
436436

437437
let mut to_swap = heapless::Vec::<u16, 8>::new();
438-
let mut add_to_to_swap = |i| {
438+
let add_to_to_swap = |i, to_swap: &mut heapless::Vec<u16, 8>| {
439439
let i = if i < 46 {
440440
26 * 26 * 26 + i
441441
} else {
@@ -449,19 +449,22 @@ pub fn part2_inner(s: &[u8]) -> &'static str {
449449
// Asuming its a swap is with the carry
450450
debug_assert_eq!(and1, ZSTART);
451451
// println!("Swaping start: {} - {}", tos(and1), tos(ZSTART));
452-
add_to_to_swap(xor1);
453-
add_to_to_swap(and1);
452+
add_to_to_swap(xor1, &mut to_swap);
453+
add_to_to_swap(and1, &mut to_swap);
454454
xor1
455455
} else {
456456
and1
457457
};
458458

459-
for i in 1..45 {
459+
'outer: for i in 1..45 {
460460
let (and1, xor1) = inputs[i as usize];
461461
debug_assert_ne!(gates[xor1 as usize].out_1, 0);
462462
let (and1, xor1) = if gates.get_unchecked(xor1 as usize).out_2 == 0 {
463-
add_to_to_swap(and1);
464-
add_to_to_swap(xor1);
463+
add_to_to_swap(and1, &mut to_swap);
464+
add_to_to_swap(xor1, &mut to_swap);
465+
if to_swap.len() == 8 {
466+
break 'outer;
467+
}
465468

466469
(xor1, and1)
467470
} else {
@@ -472,15 +475,18 @@ pub fn part2_inner(s: &[u8]) -> &'static str {
472475
let next2 = gates.get_unchecked(xor1 as usize).out_2;
473476

474477
let or = if and1 == ZSTART + i {
475-
add_to_to_swap(ZSTART + i);
478+
add_to_to_swap(ZSTART + i, &mut to_swap);
476479

477480
if gates.get_unchecked(next1 as usize).state == State::Xor {
478-
add_to_to_swap(next1);
481+
add_to_to_swap(next1, &mut to_swap);
479482
} else if gates.get_unchecked(next2 as usize).state == State::Xor {
480-
add_to_to_swap(next2);
483+
add_to_to_swap(next2, &mut to_swap);
481484
} else {
482485
unreachable_unchecked()
483486
}
487+
if to_swap.len() == 8 {
488+
break 'outer;
489+
}
484490

485491
// TODO: is it correct to asume this?
486492
let or = gates.get_unchecked(next1 as usize).out_1;
@@ -490,20 +496,27 @@ pub fn part2_inner(s: &[u8]) -> &'static str {
490496
let or_from_and1 = gates.get_unchecked(and1 as usize).out_1;
491497

492498
if or_from_and1 == ZSTART + i {
493-
add_to_to_swap(ZSTART + i);
494-
if gates[next1 as usize].state == State::Xor {
495-
add_to_to_swap(next1);
499+
add_to_to_swap(ZSTART + i, &mut to_swap);
500+
let or = if gates[next1 as usize].state == State::Xor {
501+
add_to_to_swap(next1, &mut to_swap);
496502
next1
497503
} else if gates[next2 as usize].state == State::Xor {
498-
add_to_to_swap(next2);
504+
add_to_to_swap(next2, &mut to_swap);
499505
next2
500506
} else {
501507
unreachable_unchecked()
508+
};
509+
if to_swap.len() == 8 {
510+
break 'outer;
502511
}
512+
or
503513
} else {
504514
if gates.get_unchecked((ZSTART + i) as usize).state != State::Xor {
505-
add_to_to_swap(next1);
506-
add_to_to_swap(next2);
515+
add_to_to_swap(next1, &mut to_swap);
516+
add_to_to_swap(next2, &mut to_swap);
517+
}
518+
if to_swap.len() == 8 {
519+
break 'outer;
507520
}
508521

509522
or_from_and1
@@ -513,7 +526,7 @@ pub fn part2_inner(s: &[u8]) -> &'static str {
513526
carry = or;
514527
}
515528

516-
debug_assert_eq!(carry, ZSTART + 45);
529+
// debug_assert_eq!(carry, ZSTART + 45);
517530

518531
to_swap.sort_unstable();
519532
debug_assert_eq!(to_swap.len(), 8);

0 commit comments

Comments
 (0)