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

/// Checking triangle validity.
#[derive(Clone, Debug)]
pub struct Day03 {
    input: Vec<[u32; 3]>,
}

impl Day03 {
    pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
        Ok(Self {
            input: parser::u32()
                .with_prefix(parser::take_while(u8::is_ascii_whitespace))
                .repeat_n(parser::noop())
                .parse_lines(input)?,
        })
    }

    #[must_use]
    pub fn part1(&self) -> usize {
        self.input
            .iter()
            .filter(|&&[a, b, c]| Self::valid_triangle(a, b, c))
            .count()
    }

    #[must_use]
    pub fn part2(&self) -> usize {
        self.input
            .chunks_exact(3)
            .flat_map(|w| {
                (0..3).map(|i| usize::from(Self::valid_triangle(w[0][i], w[1][i], w[2][i])))
            })
            .sum()
    }

    fn valid_triangle(a: u32, b: u32, c: u32) -> bool {
        let sum = a + b + c;
        let max = a.max(b).max(c);
        // This isn't checking for overflow, but that the shortest 2 sides (sum - max) are longer
        // than the longest side
        sum - max > max
    }
}

examples!(Day03 -> (usize, usize) [
    {input: "5 10 25", part1: 0},
]);