From 0000031000f40bc04ee2ac52b490d15a08f4c095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Fri, 9 Dec 2022 17:10:38 +0100 Subject: [PATCH] solution: day16 --- src/bin/16.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/bin/16.rs diff --git a/src/bin/16.rs b/src/bin/16.rs new file mode 100644 index 000000000..5bf8d3e --- /dev/null +++ b/src/bin/16.rs @@ -0,0 +1,74 @@ +use lazy_static::lazy_static; +use std::collections::HashMap; + +lazy_static! { + static ref MFCSAM: HashMap = HashMap::from([ + ("children".to_string(), 3), + ("cats".to_string(), 7), + ("samoyeds".to_string(), 2), + ("pomeranians".to_string(), 3), + ("akitas".to_string(), 0), + ("vizslas".to_string(), 0), + ("goldfish".to_string(), 5), + ("trees".to_string(), 3), + ("cars".to_string(), 2), + ("perfumes".to_string(), 1), + ]); +} +pub fn part_one(input: &str) -> Option { + for line in input.lines() { + let (aunt, inventory) = line.split_once(": ").unwrap(); + let n = aunt[4..].parse().unwrap(); + let mut matching = true; + for (item, amount_str) in inventory.split(", ").map(|x| x.split_once(": ").unwrap()) { + let amount = amount_str.parse().unwrap(); + if MFCSAM.get(item).unwrap() != &amount { + matching = false; + break; + } + } + if matching { + return Some(n); + } + } + None +} +pub fn part_two(input: &str) -> Option { + for line in input.lines() { + let (aunt, inventory) = line.split_once(": ").unwrap(); + let n = aunt[4..].parse().unwrap(); + let mut matching = true; + for (item, amount_str) in inventory.split(", ").map(|x| x.split_once(": ").unwrap()) { + let amount = amount_str.parse().unwrap(); + match item { + "cats" | "trees" => { + if MFCSAM.get(item).unwrap() >= &amount { + matching = false; + break; + } + } + "pomeranians" | "goldfish" => { + if MFCSAM.get(item).unwrap() <= &amount { + matching = false; + break; + } + } + _ => { + if MFCSAM.get(item).unwrap() != &amount { + matching = false; + break; + } + } + } + } + if matching { + return Some(n); + } + } + None +} +fn main() { + let input = &aoc::read_file("inputs", 16); + aoc::solve!(1, part_one, input); + aoc::solve!(2, part_two, input); +}