Skip to main content

year2025/
day12.rs

1use utils::prelude::*;
2
3/// Checking region sizes.
4///
5/// The solution assumes that each present shape can be treated as a solid 3x3 square. This makes
6/// the implementation trivial and much faster than a general solution to 2D bin packing (which is
7/// NP-complete).
8#[derive(Clone, Debug)]
9pub struct Day12 {
10    part1: usize,
11}
12
13impl Day12 {
14    pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
15        let part1 = parser::digits_range!(0u32..=99)
16            .repeat_n::<2, _>(b'x')
17            .with_suffix(": ")
18            .then(parser::digits_range!(0u32..=99).repeat_fold(b' ', 1, 0u32, |acc, x| acc + x))
19            .repeat_fold(
20                parser::eol(),
21                1,
22                0usize,
23                |acc, ([width, height], shapes)| {
24                    acc + usize::from((width / 3) * (height / 3) >= shapes)
25                },
26            )
27            .with_prefix(
28                // Discard the present shapes at the start of the input
29                parser::digits_range!(0u32..=99)
30                    .with_suffix(b':')
31                    .with_eol()
32                    .then(parser::bitmask_n::<3, u8>(b".#").repeat_n::<3, _>(parser::eol()))
33                    .repeat_fold(parser::eol().with_eol(), 1, (), |(), _| ())
34                    .with_eol()
35                    .with_eol(),
36            )
37            .parse_complete(input)?;
38
39        Ok(Self { part1 })
40    }
41
42    #[must_use]
43    pub fn part1(&self) -> usize {
44        self.part1
45    }
46
47    #[must_use]
48    pub fn part2(&self) -> &'static str {
49        "🎄"
50    }
51}
52
53// The examples require actual bin packing, not just counting regions
54examples!(Day12 -> (usize, &'static str) []);