summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-rw-r--r--2017/input/day03.txt1
-rw-r--r--2017/src/bin/day03.rs57
2 files changed, 58 insertions, 0 deletions
diff --git a/2017/input/day03.txt b/2017/input/day03.txt
new file mode 100644
index 0000000..9cf67fc
--- /dev/null
+++ b/2017/input/day03.txt
@@ -0,0 +1 @@
+368078
diff --git a/2017/src/bin/day03.rs b/2017/src/bin/day03.rs
new file mode 100644
index 0000000..4f38ac3
--- /dev/null
+++ b/2017/src/bin/day03.rs
@@ -0,0 +1,57 @@
+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 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 {
+            if n == input {
+                return x.abs() + y.abs();
+            }
+
+            n += 1;
+            x += dx;
+            y += dy;
+        }
+        r += i % 2;
+        i += 1;
+    }
+    unreachable!()
+}
+
+fn main() {
+    let mut input = String::new();
+    io::stdin().read_to_string(&mut input).unwrap();
+
+    println!("Part 1: {}", solve1(input.trim().parse().unwrap()));
+}
+
+#[test]
+fn part1() {
+    assert_eq!(0, solve1(1));
+    assert_eq!(3, solve1(12));
+    assert_eq!(2, solve1(23));
+    assert_eq!(31, solve1(1024));
+}
d/bin/cgit?h=1.4.1&id=1b1974c45e5e23e5527aa43c4d9ece423a13dfdc&follow=1'>tests: add Valgrind supportJohn Keeping 2014-01-12cache: don't leave cache_slot fields uninitializedJohn Keeping 2014-01-10filter: split filter functions into their own fileJason A. Donenfeld 2014-01-10filter: make exit status localJason A. Donenfeld 2014-01-10parsing: fix header typoJason A. Donenfeld 2014-01-10cgit.c: Fix comment on bit mask hackLukas Fleischer 2014-01-10cgit.c: Use "else" for mutually exclusive branchesLukas Fleischer 2014-01-10ui-snapshot.c: Do not reinvent suffixcmp()Lukas Fleischer 2014-01-10Refactor cgit_parse_snapshots_mask()Lukas Fleischer 2014-01-10Disallow use of undocumented snapshot delimitersLukas Fleischer 2014-01-10Replace most uses of strncmp() with prefixcmp()Lukas Fleischer 2014-01-09README: Fix dependenciesLukas Fleischer 2014-01-08README: Spelling and formatting fixesLukas Fleischer 2014-01-08Fix UTF-8 with syntax-highlighting.pyPřemysl Janouch 2014-01-08Add a suggestion to the manpagePřemysl Janouch 2014-01-08Fix the example configurationPřemysl Janouch 2014-01-08Fix about-formatting.shPřemysl Janouch 2014-01-08Fix some spelling errorsPřemysl Janouch 2014-01-08filters: highlight.sh: add css comments for highlight 2.6 and 3.8Ferry Huberts