Advent of Code solutions
1use advent_core::{day_stuff, ex_for_day, Day};
2use utils::{dir::ALL_8, tiles};
3
4pub struct Day4;
5
6tiles!(Tile, [
7 '@' => Paper,
8 '.' => Empty
9]);
10
11type Grid = utils::grid::Grid<Tile>;
12
13impl Day for Day4 {
14 day_stuff!(4, "13", "43", Grid);
15
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();
28
29 Some(ans.to_string())
30 }
31
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)
64 }
65}