Day 2: make proximity check more generically applicable

This commit is contained in:
D. Scott Boggs 2023-12-03 06:55:58 -05:00
parent 66728f862e
commit 36400e5636

View file

@ -71,7 +71,7 @@ impl Number {
}
numbers
}
fn is_part(&self, grid: Grid) -> bool {
fn proximity_check(&self, grid: Grid, check: impl Fn(char, (usize, usize)) -> bool) -> bool {
for digit_index in self.location.clone() {
for xoff in [-1_i32, 0, 1] {
for yoff in [-1_i32, 0, 1] {
@ -98,7 +98,7 @@ impl Number {
.unwrap_or_else(|_| panic!("bad xoff {xoff} at digit index {digit_index}"));
let x = digit_index.min(line.len() - 1);
let chr = line[x];
if chr != '.' && !chr.is_ascii_digit() {
if check(chr, (x, y)) {
return true;
}
}
@ -106,4 +106,7 @@ impl Number {
}
false
}
fn is_part(&self, grid: Grid) -> bool {
self.proximity_check(grid, |chr, _| chr != '.' && !chr.is_ascii_digit())
}
}