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
55
56
57
58
59
use std::collections::HashSet;
use utils::prelude::*;

/// Checking passphrase validity.
#[derive(Clone, Debug)]
pub struct Day04<'a> {
    input: &'a str,
}

impl<'a> Day04<'a> {
    pub fn new(input: &'a str, _: InputType) -> Result<Self, InputError> {
        if let Some(w) = input.find(|c: char| !c.is_ascii_lowercase() && !c.is_ascii_whitespace()) {
            Err(InputError::new(input, w, "expected lowercase letters"))
        } else {
            Ok(Self { input })
        }
    }

    #[must_use]
    pub fn part1(&self) -> usize {
        let mut seen = HashSet::new();
        self.input
            .lines()
            .filter(|line| {
                seen.clear();
                line.split_ascii_whitespace().all(|word| seen.insert(word))
            })
            .count()
    }

    #[must_use]
    pub fn part2(&self) -> usize {
        let mut seen = HashSet::new();
        self.input
            .lines()
            .filter(|line| {
                seen.clear();
                line.split_ascii_whitespace().all(|word| {
                    let mut letters = [0u8; 26];
                    for b in word.bytes() {
                        letters[b as usize - b'a' as usize] += 1;
                    }
                    seen.insert(letters)
                })
            })
            .count()
    }
}

examples!(Day04<'_> -> (usize, usize) [
    {input: "aa bb cc dd ee", part1: 1},
    {input: "aa bb cc dd aa", part1: 0},
    {input: "aa bb cc dd aaa", part1: 1},
    {input: "abcde fghij", part2: 1},
    {input: "abcde xyz ecdab", part2: 0},
    {input: "a ab abc abd abf abj", part2: 1},
    {input: "iiii oiii ooii oooi oooo", part2: 1},
    {input: "oiii ioii iioi iiio", part2: 0},
]);