summary refs log tree commit diff homepage
path: root/2017
diff options
context:
space:
mode:
authorJune McEnroe <programble@gmail.com>2017-12-23 17:33:41 -0500
committerJune McEnroe <programble@gmail.com>2017-12-23 17:33:41 -0500
commitfe31ed565b9bc5c4a5b74d6898f4f93c523c5b13 (patch)
tree9855d579563f5bcc559e419948ba0e8c47679bcd /2017
parentDay 13 (diff)
downloadaoc-fe31ed565b9bc5c4a5b74d6898f4f93c523c5b13.tar.gz
aoc-fe31ed565b9bc5c4a5b74d6898f4f93c523c5b13.zip
Day 13, part 2
Diffstat (limited to '2017')
-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
+"
+    ));
+}