Skip to content

Commit 45754a4

Browse files
committed
Unsafe it baby
1 parent 8b718a6 commit 45754a4

File tree

1 file changed

+31
-41
lines changed

1 file changed

+31
-41
lines changed

src/day24.rs

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ pub fn part2_inner(s: &[u8]) -> &'static str {
372372
// Asuming its a swap is with the carry
373373
debug_assert_eq!(and1, ZSTART);
374374
// println!("Swaping start: {} - {}", tos(and1), tos(ZSTART));
375-
to_swap.push(xor1).unwrap();
376-
to_swap.push(and1).unwrap();
375+
to_swap.push_unchecked(xor1);
376+
to_swap.push_unchecked(and1);
377377
xor1
378378
} else {
379379
and1
@@ -382,62 +382,51 @@ pub fn part2_inner(s: &[u8]) -> &'static str {
382382
for i in 1..45 {
383383
let (and1, xor1) = inputs[i as usize];
384384
debug_assert_ne!(gates[xor1 as usize].out_1, 0);
385-
let (and1, xor1) = if gates[xor1 as usize].out_2 == 0 {
386-
to_swap.push(and1).unwrap();
387-
to_swap.push(xor1).unwrap();
385+
let (and1, xor1) = if gates.get_unchecked(xor1 as usize).out_2 == 0 {
386+
to_swap.push_unchecked(and1);
387+
to_swap.push_unchecked(xor1);
388388

389389
(xor1, and1)
390390
} else {
391391
(and1, xor1)
392392
};
393393

394-
let xor1_next1 = gates[xor1 as usize].out_1;
395-
let xor1_next2 = gates[xor1 as usize].out_2;
396-
397-
let carry_next1 = gates[carry as usize].out_1;
398-
let carry_next2 = gates[carry as usize].out_2;
399-
400-
let (next1, next2) = if xor1_next1 == carry_next1 && xor1_next2 == carry_next2 {
401-
(xor1_next1, xor1_next2)
402-
} else if xor1_next1 == carry_next2 && xor1_next2 == carry_next1 {
403-
(xor1_next1, xor1_next2)
404-
} else {
405-
unreachable!();
406-
};
394+
let next1 = gates.get_unchecked(xor1 as usize).out_1;
395+
let next2 = gates.get_unchecked(xor1 as usize).out_2;
407396

408397
let or = if and1 == ZSTART + i {
409-
to_swap.push(ZSTART + i).unwrap();
398+
to_swap.push_unchecked(ZSTART + i);
410399

411-
if gates[next1 as usize].state == State::Xor {
412-
to_swap.push(next1).unwrap();
413-
} else if gates[next2 as usize].state == State::Xor {
414-
to_swap.push(next2).unwrap();
400+
if gates.get_unchecked(next1 as usize).state == State::Xor {
401+
to_swap.push_unchecked(next1);
402+
} else if gates.get_unchecked(next2 as usize).state == State::Xor {
403+
to_swap.push_unchecked(next2);
415404
} else {
416-
unreachable!()
405+
unreachable_unchecked()
417406
}
418407

419408
// TODO: is it correct to asume this?
420-
let or = gates[next1 as usize].out_1;
409+
let or = gates.get_unchecked(next1 as usize).out_1;
421410
or
422411
} else {
423412
debug_assert_eq!(gates[and1 as usize].out_2, 0);
424-
let or_from_and1 = gates[and1 as usize].out_1;
413+
let or_from_and1 = gates.get_unchecked(and1 as usize).out_1;
425414

426415
if or_from_and1 == ZSTART + i {
427416
to_swap.push(ZSTART + i).unwrap();
428417
if gates[next1 as usize].state == State::Xor {
429-
to_swap.push(next1).unwrap();
418+
to_swap.push_unchecked(next1);
430419
next1
431420
} else if gates[next2 as usize].state == State::Xor {
432-
to_swap.push(next2).unwrap();
421+
to_swap.push_unchecked(next2);
433422
next2
434423
} else {
435-
unreachable!()
424+
unreachable_unchecked()
436425
}
437426
} else {
438-
if gates[(ZSTART + i) as usize].state != State::Xor {
439-
to_swap.push(next1).unwrap();
440-
to_swap.push(next2).unwrap();
427+
if gates.get_unchecked((ZSTART + i) as usize).state != State::Xor {
428+
to_swap.push_unchecked(next1);
429+
to_swap.push_unchecked(next2);
441430
}
442431

443432
or_from_and1
@@ -453,19 +442,20 @@ pub fn part2_inner(s: &[u8]) -> &'static str {
453442
debug_assert_eq!(to_swap.len(), 8);
454443

455444
static mut OUTPUT: [u8; 8 * 4 - 1] = [b','; 8 * 4 - 1];
456-
let mut i = 0;
457-
for w in to_swap {
445+
let mut j = 0;
446+
while j < 8 {
447+
let w = *to_swap.get_unchecked(j);
458448
if w >= ZSTART {
459449
let w = w - ZSTART;
460-
OUTPUT[i] = b'z';
461-
OUTPUT[i + 1] = (w / 10) as u8 + b'0';
462-
OUTPUT[i + 2] = (w % 10) as u8 + b'0';
450+
OUTPUT[j * 4] = b'z';
451+
OUTPUT[j * 4 + 1] = (w / 10) as u8 + b'0';
452+
OUTPUT[j * 4 + 2] = (w % 10) as u8 + b'0';
463453
} else {
464-
OUTPUT[i + 0] = (w / 26 / 26) as u8 + b'a';
465-
OUTPUT[i + 1] = (w / 26 % 26) as u8 + b'a';
466-
OUTPUT[i + 2] = (w % 26) as u8 + b'a';
454+
OUTPUT[j * 4 + 0] = (w / 26 / 26) as u8 + b'a';
455+
OUTPUT[j * 4 + 1] = (w / 26 % 26) as u8 + b'a';
456+
OUTPUT[j * 4 + 2] = (w % 26) as u8 + b'a';
467457
}
468-
i += 4;
458+
j += 1;
469459
}
470460

471461
std::str::from_utf8_unchecked(&*(&raw const OUTPUT))

0 commit comments

Comments
 (0)