aoc/year2015/
day16.rs

1//! # Aunt Sue
2//!
3//! Brute force search through each aunt until we find one that matches all the facts.
4use crate::util::iter::*;
5use crate::util::parse::*;
6
7pub fn parse(input: &str) -> &str {
8    input
9}
10
11pub fn part1(input: &str) -> usize {
12    solve(input, |key, value| match key {
13        "akitas" | "vizslas" => value == 0,
14        "perfumes" => value == 1,
15        "samoyeds" | "cars" => value == 2,
16        "children" | "pomeranians" | "trees" => value == 3,
17        "goldfish" => value == 5,
18        "cats" => value == 7,
19        _ => unreachable!(),
20    })
21}
22
23pub fn part2(input: &str) -> usize {
24    solve(input, |key, value| match key {
25        "akitas" | "vizslas" => value == 0,
26        "perfumes" => value == 1,
27        "samoyeds" | "cars" => value == 2,
28        "children" => value == 3,
29        "pomeranians" => value < 3,
30        "goldfish" => value < 5,
31        "trees" => value > 3,
32        "cats" => value > 7,
33        _ => unreachable!(),
34    })
35}
36
37fn solve(input: &str, predicate: fn(&str, u32) -> bool) -> usize {
38    for (index, line) in input.lines().enumerate() {
39        if line
40            .split([' ', ':', ','])
41            .filter(|s| !s.is_empty())
42            .chunk::<2>()
43            .skip(1)
44            .all(|[key, value]| predicate(key, value.unsigned()))
45        {
46            return index + 1;
47        }
48    }
49
50    unreachable!()
51}