diff --git a/src/day4.rs b/src/day4.rs index ab98f5e..16315ac 100644 --- a/src/day4.rs +++ b/src/day4.rs @@ -17,7 +17,26 @@ fn part1(input: &Grid) -> u64 { #[aoc(day4, part2)] fn part2(input: &Grid) -> 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)] @@ -42,6 +61,6 @@ mod tests { #[test] fn part2_example() { - assert_eq!(part2(&parse(EXAMPLE)), 0); + assert_eq!(part2(&parse(EXAMPLE)), 43); } }