1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use utils::prelude::*;

/// Box maths.
#[derive(Clone, Debug)]
pub struct Day02 {
    boxes: Vec<Box>,
}

#[derive(Clone, Debug)]
struct Box {
    l: u32,
    w: u32,
    h: u32,
}

impl Day02 {
    pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
        Ok(Self {
            boxes: parser::u32()
                .then(parser::u32().with_prefix(b'x'))
                .then(parser::u32().with_prefix(b'x'))
                .map(|(l, w, h)| Box { l, w, h })
                .parse_lines(input)?,
        })
    }

    #[must_use]
    pub fn part1(&self) -> u32 {
        self.boxes
            .iter()
            .map(|&Box { l, w, h }| {
                (2 * l * w) + (2 * w * h) + (2 * h * l) + (l * w).min(w * h).min(h * l)
            })
            .sum()
    }

    #[must_use]
    pub fn part2(&self) -> u32 {
        self.boxes
            .iter()
            .map(|&Box { l, w, h }| {
                let min = l.min(w).min(h);
                let max = l.max(w).max(h);
                let mid = l + w + h - min - max;
                (2 * min) + (2 * mid) + (l * w * h)
            })
            .sum()
    }
}

examples!(Day02 -> (u32, u32) [
    {input: "2x3x4", part1: 58, part2: 34},
    {input: "1x1x10", part1: 43, part2: 14},
]);