From 1f104714138d76e64ea58ef31be1950d05da2450 Mon Sep 17 00:00:00 2001 From: kageru Date: Wed, 21 Dec 2022 13:39:42 +0100 Subject: [PATCH] bit of cleanup --- 2022/src/bin/day21.rs | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/2022/src/bin/day21.rs b/2022/src/bin/day21.rs index 3e21dc9..daf2c34 100644 --- a/2022/src/bin/day21.rs +++ b/2022/src/bin/day21.rs @@ -1,4 +1,4 @@ -#![feature(test, try_blocks, hash_drain_filter)] +#![feature(test, hash_drain_filter)] extern crate test; use aoc2022::{boilerplate, common::*}; use fnv::FnvHashMap as HashMap; @@ -19,7 +19,7 @@ fn parse_input(raw: &str) -> Parsed { raw.lines() .map(|line| { let key = &line.as_bytes()[0..4]; - let value = line[6..].as_bytes(); + let value = &line.as_bytes()[6..]; if value[0].is_ascii_digit() { return (key, Monkey::Number(parse_num(&line[6..]))); @@ -35,31 +35,25 @@ fn parse_input(raw: &str) -> Parsed { .collect() } +const ROOT: &[u8] = b"root"; + +fn resolve_monkey(monkey: &Monkey, resolved: &HashMap<&[u8], isize>) -> Option { + Some(match monkey { + Monkey::Number(n) => *n, + Monkey::Add(a, b) => resolved.get(a)? + resolved.get(b)?, + Monkey::Sub(a, b) => resolved.get(a)? - resolved.get(b)?, + Monkey::Mul(a, b) => resolved.get(a)? * resolved.get(b)?, + Monkey::Div(a, b) => resolved.get(a)? / resolved.get(b)?, + }) +} + fn part1(parsed: &Parsed) -> isize { let mut missing = parsed.clone(); let mut resolved = HashMap::<&[u8], isize>::default(); - // parsed.iter().filter_map(|(&k, m)| if let Monkey::Number(n) = m { Some((k, *n)) } else { None }).collect(); while !missing.is_empty() { - missing - .drain_filter(|&k, v| { - let o: Option<()> = try { - resolved.insert( - k, - match v { - Monkey::Number(n) => *n, - Monkey::Add(a, b) => resolved.get(a)? + resolved.get(b)?, - Monkey::Sub(a, b) => resolved.get(a)? - resolved.get(b)?, - Monkey::Mul(a, b) => resolved.get(a)? * resolved.get(b)?, - Monkey::Div(a, b) => resolved.get(a)? / resolved.get(b)?, - }, - ); - }; - o.is_some() - }) - .last(); + missing.drain_filter(|&k, v| resolve_monkey(v, &resolved).map(|n| resolved.insert(k, n)).is_some()).last(); } - let root: &[u8] = b"root"; - resolved[root] + resolved[ROOT] } fn part2(parsed: &Parsed) -> usize {