From 9064da174e28ce9f56d87cda4e20b0ec02d577e1 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sat, 10 Dec 2016 23:43:56 -0500 Subject: Day 10 part 2 --- src/bin/day10.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/bin/day10.rs b/src/bin/day10.rs index d45661e..a824f56 100644 --- a/src/bin/day10.rs +++ b/src/bin/day10.rs @@ -90,7 +90,7 @@ impl FromStr for Instructions { #[derive(Default)] struct State { - outputs: HashMap>, + outputs: HashMap, bots: HashMap, comparisons: HashMap<(Chip, Chip), Bot>, } @@ -120,26 +120,26 @@ impl State { self.comparisons.insert((low, high), bot); let &(low_dest, high_dest) = instructions.bots.get(&bot).unwrap(); - self.add_to(low_dest, low); - self.add_to(high_dest, high); + self.give_to(low_dest, low); + self.give_to(high_dest, high); } true } - fn add_to(&mut self, destination: Destination, chip: Chip) { + fn give_to(&mut self, destination: Destination, chip: Chip) { match destination { Destination::Bot(bot) => { self.bots.entry(bot).or_insert_with(Default::default).add(chip); }, Destination::Output(output) => { - self.outputs.entry(output).or_insert_with(Vec::new).push(chip); + self.outputs.insert(output, chip); }, } } } -fn solve(comparison: (Chip, Chip), input: &str) -> Option { +fn solve1(comparison: (Chip, Chip), input: &str) -> Option { let instructions = input.parse().unwrap(); let mut state = State::default(); state.initialize(&instructions); @@ -147,11 +147,23 @@ fn solve(comparison: (Chip, Chip), input: &str) -> Option { state.comparisons.get(&comparison).cloned() } +fn solve2(input: &str) -> u32 { + let instructions = input.parse().unwrap(); + let mut state = State::default(); + state.initialize(&instructions); + while state.step(&instructions) { } + + state.outputs.get(&Output(0)).unwrap().0 + * state.outputs.get(&Output(1)).unwrap().0 + * state.outputs.get(&Output(2)).unwrap().0 +} + fn main() { let mut input = String::new(); io::stdin().read_to_string(&mut input).unwrap(); - println!("Part 1: {:?}", solve((Chip(17), Chip(61)), &input)); + println!("Part 1: {:?}", solve1((Chip(17), Chip(61)), &input)); + println!("Part 2: {}", solve2(&input)); } #[test] @@ -164,5 +176,5 @@ bot 1 gives low to output 1 and high to bot 0 bot 0 gives low to output 2 and high to output 0 value 2 goes to bot 2 "; - assert_eq!(Some(Bot(2)), solve((Chip(2), Chip(5)), input.trim())); + assert_eq!(Some(Bot(2)), solve1((Chip(2), Chip(5)), input.trim())); } -- cgit 1.4.1