From 4fe10ee92543b6352e90a9d8e3cee4d8cb99b0aa Mon Sep 17 00:00:00 2001 From: Lol3rrr Date: Fri, 9 Jan 2026 00:15:42 +0100 Subject: [PATCH] Implement 2025 - Day 4 - Part 2 --- 2025/04.rs | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/2025/04.rs b/2025/04.rs index 5f4caf9..aeebc46 100644 --- a/2025/04.rs +++ b/2025/04.rs @@ -11,7 +11,7 @@ enum Cell { } fn main() { - let grid: Vec<_> = CONTENT.lines().map(|l| l.trim()).filter(|l| !l.is_empty()).map(|l| { + let mut grid: Vec<_> = CONTENT.lines().map(|l| l.trim()).filter(|l| !l.is_empty()).map(|l| { l.chars().map(|c| match c { '.' => Cell::Empty, '@' => Cell::PRoll, @@ -20,23 +20,36 @@ fn main() { }).collect(); let mut result = 0; - for (y, row) in grid.iter().enumerate() { - for (x, cell) in row.iter().enumerate() { - if cell != &Cell::PRoll { - continue; - } - let neighbours = GRID_OFFSETS.iter().filter_map(|(xoff, yoff)| Some((x.checked_add_signed(*xoff)?, y.checked_add_signed(*yoff)?))).filter_map(|(x, y)| { - let cell = grid.get(y)?.get(x)?; - Some((x, y, cell.clone())) - }).filter(|(_, _, c)| c == &Cell::PRoll).count(); - + loop { + let mut to_remove = Vec::new(); + for (y, row) in grid.iter().enumerate() { + for (x, cell) in row.iter().enumerate() { + if cell != &Cell::PRoll { + continue; + } - if neighbours < 4 { - result += 1; + let neighbours = GRID_OFFSETS.iter().filter_map(|(xoff, yoff)| Some((x.checked_add_signed(*xoff)?, y.checked_add_signed(*yoff)?))).filter_map(|(x, y)| { + let cell = grid.get(y)?.get(x)?; + Some((x, y, cell.clone())) + }).filter(|(_, _, c)| c == &Cell::PRoll).count(); + + + if neighbours < 4 { + result += 1; + to_remove.push((x, y)); + } } } + if to_remove.is_empty() { + break; + } + + for (x, y) in to_remove { + grid[y][x] = Cell::Empty; + } } + println!("Result: {}", result); }