summary refs log tree commit diff homepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-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!()
 }
53d1cd5dff785c59a713ca8c73207&follow=1'>Revert "Add AutoClose"June McEnroe This reverts commit 2705f4b91a56caf4cf2b3b89b247580debd5d453. This fixes the delay when leaving insert mode 2012-01-22Revert "Disable powerline for now"June McEnroe This reverts commit 171b1aa0d1d445a1223d0d638f2798b780c3e1ce. 2012-01-22Revert "Add VCS repo directories to wildignore"June McEnroe This reverts commit f4cc36a0aca465eea866fa7131a19497d46e63f0. This fixes not being able to commit with vim-fugitive 2012-01-22Disable powerline for nowJune McEnroe 2012-01-22Update some pluginsJune McEnroe 2012-01-22Add VCS repo directories to wildignoreJune McEnroe 2012-01-22Map ,e and ,b to CtrlP file and buffer respectivelyJune McEnroe 2012-01-22Add vim-spaceJune McEnroe 2012-01-22Add AutoCloseJune McEnroe 2012-01-22Add binding for GundoJune McEnroe 2012-01-22Add GundoJune McEnroe 2012-01-22Add Jellybeans colorschemeJune McEnroe 2012-01-22Add syntasticJune McEnroe 2012-01-21Add PowerlineJune McEnroe 2012-01-21Add quicktaskJune McEnroe 2012-01-15Moved comments out of mapsJune McEnroe 2012-01-14Disable scrollbarsJune McEnroe