summary refs log tree commit diff homepage
path: root/2017/src
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2017-12-23 17:19:04 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:25 -0500
commit31bd13da397586bb0afbe61dab5c66bfd983c671 (patch)
tree51abad5e239ee2c160caeea7797b8f9e36df88f6 /2017/src
parentDay 12, part 2 (diff)
downloadaoc-31bd13da397586bb0afbe61dab5c66bfd983c671.tar.gz
aoc-31bd13da397586bb0afbe61dab5c66bfd983c671.zip
Day 13
Diffstat (limited to '2017/src')
-rw-r--r--2017/src/bin/day13.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/2017/src/bin/day13.rs b/2017/src/bin/day13.rs
new file mode 100644
index 0000000..360e266
--- /dev/null
+++ b/2017/src/bin/day13.rs
@@ -0,0 +1,68 @@
+use std::io::{self, Read};
+
+#[derive(Debug, Clone, Copy)]
+struct Layer {
+    range: i32,
+    scanner: i32,
+    direction: i32,
+}
+
+impl Layer {
+    fn new(range: i32) -> Self {
+        Layer { range, scanner: 0, direction: 1 }
+    }
+
+    fn step(&mut self) {
+        if self.scanner == 0 {
+            self.direction = 1;
+        } else if self.scanner == self.range - 1 {
+            self.direction = -1;
+        }
+        self.scanner += self.direction;
+    }
+}
+
+fn solve1(input: &str) -> usize {
+    let mut 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();
+        layers.resize(index + 1, None);
+        layers[index] = Some(Layer::new(range));
+    }
+
+    let mut severity = 0;
+    for i in 0..layers.len() {
+        if let Some(ref layer) = layers[i] {
+            if layer.scanner == 0 {
+                severity += i * layer.range as usize;
+            }
+        }
+
+        for layer in &mut layers {
+            layer.as_mut().map(Layer::step);
+        }
+    }
+
+    severity
+}
+
+fn main() {
+    let mut input = String::new();
+    io::stdin().read_to_string(&mut input).unwrap();
+
+    println!("Part 1: {}", solve1(&input));
+}
+
+#[test]
+fn part1() {
+    assert_eq!(24, solve1(
+"\
+0: 3
+1: 2
+4: 4
+6: 4
+"
+    ));
+}