about summary refs log tree commit diff
path: root/day02.clj
diff options
context:
space:
mode:
Diffstat (limited to 'day02.clj')
-rw-r--r--day02.clj35
1 files changed, 35 insertions, 0 deletions
diff --git a/day02.clj b/day02.clj
new file mode 100644
index 0000000..3e293ff
--- /dev/null
+++ b/day02.clj
@@ -0,0 +1,35 @@
+(ns org.vuxu.aoc2021.day02
+  (:require [clojure.string :as str]))
+
+(def data
+  (->> (slurp "day02")
+       (str/split-lines)
+       (map (fn [line]
+              (let [[command amount] (str/split line #" ")]
+                [(keyword command) (parse-long amount)])))))
+
+(def part1
+  (->> data
+       (reduce (fn [state [command amount]]
+                 (case command
+                   :forward (update state :pos   + amount)
+                   :up      (update state :depth - amount)
+                   :down    (update state :depth + amount)))
+               {:pos 0 :depth 0})
+       vals
+       (apply *)))
+;; => 1250395
+
+(def part2
+  (->> data
+       (reduce (fn [state [command amount]]
+                 (case command
+                   :forward (-> state
+                                (update :pos   + amount)
+                                (update :depth + (* (:aim state) amount)))
+                   :up      (update state :aim - amount)
+                   :down    (update state :aim + amount)))
+               {:pos 0 :depth 0 :aim 0})
+       ((juxt :pos :depth))
+       (apply *)))
+;; => 1451210346