Skip to content

Commit cb096d7

Browse files
committed
minor refactor and grid:set_all
1 parent 13bbf8c commit cb096d7

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/bin/2025_04/main.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
use aoc::{common, grid::Grid};
22

33
fn remove_if_possible(grid: &mut Grid<char>) -> Option<usize> {
4-
let positions = grid.positions(&'@');
5-
let mut to_be_removed = Vec::new();
6-
for pos in positions.iter() {
7-
let nbs = grid.adjacent_8(pos);
8-
//dbg!(pos, &nbs);
9-
if nbs.iter().filter(|x| grid.get(x) == '@').count() < 4 {
10-
to_be_removed.push(pos);
11-
}
12-
}
4+
let to_be_removed: Vec<(usize, usize)> = grid
5+
.positions(&'@')
6+
.iter()
7+
.map(|idx| (idx, grid.adjacent_8(idx)))
8+
.filter(|(_idx, neighbors)| neighbors.iter().filter(|x| grid.get(x) == '@').count() < 4)
9+
.map(|(idx, _neighbors)| *idx)
10+
.collect();
1311

1412
if to_be_removed.is_empty() {
1513
return None;
1614
}
1715

18-
for idx in to_be_removed.iter() {
19-
grid.set(idx, '.');
20-
}
16+
grid.set_all(&to_be_removed, '.');
2117

2218
Some(to_be_removed.len())
2319
}

src/grid.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ impl<T: std::fmt::Debug + Clone + Default + PartialEq + Hash> Grid<T> {
121121
ret
122122
}
123123

124+
pub fn set_all(&mut self, positions: &[CellIndex], val: T) {
125+
for idx in positions.iter() {
126+
self.set(idx, val.clone());
127+
}
128+
}
129+
124130
#[must_use]
125131
pub fn count(&self, x: &T) -> usize {
126132
self.values

0 commit comments

Comments
 (0)