about summary refs log tree commit diff
path: root/day12.clj
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2020-12-13 16:34:55 +0100
committerLeah Neukirchen <leah@vuxu.org>2020-12-13 16:34:55 +0100
commit6f632d4daff51d874e049bcdd168366fd1b4c71c (patch)
tree690190640f7b064938c898d8ec6a94d3b907d413 /day12.clj
parentcc9dc71b287c001b1dc5f61a59a770d805d65659 (diff)
downloadadventofcode2020-6f632d4daff51d874e049bcdd168366fd1b4c71c.tar.gz
adventofcode2020-6f632d4daff51d874e049bcdd168366fd1b4c71c.tar.xz
adventofcode2020-6f632d4daff51d874e049bcdd168366fd1b4c71c.zip
day12
Diffstat (limited to 'day12.clj')
-rw-r--r--day12.clj35
1 files changed, 35 insertions, 0 deletions
diff --git a/day12.clj b/day12.clj
new file mode 100644
index 0000000..1ce50bc
--- /dev/null
+++ b/day12.clj
@@ -0,0 +1,35 @@
+(def data
+  (->> (slurp "day12")
+       (clojure.string/split-lines)
+       (map (juxt first (comp read-string #(subs % 1))))
+       vec))
+
+(defn rotl [[dx dy] deg]
+  (case deg
+     90 [(- dy) dx]
+    180 [(- dx) (- dy)]
+    270 [dy (- dx)]))
+
+(defn step1 [[x y dx dy] [ins n]]
+  (case ins
+    \N [x (+ y n) dx dy]
+    \S [x (- y n) dx dy]
+    \E [(+ x n) y dx dy]
+    \W [(- x n) y dx dy]
+    \L (concat [x y] (rotl [dx dy] n))
+    \R (concat [x y] (rotl [dx dy] (- 360 n)))    
+    \F [(+ x (* n dx)) (+ y (* n dy)) dx dy]))
+
+(apply + (map #(Math/abs %) (take 2 (reduce step1 [0 0 1 0] data)))) ; => 1294
+
+(defn step2 [[x y wx wy] [ins n]]
+  (case ins
+    \N [x y wx (+ wy n)]
+    \S [x y wx (- wy n)]
+    \E [x y (+ wx n) wy]
+    \W [x y (- wx n) wy]
+    \L (concat [x y] (rotl [wx wy] n))
+    \R (concat [x y] (rotl [wx wy] (- 360 n)))
+    \F [(+ x (* n wx)) (+ y (* n wy)) wx wy]))
+
+(apply + (map #(Math/abs %) (take 2 (reduce step2 [0 0 10 1] data)))) ; => 20592