From 2c109ea276ae389743534b42bc694c6c5b5d1897 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sat, 23 Dec 2017 17:33:41 -0500 Subject: Day 13, part 2 --- 2017/src/bin/day13.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to '2017/src') diff --git a/2017/src/bin/day13.rs b/2017/src/bin/day13.rs index 360e266..6765488 100644 --- a/2017/src/bin/day13.rs +++ b/2017/src/bin/day13.rs @@ -48,11 +48,47 @@ fn solve1(input: &str) -> usize { severity } +fn solve2(input: &str) -> usize { + let mut init_layers = vec![]; + for line in input.lines() { + let mut iter = line.split(": "); + let index: usize = iter.next().unwrap().parse().unwrap(); + let range = iter.next().unwrap().parse().unwrap(); + init_layers.resize(index + 1, None); + init_layers[index] = Some(Layer::new(range)); + } + + 'delay: for delay in 0.. { + if delay > 0 { + for layer in &mut init_layers { + layer.as_mut().map(Layer::step); + } + } + + let mut layers = init_layers.clone(); + for i in 0..layers.len() { + if let Some(ref layer) = layers[i] { + if layer.scanner == 0 { + continue 'delay; + } + } + + for layer in &mut layers { + layer.as_mut().map(Layer::step); + } + } + + return delay; + } + unreachable!() +} + fn main() { let mut input = String::new(); io::stdin().read_to_string(&mut input).unwrap(); println!("Part 1: {}", solve1(&input)); + println!("Part 2: {}", solve2(&input)); } #[test] @@ -66,3 +102,15 @@ fn part1() { " )); } + +#[test] +fn part2() { + assert_eq!(10, solve2( +"\ +0: 3 +1: 2 +4: 4 +6: 4 +" + )); +} -- cgit 1.4.1