day4: part1

This commit is contained in:
2025-12-03 21:28:49 -08:00
parent fa40701adb
commit 6030065f95
5 changed files with 74 additions and 1 deletions

View File

@@ -352,6 +352,28 @@ impl<T: Clone + Eq + PartialEq + Debug> Grid<T> {
}
}
/// Return the count of neighbours (8 directions) matching predicate p
pub fn neighbours_count<C: AsCoord2d, P>(&self, c: C, mut p: P) -> usize
where
P: FnMut(&T) -> bool,
{
const DIRECTIONS: [(i64, i64); 8] = [
(-1, -1),
(0, -1),
(1, -1),
(-1, 0),
(1, 0),
(1, 1),
(0, 1),
(-1, 1),
];
DIRECTIONS
.iter()
.map(|d| (c.x() + d.0, c.y() + d.1))
.filter(|c| self.get(c).is_some_and(|x| p(x)))
.count()
}
// fn window_compare_impl<const REV: bool>(&self, needle: &[T]) -> Vec<(i64, i64)> {
// if (self.width as usize) < needle.len() {
// return Vec::new();