Skip to content

Commit

Permalink
Change day10 to use BitGrid.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidiakapi committed Jan 7, 2024
1 parent d3ab943 commit e1c53d1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
13 changes: 7 additions & 6 deletions framework/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,13 @@ impl BitGrid {
unsafe { *self.data.get_unchecked(index) }
}

pub fn set(&mut self, position: Vec2<u32>, value: bool) {
assert!(position.x < self.size.x && position.y < self.size.y);
self.data.set(
position.y as usize * self.size.x as usize + position.x as usize,
value,
)
#[inline]
pub fn set(&mut self, position: impl Into<Vec2<u32>>, value: bool) {
fn set_impl(this: &mut BitGrid, position: Vec2<u32>, value: bool) {
let index = this.position_to_index(position);
this.data.set(index, value)
}
set_impl(self, position.into(), value);
}

pub fn fill(&mut self, value: bool) {
Expand Down
18 changes: 6 additions & 12 deletions src/day10.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use bitvec::prelude::*;

framework::day!(10, parse => pt1, pt2);

type Vec2 = framework::vecs::Vec2<u32>;
Expand Down Expand Up @@ -61,19 +59,16 @@ fn pt1(grid: &Grid) -> Result<u32> {
}

fn pt2(grid: &Grid) -> Result<usize> {
let mut mask = BitVec::<u64, LocalBits>::new();
let w3 = grid.width() * 3;
let h3 = grid.height() * 3;
mask.resize((w3 * h3) as usize, false);

let pos_to_idx = |x: u32, y: u32| -> usize { (y * w3 + x) as usize };
let mut mask = BitGrid::new((w3, h3), false);

for_each_path_pos(grid, |position, cell| {
let p3 = position * 3;
mask.set(pos_to_idx(p3.x + 1, p3.y + 1), true);
mask.set((p3.x + 1, p3.y + 1), true);
let mut set_if = |dir: u8, ox: u32, oy: u32| {
if cell & dir == dir {
mask.set(pos_to_idx(p3.x + ox, p3.y + oy), true);
mask.set((p3.x + ox, p3.y + oy), true);
}
};
set_if(LEFT, 0, 1);
Expand All @@ -85,10 +80,9 @@ fn pt2(grid: &Grid) -> Result<usize> {
let mut stack = Vec::new();
stack.push(Vec2::zero());
while let Some(v) = stack.pop() {
mask.set(pos_to_idx(v.x, v.y), true);
mask.set((v.x, v.y), true);
for neighbor in v.neighbors(&Offset::ORTHOGONAL) {
let idx = pos_to_idx(neighbor.x, neighbor.y);
if idx < mask.len() && !mask[idx] {
if matches!(mask.get(neighbor), Some(false)) {
stack.push(neighbor);
}
}
Expand All @@ -99,7 +93,7 @@ fn pt2(grid: &Grid) -> Result<usize> {
let base = y * w3 + 1;
(base..base + w3).step_by(3)
})
.filter(|&idx| !mask[idx as usize])
.filter(|&idx| !mask.data[idx as usize])
.count();

Ok(count)
Expand Down

0 comments on commit e1c53d1

Please sign in to comment.