summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2017-12-23 17:33:41 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:25 -0500
commit2c109ea276ae389743534b42bc694c6c5b5d1897 (patch)
tree04350a2f5b35375010537076ad2399fc348e98f4
parentDay 13 (diff)
downloadaoc-2c109ea276ae389743534b42bc694c6c5b5d1897.tar.gz
aoc-2c109ea276ae389743534b42bc694c6c5b5d1897.zip
Day 13, part 2
-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
+"
+    ));
+}