summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-rw-r--r--2017/src/bin/day13.rs48
1 files changed, 48 insertions, 0 deletions
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
+"
+    ));
+}