100 lines
2.5 KiB
Rust
100 lines
2.5 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use aoc_runner_derive::{aoc, aoc_generator};
|
|
|
|
#[aoc_generator(day16)]
|
|
fn parse(input: &str) -> Vec<Sue> {
|
|
input.lines()
|
|
.map(|line| {
|
|
let (_, hints) = line.split_once(": ").unwrap();
|
|
let hints: Vec<_> = hints.split(", ").collect();
|
|
hints.into_iter().map(|thing| {
|
|
let (name, count) = thing.split_once(": ").unwrap();
|
|
(name.to_string(), count.parse().unwrap())
|
|
}).collect()
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
#[aoc(day16, part1)]
|
|
fn part1(input: &[Sue]) -> usize {
|
|
let known = "children: 3
|
|
cats: 7
|
|
samoyeds: 2
|
|
pomeranians: 3
|
|
akitas: 0
|
|
vizslas: 0
|
|
goldfish: 5
|
|
trees: 3
|
|
cars: 2
|
|
perfumes: 1";
|
|
let known: Sue = known.split("\n")
|
|
.map(|line| {
|
|
let (name, count) = line.split_once(": ").unwrap();
|
|
(name.to_string(), count.parse().unwrap())
|
|
}).collect();
|
|
|
|
let sue = input.iter()
|
|
.enumerate()
|
|
.filter(|(_index, sue)| {
|
|
known.iter()
|
|
.all(|(property, _)| {
|
|
!sue.contains_key(property) || known.get(property) == sue.get(property)
|
|
})
|
|
})
|
|
.next().unwrap();
|
|
sue.0 + 1
|
|
}
|
|
|
|
#[aoc(day16, part2)]
|
|
fn part2(input: &[Sue]) -> usize {
|
|
let exact = "children: 3
|
|
samoyeds: 2
|
|
akitas: 0
|
|
vizslas: 0
|
|
cars: 2
|
|
perfumes: 1";
|
|
let exact = foo(exact);
|
|
let gt = "cats: 7
|
|
trees: 3";
|
|
let gt = foo(gt);
|
|
let lt = "pomeranians: 3
|
|
goldfish: 5";
|
|
let lt = foo(lt);
|
|
|
|
let sues: Vec<_> = input.iter()
|
|
.enumerate()
|
|
.filter(|(_index, sue)| {
|
|
exact.iter()
|
|
.all(|(property, _)| {
|
|
!sue.contains_key(property) || exact.get(property) == sue.get(property)
|
|
})
|
|
&& gt.iter()
|
|
.all(|(property, _)| {
|
|
!sue.contains_key(property) || gt.get(property).unwrap() < sue.get(property).unwrap()
|
|
})
|
|
&& lt.iter()
|
|
.all(|(property, _)| {
|
|
!sue.contains_key(property) || lt.get(property).unwrap() > sue.get(property).unwrap()
|
|
})
|
|
})
|
|
.collect();
|
|
dbg!(&sues);
|
|
sues[0].0 + 1
|
|
}
|
|
|
|
// 56 wrong
|
|
// 213 too low
|
|
|
|
fn foo(exact: &str) -> Sue {
|
|
let exact: Sue = exact.split("\n")
|
|
.map(|line| {
|
|
let (name, count) = line.split_once(": ").unwrap();
|
|
(name.to_string(), count.parse().unwrap())
|
|
}).collect();
|
|
exact
|
|
}
|
|
|
|
type Sue = HashMap<String, u8>;
|
|
|