day4: part1
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user