Skip to content

Commit 304fe23

Browse files
committed
Remove converions loop
1 parent cc4ce1c commit 304fe23

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/day24.rs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,22 @@ 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| {
439+
let i = if i < 46 {
440+
26 * 26 * 26 + i
441+
} else {
442+
gates.get_unchecked(i as usize).id
443+
};
444+
to_swap.push_unchecked(i);
445+
};
438446

439447
let (and1, xor1) = inputs[0];
440448
let mut carry = if xor1 != ZSTART + 0 {
441449
// Asuming its a swap is with the carry
442450
debug_assert_eq!(and1, ZSTART);
443451
// println!("Swaping start: {} - {}", tos(and1), tos(ZSTART));
444-
to_swap.push_unchecked(xor1);
445-
to_swap.push_unchecked(and1);
452+
add_to_to_swap(xor1);
453+
add_to_to_swap(and1);
446454
xor1
447455
} else {
448456
and1
@@ -452,8 +460,8 @@ pub fn part2_inner(s: &[u8]) -> &'static str {
452460
let (and1, xor1) = inputs[i as usize];
453461
debug_assert_ne!(gates[xor1 as usize].out_1, 0);
454462
let (and1, xor1) = if gates.get_unchecked(xor1 as usize).out_2 == 0 {
455-
to_swap.push_unchecked(and1);
456-
to_swap.push_unchecked(xor1);
463+
add_to_to_swap(and1);
464+
add_to_to_swap(xor1);
457465

458466
(xor1, and1)
459467
} else {
@@ -464,12 +472,12 @@ pub fn part2_inner(s: &[u8]) -> &'static str {
464472
let next2 = gates.get_unchecked(xor1 as usize).out_2;
465473

466474
let or = if and1 == ZSTART + i {
467-
to_swap.push_unchecked(ZSTART + i);
475+
add_to_to_swap(ZSTART + i);
468476

469477
if gates.get_unchecked(next1 as usize).state == State::Xor {
470-
to_swap.push_unchecked(next1);
478+
add_to_to_swap(next1);
471479
} else if gates.get_unchecked(next2 as usize).state == State::Xor {
472-
to_swap.push_unchecked(next2);
480+
add_to_to_swap(next2);
473481
} else {
474482
unreachable_unchecked()
475483
}
@@ -482,20 +490,20 @@ pub fn part2_inner(s: &[u8]) -> &'static str {
482490
let or_from_and1 = gates.get_unchecked(and1 as usize).out_1;
483491

484492
if or_from_and1 == ZSTART + i {
485-
to_swap.push(ZSTART + i).unwrap();
493+
add_to_to_swap(ZSTART + i);
486494
if gates[next1 as usize].state == State::Xor {
487-
to_swap.push_unchecked(next1);
495+
add_to_to_swap(next1);
488496
next1
489497
} else if gates[next2 as usize].state == State::Xor {
490-
to_swap.push_unchecked(next2);
498+
add_to_to_swap(next2);
491499
next2
492500
} else {
493501
unreachable_unchecked()
494502
}
495503
} else {
496504
if gates.get_unchecked((ZSTART + i) as usize).state != State::Xor {
497-
to_swap.push_unchecked(next1);
498-
to_swap.push_unchecked(next2);
505+
add_to_to_swap(next1);
506+
add_to_to_swap(next2);
499507
}
500508

501509
or_from_and1
@@ -507,13 +515,6 @@ pub fn part2_inner(s: &[u8]) -> &'static str {
507515

508516
debug_assert_eq!(carry, ZSTART + 45);
509517

510-
for i in to_swap.iter_mut() {
511-
if *i < 46 {
512-
*i = 26 * 26 * 26 + *i;
513-
} else {
514-
*i = gates.get_unchecked(*i as usize).id;
515-
}
516-
}
517518
to_swap.sort_unstable();
518519
debug_assert_eq!(to_swap.len(), 8);
519520

0 commit comments

Comments
 (0)