Skip to content

Commit 8f20215

Browse files
committed
Make part 1 work with bit array again lol
1 parent 864798c commit 8f20215

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/day6.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ const SIZE: usize = 130;
1010
pub fn part1(s: &str) -> usize {
1111
let s = s.as_bytes();
1212

13-
let mut hor_grid = [[0u8; SIZE]; SIZE];
14-
let mut vert_grid = [[0u8; SIZE]; SIZE];
13+
let mut hor_grid = [BitArray::<[u64; SIZE.div_ceil(64)]>::default(); SIZE];
14+
let mut vert_grid = [BitArray::<[u64; SIZE.div_ceil(64)]>::default(); SIZE];
1515
let mut hor_visit = [BitArray::<[u64; SIZE.div_ceil(64)]>::default(); SIZE];
16+
// let mut vert_visit = [BitArray::<[u64; SIZE.div_ceil(64)]>::default(); SIZE];
1617

1718
for i in memchr::memchr_iter(b'#', s) {
1819
let x = i % (SIZE + 1);
1920
let y = i / (SIZE + 1);
2021

21-
hor_grid[y][x] = b'#';
22-
vert_grid[x][y] = b'#';
22+
hor_grid[y].set(x, true);
23+
vert_grid[x].set(y, true);
2324
}
2425

2526
let start = memchr::memchr(b'^', s).unwrap();
@@ -30,44 +31,45 @@ pub fn part1(s: &str) -> usize {
3031

3132
loop {
3233
// Up
33-
let Some(y_move) = memchr::memrchr(b'#', &vert_grid[x][..y]) else {
34-
for y in 0..y {
35-
hor_visit[y].set(x, true);
36-
}
37-
break;
38-
};
39-
for y in y_move + 1..y {
34+
let y_move = vert_grid[x][..y].trailing_zeros();
35+
// vert_visit[x][..y][y - y_move..].fill(true);
36+
for y in y - y_move..y {
4037
hor_visit[y].set(x, true);
4138
}
42-
y = y_move + 1;
39+
if y_move >= y {
40+
break;
41+
}
42+
y -= y_move;
4343

4444
// Right
45-
let Some(x_move) = memchr::memchr(b'#', &hor_grid[y][x + 1..]) else {
45+
let x_move = hor_grid[y][x + 1..].leading_zeros();
46+
if x + x_move > SIZE {
4647
hor_visit[y][x..SIZE].fill(true);
4748
break;
48-
};
49-
hor_visit[y][x..x + x_move + 1].fill(true);
49+
}
50+
hor_visit[y][x..SIZE][..x_move + 1].fill(true);
5051
x += x_move;
5152

5253
// Down
53-
let Some(y_move) = memchr::memchr(b'#', &vert_grid[x][y + 1..]) else {
54+
let y_move = vert_grid[x][y + 1..].leading_zeros();
55+
if y + y_move > SIZE {
5456
for y in y..SIZE {
5557
hor_visit[y].set(x, true);
5658
}
5759
break;
58-
};
60+
}
5961
for y in y..y + y_move + 1 {
6062
hor_visit[y].set(x, true);
6163
}
6264
y += y_move;
6365

6466
// Left
65-
let Some(x_move) = memchr::memrchr(b'#', &hor_grid[y][..x]) else {
66-
hor_visit[y][..x].fill(true);
67+
let x_move = hor_grid[y][..x].trailing_zeros();
68+
hor_visit[y][..x][x - x_move..].fill(true);
69+
if x_move >= x {
6770
break;
68-
};
69-
hor_visit[y][x_move + 1..x].fill(true);
70-
x = x_move + 1;
71+
}
72+
x -= x_move;
7173
}
7274

7375
// TODO: is there a smarter way to do this?

0 commit comments

Comments
 (0)