moar rust. My brain ran into so many fence post errors on this one. I should have drank more coffee before attempting.
use std::collections::HashMap; #[derive(Debug, Hash, Eq, PartialEq)] struct BagRule { num: usize, bag_type: String, } impl BagRule { fn contains_recur(&self, bag: &str, collection: &HashMap<String, Vec<BagRule>>) -> bool { if self.bag_type == bag { return true; } collection .get(&self.bag_type) .unwrap() .iter() .any(|br| br.contains_recur(bag, collection)) } fn bag_count(&self, collection: &HashMap<String, Vec<BagRule>>, prev_count: usize) -> usize { let rules = collection.get(&self.bag_type).unwrap(); if rules.is_empty() { prev_count } else { rules .iter() .map(|br| br.bag_count(collection, br.num * prev_count)) .sum::<usize>() + prev_count } } } impl From<&str> for BagRule { fn from(s: &str) -> Self { match s.find(" ") { Some(n) => { let num: usize = s[0..n].parse().unwrap(); BagRule { num, bag_type: String::from(s[n + 1..].trim_end_matches("s")), } } // no bags None => { panic!("boom") } } } } #[aoc_generator(day7)] fn to_hashmap(input: &str) -> HashMap<String, Vec<BagRule>> { input .lines() .map(|i| { let mut splt = i.split(" contain "); let bag = splt.next().unwrap().trim_end_matches("s"); let unparsed_rules = splt.next().unwrap().trim_end_matches("."); let rules: Vec<BagRule> = if unparsed_rules == "no other bags" { vec![] } else { unparsed_rules.split(", ").map(|s| s.into()).collect() }; (String::from(bag), rules) }) .collect() } #[aoc(day7, part1)] fn how_many_shiny_gold(input: &HashMap<String, Vec<BagRule>>) -> usize { input .iter() .filter(|(bag, rules)| { if bag.as_str() == "shiny gold bag" { false } else { rules .iter() .any(|br| br.contains_recur("shiny gold bag", input)) } }) .count() } #[aoc(day7, part2)] fn how_many_in_shiny_gold(input: &HashMap<String, Vec<BagRule>>) -> usize { let rules = input.get("shiny gold bag").unwrap(); return rules .iter() .map(|br| br.bag_count(input, br.num)) .sum::<usize>(); }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
moar rust. My brain ran into so many fence post errors on this one. I should have drank more coffee before attempting.