summary refs log tree commit diff homepage
path: root/2017
diff options
context:
space:
mode:
authorJune McEnroe <programble@gmail.com>2017-12-03 02:31:14 -0500
committerJune McEnroe <programble@gmail.com>2017-12-03 02:31:14 -0500
commit4e0ca00936fca466dbd47a0e81aaaefa73bc0afc (patch)
tree151ebacf7fafffbbe019bacccd7798b2fb9bd12e /2017
parentDay 2, part 2 (diff)
downloadaoc-4e0ca00936fca466dbd47a0e81aaaefa73bc0afc.tar.gz
aoc-4e0ca00936fca466dbd47a0e81aaaefa73bc0afc.zip
Day 3
This is fucking awful and I'm angry.
Diffstat (limited to '2017')
-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));
+}
in/up.sh?id=8a637380b556760be922624584d3774c4952f8cf&follow=1'>Use scrot for up -s if no screencaptureJune McEnroe 2021-02-07Enable mouse acceleration in XJune McEnroe 2021-02-07Set colours for Xt and cwmJune McEnroe 2021-02-07Set urgency on bell in xtermJune McEnroe 2021-02-07Add bindings for brightness controlJune McEnroe 2021-02-07Set X key repeat rateJune McEnroe 2021-02-07Bump font size to 12June McEnroe 2021-02-07Fully configure and rebind cwmJune McEnroe 2021-02-07Add BintiJune McEnroe 2021-02-07Finish configuring xtermJune McEnroe 2021-02-06Enable tapping, reverse scroll, set scaling in wsconsctlJune McEnroe 2021-02-06Set root window to black on purple snowJune McEnroe 2021-02-06Add xmodmap configurationJune McEnroe 2021-02-06Add initial OpenBSD X configurationJune McEnroe 2021-02-06Add xterm output to schemeJune McEnroe