Skip to content

Commit 85961a2

Browse files
committed
Use u128 to store data
1 parent 8fc5bea commit 85961a2

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/day17.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use aoc_runner_derive::aoc;
1212
// 5 5: out out b
1313
// 3 0: jnz a!=0 -> 0
1414

15-
static mut RESULT: [u8; 128] = [0; 128];
15+
#[repr(C, align(16))]
16+
struct Allign([u8; 17]);
17+
18+
static mut RESULT: Allign = Allign([0; 17]);
1619

1720
#[aoc(day17, part1)]
1821
pub fn part1(s: &str) -> &'static str {
@@ -42,25 +45,31 @@ pub fn part1(s: &str) -> &'static str {
4245
} as u64;
4346

4447
let result_ptr = (&raw mut RESULT).cast::<u8>();
45-
let mut out_ptr = result_ptr;
46-
let result_ptr = result_ptr.cast_const();
48+
let out_ptr = result_ptr;
4749

48-
std::hint::assert_unchecked(a != 0);
49-
while a != 0 {
50+
let mut res = 0u128;
51+
std::hint::assert_unchecked(a > 8);
52+
while a > 8 {
5053
let b = a % 8;
5154
let b = b ^ x;
5255
let c = a >> b;
5356
let b = b ^ y ^ c;
5457

55-
out_ptr.write((b % 8) as u8 + b'0');
56-
out_ptr.offset(1).write(b',');
57-
out_ptr = out_ptr.offset(2);
58+
res = res >> 16;
59+
res |= (((b % 8) as u8 + b'0') as u128) << 112;
60+
res |= (b',' as u128) << 120;
5861
a = a / 8;
5962
}
63+
let b = a % 8;
64+
let b = b ^ x;
65+
let c = a >> b;
66+
let b = b ^ y ^ c;
67+
let b = (b % 8) as u8 + b'0';
6068

61-
let out_len = out_ptr.offset_from(result_ptr);
69+
out_ptr.cast::<u128>().write(res);
70+
out_ptr.offset(16).write(b);
6271

63-
str::from_utf8_unchecked((*&raw const RESULT).get_unchecked(..(out_len - 1) as usize))
72+
std::str::from_utf8_unchecked(&(*&raw const RESULT).0)
6473
}
6574
}
6675

0 commit comments

Comments
 (0)