|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +use crate::solution::{AocError, Solution}; |
| 4 | + |
| 5 | +pub type Coords = (isize, isize); |
| 6 | + |
| 7 | +#[rustfmt::skip] |
| 8 | +const NEIGHBOURS: [Coords; 8] = [ |
| 9 | + (-1, -1), (0, -1), (1, -1), |
| 10 | + (-1, 0), (1, 0), |
| 11 | + (-1, 1), (0, 1), (1, 1), |
| 12 | +]; |
| 13 | + |
| 14 | +pub struct Day04; |
| 15 | + |
| 16 | +fn parse(input: &str) -> Result<HashSet<Coords>, AocError> { |
| 17 | + let mut result = HashSet::new(); |
| 18 | + |
| 19 | + for (y, row) in input.trim().lines().enumerate() { |
| 20 | + for (x, tile) in row.chars().enumerate() { |
| 21 | + if tile == '@' { |
| 22 | + result.insert((x as isize, y as isize)); |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + Ok(result) |
| 28 | +} |
| 29 | + |
| 30 | +fn count_neighbours((x, y): &Coords, occupied: &HashSet<Coords>) -> usize { |
| 31 | + NEIGHBOURS |
| 32 | + .iter() |
| 33 | + .filter(|(dx, dy)| occupied.contains(&(x + dx, y + dy))) |
| 34 | + .count() |
| 35 | +} |
| 36 | + |
| 37 | +impl Solution for Day04 { |
| 38 | + type Part1 = usize; |
| 39 | + type Part2 = usize; |
| 40 | + |
| 41 | + fn default_input(&self) -> &'static str { |
| 42 | + include_str!("../../../inputs/2025/day04.txt") |
| 43 | + } |
| 44 | + |
| 45 | + fn part_1(&self, input: &str) -> Result<usize, AocError> { |
| 46 | + let occupied = parse(input)?; |
| 47 | + |
| 48 | + let result = occupied |
| 49 | + .iter() |
| 50 | + .filter(|roll| count_neighbours(roll, &occupied) < 4) |
| 51 | + .count(); |
| 52 | + |
| 53 | + Ok(result) |
| 54 | + } |
| 55 | + |
| 56 | + fn part_2(&self, input: &str) -> Result<usize, AocError> { |
| 57 | + let mut occupied = parse(input)?; |
| 58 | + let mut removed = 0; |
| 59 | + |
| 60 | + loop { |
| 61 | + let before = occupied.len(); |
| 62 | + let current = occupied.clone(); |
| 63 | + occupied.retain(|roll| count_neighbours(roll, ¤t) >= 4); |
| 64 | + let after = occupied.len(); |
| 65 | + |
| 66 | + removed += before - after; |
| 67 | + |
| 68 | + if before == after { |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + Ok(removed) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod tests { |
| 79 | + use super::*; |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn it_solves_part1_example() { |
| 83 | + assert_eq!( |
| 84 | + Day04.part_1( |
| 85 | + "..@@.@@@@.\n\ |
| 86 | + @@@.@.@.@@\n\ |
| 87 | + @@@@@.@.@@\n\ |
| 88 | + @.@@@@..@.\n\ |
| 89 | + @@.@@@@.@@\n\ |
| 90 | + .@@@@@@@.@\n\ |
| 91 | + .@.@.@.@@@\n\ |
| 92 | + @.@@@.@@@@\n\ |
| 93 | + .@@@@@@@@.\n\ |
| 94 | + @.@.@@@.@." |
| 95 | + ), |
| 96 | + Ok(13) |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn it_solves_part2_example() { |
| 102 | + assert_eq!( |
| 103 | + Day04.part_2( |
| 104 | + "..@@.@@@@.\n\ |
| 105 | + @@@.@.@.@@\n\ |
| 106 | + @@@@@.@.@@\n\ |
| 107 | + @.@@@@..@.\n\ |
| 108 | + @@.@@@@.@@\n\ |
| 109 | + .@@@@@@@.@\n\ |
| 110 | + .@.@.@.@@@\n\ |
| 111 | + @.@@@.@@@@\n\ |
| 112 | + .@@@@@@@@.\n\ |
| 113 | + @.@.@@@.@." |
| 114 | + ), |
| 115 | + Ok(43) |
| 116 | + ); |
| 117 | + } |
| 118 | +} |
0 commit comments