year2015/
day16.rs

1use utils::prelude::*;
2
3/// Matching Aunt Sue's gift clues.
4#[derive(Clone, Debug)]
5pub struct Day16<'a> {
6    input: &'a str,
7}
8
9impl<'a> Day16<'a> {
10    pub fn new(input: &'a str, _: InputType) -> Result<Self, InputError> {
11        Ok(Self { input })
12    }
13
14    #[must_use]
15    pub fn part1(&self) -> usize {
16        self.find(|s| {
17            matches!(
18                s,
19                "children: 3"
20                    | "cats: 7"
21                    | "samoyeds: 2"
22                    | "pomeranians: 3"
23                    | "akitas: 0"
24                    | "vizslas: 0"
25                    | "goldfish: 5"
26                    | "trees: 3"
27                    | "cars: 2"
28                    | "perfumes: 1"
29            )
30        })
31    }
32
33    #[must_use]
34    pub fn part2(&self) -> usize {
35        self.find(|s| matches!(
36            s.as_bytes(),
37            b"children: 3"
38                | b"samoyeds: 2"
39                | b"akitas: 0"
40                | b"vizslas: 0"
41                | b"cars: 2"
42                | b"perfumes: 1"
43                // Greater than 7 cats
44                | [b'c', b'a', b't', b's', b':', b' ', b'8'..=b'9']
45                | [b'c', b'a', b't', b's', b':', b' ', b'1'..=b'9', b'0'..=b'9', ..]
46                // Greater than 3 trees
47                | [b't', b'r', b'e', b'e', b's', b':', b' ', b'4'..=b'9']
48                | [b't', b'r', b'e', b'e', b's', b':', b' ', b'1'..=b'9', b'0'..=b'9', ..]
49                // Fewer than 3 pomeranians
50                | [b'p', b'o', b'm', b'e', b'r', b'a', b'n', b'i', b'a', b'n', b's', b':', b' ', b'0'..=b'2']
51                // Fewer than 5 goldfish
52                | [b'g', b'o', b'l', b'd', b'f', b'i', b's', b'h', b':', b' ', b'0'..=b'4']
53        ))
54    }
55
56    fn find(&self, f: impl Fn(&str) -> bool) -> usize {
57        self.input
58            .lines()
59            .position(|line| {
60                let (_, line) = line.split_once(": ").unwrap();
61                line.split(", ").all(&f)
62            })
63            .expect("no aunts match")
64            + 1
65    }
66}
67
68examples!(Day16<'_> -> (usize, usize) []);