summary refs log tree commit diff homepage
path: root/2017/src
diff options
context:
space:
mode:
authorJune McEnroe <programble@gmail.com>2017-12-03 19:22:53 -0500
committerJune McEnroe <programble@gmail.com>2017-12-03 19:22:53 -0500
commitd45caa333b1b81dae4ffb395caba15ad4cc79003 (patch)
tree997d3bb4e6d000a16f03bbd8df960f770ca3c56c /2017/src
parentDay 3 (diff)
downloadaoc-d45caa333b1b81dae4ffb395caba15ad4cc79003.tar.gz
aoc-d45caa333b1b81dae4ffb395caba15ad4cc79003.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!()
 }
&follow=1'>Don't match nested parentheses in Tag for CJune McEnroe Fixes mistaken highlight of: pngWrite(file, (uint8_t []) { 0, 0, 0 }, 3); 2019-02-18Match whitespace between * [] {}June McEnroe 2019-02-18Fix function-like #define regexJune McEnroe A define like #define FOO (1) is not function-like. 2019-02-18Match Tag in RustJune McEnroe 2019-02-18Match sh functions as TagJune McEnroe 2019-02-18Match Sh and Ss as Tag in mdocJune McEnroe 2019-02-18Match statics and typedefs as TagJune McEnroe 2019-02-18Clean up htmlHeaderJune McEnroe 2019-02-18Remove hi line numberingJune McEnroe Tags are much better for referring to specific parts of a file and line numbering is better done by a post-processing tool such as cat -n or producing a two-column HTML <table>. 2019-02-18Add Tag class to hiJune McEnroe 2019-02-17Generate HTML with hi -n -f html -o anchorJune McEnroe Running hi twice to insert stuff between the head and the content is a bit of a hack but oh well. 2019-02-17Add hi -f html -o anchor for line number linksJune McEnroe 2019-02-17Simplify temp trap in upJune McEnroe 2019-02-17Add line numbers to hiJune McEnroe Renames previous -n option to -m to stay consistent with cat -n. Prefixing lines with line numbers affects where the first tab indent ends up relative to the text above it. Not sure if it's worth fixing somehow. 2019-02-17Always split spans after newlinesJune McEnroe Simplifies ANSI and IRC output code, and prepares for line numbered output. 2019-02-15Color format specifiers light cyan in vimJune McEnroe 2019-02-15Highlight Interp as yellowJune McEnroe 2019-02-15Highlight strings in sh command substitutionsJune McEnroe 2019-02-15Add nmap gpJune McEnroe 2019-02-14Avoid newline when copying URL to pasteboardJune McEnroe 2019-02-13Add forgotten "sixth" book of H2G2June McEnroe