Skip to content

Commit 6a71931

Browse files
committed
Small lut?
1 parent 99963cf commit 6a71931

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

src/day8.rs

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,20 @@ pub fn part1(s: &str) -> u32 {
2020
unsafe { part1_inner(s) }
2121
}
2222

23-
// const SHIFT_LUT: [u64; (SIZE * SIZE * 2) as usize] = {
24-
// let mut lut = [0; (SIZE * SIZE * 2) as usize];
23+
const SHIFT_LUT: [u64; (SIZE * 3) as usize] = {
24+
let mut lut = [0; (SIZE * 3) as usize];
2525

26-
// let mut x = 0;
27-
// while x < SIZE {
28-
// let mut diff_x = -SIZE + 1;
29-
30-
// let field = 1 << x;
31-
32-
// while diff_x < SIZE {
33-
// lut[(x * SIZE * 2 + diff_x + SIZE - 1) as usize] = if diff_x.is_positive() {
34-
// field << diff_x
35-
// } else {
36-
// field >> -diff_x
37-
// };
38-
39-
// diff_x += 1;
40-
// }
26+
let mut i = 0;
27+
while i < SIZE * 2 {
28+
if i >= SIZE - 1 {
29+
lut[i as usize] = 1 << i - SIZE + 1;
30+
}
4131

42-
// x += 1;
43-
// }
32+
i += 1;
33+
}
4434

45-
// lut
46-
// };
35+
lut
36+
};
4737

4838
unsafe fn part1_inner(s: &str) -> u32 {
4939
#[cfg(not(test))]
@@ -79,21 +69,15 @@ unsafe fn part1_inner(s: &str) -> u32 {
7969
if *mast_y >= diff_y {
8070
let node_y = mast_y - diff_y;
8171

82-
*antinodes.get_unchecked_mut(node_y as usize) |= if diff_x.is_positive() {
83-
1 << mast_x << diff_x
84-
} else {
85-
1 << mast_x >> -diff_x
86-
};
72+
*antinodes.get_unchecked_mut(node_y as usize) |=
73+
SHIFT_LUT[(mast_x + diff_x + SIZE - 1) as usize];
8774
}
8875

8976
if new_y + diff_y < SIZE {
9077
let node_y = new_y + diff_y;
9178

92-
*antinodes.get_unchecked_mut(node_y as usize) |= if diff_x.is_positive() {
93-
1 << new_x << diff_x
94-
} else {
95-
1 << new_x >> -diff_x
96-
};
79+
*antinodes.get_unchecked_mut(node_y as usize) |=
80+
SHIFT_LUT[(new_x - diff_x + SIZE - 1) as usize];
9781
}
9882
}
9983

0 commit comments

Comments
 (0)