summary refs log tree commit diff homepage
path: root/2017/src/bin
diff options
context:
space:
mode:
authorJune McEnroe <programble@gmail.com>2017-12-03 19:35:22 -0500
committerJune McEnroe <programble@gmail.com>2017-12-03 19:35:22 -0500
commit8fa601d965a75cca5ca203c308c5af9a5cf0c66a (patch)
tree89580e4902b023c8c8f81dd7ace2f7e5f3961e2d /2017/src/bin
parentDay 3, clean up (diff)
downloadaoc-8fa601d965a75cca5ca203c308c5af9a5cf0c66a.tar.gz
aoc-8fa601d965a75cca5ca203c308c5af9a5cf0c66a.zip
Day 3, part 2
I am super surprised that worked on the first try.
Diffstat (limited to '2017/src/bin')
-rw-r--r--2017/src/bin/day03.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/2017/src/bin/day03.rs b/2017/src/bin/day03.rs
index 8a556a1..99aecd2 100644
--- a/2017/src/bin/day03.rs
+++ b/2017/src/bin/day03.rs
@@ -1,3 +1,4 @@
+use std::collections::HashMap;
 use std::io::{self, Read};
 
 fn solve1(input: i32) -> i32 {
@@ -19,11 +20,44 @@ fn solve1(input: i32) -> i32 {
     unreachable!()
 }
 
+fn solve2(input: i32) -> i32 {
+    let spiral = [(1, 0), (0, 1), (-1, 0), (0, -1)];
+    let (mut x, mut y) = (0i32, 0i32);
+    let mut values = HashMap::new();
+    values.insert((0, 0), 1);
+
+    for (i, &(dx, dy)) in spiral.iter().cycle().enumerate() {
+        let length = 1 + i / 2;
+        for _ in 0..length {
+            let n =
+                values.get(&(x,     y    )).unwrap_or(&0) +
+                values.get(&(x - 1, y - 1)).unwrap_or(&0) +
+                values.get(&(x,     y - 1)).unwrap_or(&0) +
+                values.get(&(x + 1, y - 1)).unwrap_or(&0) +
+                values.get(&(x + 1, y    )).unwrap_or(&0) +
+                values.get(&(x + 1, y + 1)).unwrap_or(&0) +
+                values.get(&(x,     y + 1)).unwrap_or(&0) +
+                values.get(&(x - 1, y + 1)).unwrap_or(&0) +
+                values.get(&(x - 1, y    )).unwrap_or(&0);
+            values.insert((x, y), n);
+
+            if n > input {
+                return n;
+            }
+
+            x += dx;
+            y += dy;
+        }
+    }
+    unreachable!()
+}
+
 fn main() {
     let mut input = String::new();
     io::stdin().read_to_string(&mut input).unwrap();
 
     println!("Part 1: {}", solve1(input.trim().parse().unwrap()));
+    println!("Part 2: {}", solve2(input.trim().parse().unwrap()));
 }
 
 #[test]