day4: part2

This commit is contained in:
2025-12-03 21:36:46 -08:00
parent 6030065f95
commit 4b51bcac08

View File

@@ -17,7 +17,26 @@ fn part1(input: &Grid<u8>) -> u64 {
#[aoc(day4, part2)] #[aoc(day4, part2)]
fn part2(input: &Grid<u8>) -> u64 { fn part2(input: &Grid<u8>) -> u64 {
input.width() as u64 let mut grid = input.clone();
let mut removed = 0;
loop {
let mut removed_iteration = 0;
for i in 0..grid.width() * grid.height() {
let pos = grid.coord(i as i64).unwrap();
if grid.get(&pos).is_some_and(|c| *c == b'@')
&& grid.neighbours_count(pos, |c| *c == b'@') < 4
{
// remove the roll
grid.set(&pos, b'.');
removed_iteration += 1;
}
}
if removed_iteration == 0 {
return removed;
}
removed += removed_iteration
}
} }
#[cfg(test)] #[cfg(test)]
@@ -42,6 +61,6 @@ mod tests {
#[test] #[test]
fn part2_example() { fn part2_example() {
assert_eq!(part2(&parse(EXAMPLE)), 0); assert_eq!(part2(&parse(EXAMPLE)), 43);
} }
} }