summary refs log tree commit diff homepage
path: root/2017/src
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2017-12-03 19:22:53 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:25 -0500
commit3a4c05ddc7b1b564510bfdd36283ad55ca9f599d (patch)
tree025cb1bed458ec8aeab32fd3923cad103e337f81 /2017/src
parentDay 3 (diff)
downloadaoc-3a4c05ddc7b1b564510bfdd36283ad55ca9f599d.tar.gz
aoc-3a4c05ddc7b1b564510bfdd36283ad55ca9f599d.zip
Day 3, clean up
Diffstat (limited to '2017/src')
-rw-r--r--2017/src/bin/day03.rs32
1 files changed, 5 insertions, 27 deletions
diff --git a/2017/src/bin/day03.rs b/2017/src/bin/day03.rs
index 4f38ac3..8a556a1 100644
--- a/2017/src/bin/day03.rs
+++ b/2017/src/bin/day03.rs
@@ -1,42 +1,20 @@
 use std::io::{self, Read};
 
-// 17  16  15  14  13
-// 18   5   4   3  12
-// 19   6   1   2  11
-// 20   7   8   9  10
-// 21  22  23---> ...
-//
-// 1 R
-// 1 U
-// 2 L
-// 2 D
-// 3 R
-// 3 U
-// 4 L
-// 4 D
-// 5 R
-
 fn solve1(input: i32) -> i32 {
-    let ds = [(1, 0), (0, 1), (-1, 0), (0, -1)];
-
+    let spiral = [(1, 0), (0, 1), (-1, 0), (0, -1)];
+    let (mut x, mut y) = (0i32, 0i32);
     let mut n = 1;
-    let mut x = 0i32;
-    let mut y = 0i32;
-    let mut r = 1;
-    let mut i = 0;
 
-    for &(dx, dy) in ds.iter().cycle() {
-        for _ in 0..r {
+    for (i, &(dx, dy)) in spiral.iter().cycle().enumerate() {
+        let length = 1 + i / 2;
+        for _ in 0..length {
             if n == input {
                 return x.abs() + y.abs();
             }
-
             n += 1;
             x += dx;
             y += dy;
         }
-        r += i % 2;
-        i += 1;
     }
     unreachable!()
 }