1use utils::prelude::*;
2
3#[derive(Clone, Debug)]
5pub struct Day02 {
6 boxes: Vec<Box>,
7}
8
9#[derive(Clone, Debug)]
10struct Box {
11 l: u32,
12 w: u32,
13 h: u32,
14}
15
16impl Day02 {
17 pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
18 Ok(Self {
19 boxes: parser::u32()
20 .then(parser::u32().with_prefix(b'x'))
21 .then(parser::u32().with_prefix(b'x'))
22 .map(|(l, w, h)| Box { l, w, h })
23 .parse_lines(input)?,
24 })
25 }
26
27 #[must_use]
28 pub fn part1(&self) -> u32 {
29 self.boxes
30 .iter()
31 .map(|&Box { l, w, h }| {
32 (2 * l * w) + (2 * w * h) + (2 * h * l) + (l * w).min(w * h).min(h * l)
33 })
34 .sum()
35 }
36
37 #[must_use]
38 pub fn part2(&self) -> u32 {
39 self.boxes
40 .iter()
41 .map(|&Box { l, w, h }| {
42 let min = l.min(w).min(h);
43 let max = l.max(w).max(h);
44 let mid = l + w + h - min - max;
45 (2 * min) + (2 * mid) + (l * w * h)
46 })
47 .sum()
48 }
49}
50
51examples!(Day02 -> (u32, u32) [
52 {input: "2x3x4", part1: 58, part2: 34},
53 {input: "1x1x10", part1: 43, part2: 14},
54]);