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
use utils::prelude::*;

/// Calculating spreadsheet checksums.
#[derive(Clone, Debug)]
pub struct Day02 {
    rows: Vec<Vec<u32>>,
}

impl Day02 {
    pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
        Ok(Self {
            rows: parser::u32()
                .repeat(b' '.or(b'\t'), 2)
                .map(|mut v| {
                    v.sort_unstable();
                    v
                })
                .parse_lines(input)?,
        })
    }

    #[must_use]
    pub fn part1(&self) -> u32 {
        self.rows
            .iter()
            .map(|row| row[row.len() - 1] - row[0])
            .sum()
    }

    #[must_use]
    pub fn part2(&self) -> u32 {
        self.rows
            .iter()
            .map(|row| {
                for i in 0..row.len() - 1 {
                    for j in i + 1..row.len() {
                        if row[j] % row[i] == 0 {
                            return row[j] / row[i];
                        }
                    }
                }
                panic!("no solution found")
            })
            .sum()
    }
}

examples!(Day02 -> (u32, u32) [
    {input: "5 1 9 5\n7 5 3\n2 4 6 8", part1: 18},
    {input: "5 9 2 8\n9 4 7 3\n3 8 6 5", part2: 9},
]);