Advent of Code solutions

Day 4

bwc9876.dev a668d38d 02846257

verified
+83 -8
+7
utils/src/grid.rs
··· 156 156 .and_then(|row| row.get(pos.x as usize)) 157 157 } 158 158 159 + /// Obtain a mutable reference to a tile in the grid 160 + pub fn get_mut<'a>(&'a mut self, pos: Position) -> Option<&'a mut T> { 161 + self.data 162 + .get_mut(pos.y as usize) 163 + .and_then(|row| row.get_mut(pos.x as usize)) 164 + } 165 + 159 166 /// Get a value from the grid at the given position, 160 167 /// panicking if the position is out of bounds. 161 168 ///
+2 -2
years/2025/src/day_2.rs
··· 28 28 .flat_map(|r| { 29 29 r.into_iter().filter(|x| { 30 30 let digs = num_digits(*x); 31 - (2..=digs).into_iter().filter(|n| digs % n == 0).any(|n| { 31 + (2..=digs).filter(|n| digs.is_multiple_of(*n)).any(|n| { 32 32 let mut parts = Vec::with_capacity(n); 33 33 let mut current = *x; 34 34 let split_at = digs / n; ··· 40 40 parts 41 41 .first() 42 42 .copied() 43 - .map_or(false, |first| parts.into_iter().all(|x| x == first)) 43 + .is_some_and(|first| parts.into_iter().all(|x| x == first)) 44 44 }) 45 45 }) 46 46 })
+54 -6
years/2025/src/day_4.rs
··· 1 - 2 1 use advent_core::{Day, day_stuff, ex_for_day}; 2 + use utils::{dir::ALL_8, tiles}; 3 3 4 4 pub struct Day4; 5 5 6 + tiles!(Tile, [ 7 + '@' => Paper, 8 + '.' => Empty 9 + ]); 10 + 11 + type Grid = utils::grid::Grid<Tile>; 12 + 6 13 impl Day for Day4 { 14 + day_stuff!(4, "13", "43", Grid); 7 15 8 - day_stuff!(4, "", ""); 16 + fn part_1(input: Self::Input) -> Option<String> { 17 + let ans = input 18 + .iter() 19 + .filter(|(_, t)| **t == Tile::Paper) 20 + .filter(|(pos, _)| { 21 + input 22 + .relatives(*pos, &ALL_8) 23 + .filter(|(_, _, t)| **t == Tile::Paper) 24 + .count() 25 + < 4 26 + }) 27 + .count(); 9 28 10 - fn part_1(_input: Self::Input) -> Option<String> { 11 - None 29 + Some(ans.to_string()) 12 30 } 13 31 14 - fn part_2(_input: Self::Input) -> Option<String> { 15 - None 32 + fn part_2(mut input: Self::Input) -> Option<String> { 33 + let mut i = 0; 34 + 35 + loop { 36 + let next = input 37 + .iter() 38 + .filter(|(_, t)| **t == Tile::Paper) 39 + .filter(|(pos, _)| { 40 + input 41 + .relatives(*pos, &ALL_8) 42 + .filter(|(_, _, t)| **t == Tile::Paper) 43 + .count() 44 + < 4 45 + }) 46 + .map(|(p, _)| p) 47 + .collect::<Vec<_>>(); 48 + 49 + if next.is_empty() { 50 + break; 51 + } else { 52 + i += next.len(); 53 + for pos in next { 54 + *(input.get_mut(pos).unwrap()) = Tile::Empty; 55 + } 56 + } 57 + } 58 + 59 + Some(i.to_string()) 60 + } 61 + 62 + fn parse_input(input: &str) -> Self::Input { 63 + Grid::parse(input) 16 64 } 17 65 }
+10
years/2025/src/examples/day_4/1.txt
··· 1 + ..@@.@@@@. 2 + @@@.@.@.@@ 3 + @@@@@.@.@@ 4 + @.@@@@..@. 5 + @@.@@@@.@@ 6 + .@@@@@@@.@ 7 + .@.@.@.@@@ 8 + @.@@@.@@@@ 9 + .@@@@@@@@. 10 + @.@.@@@.@.
+10
years/2025/src/examples/day_4/2.txt
··· 1 + ..@@.@@@@. 2 + @@@.@.@.@@ 3 + @@@@@.@.@@ 4 + @.@@@@..@. 5 + @@.@@@@.@@ 6 + .@@@@@@@.@ 7 + .@.@.@.@@@ 8 + @.@@@.@@@@ 9 + .@@@@@@@@. 10 + @.@.@@@.@.