diff --git a/day2/part1.clj b/day2/part1.clj index 1a4df6b..6b09212 100644 --- a/day2/part1.clj +++ b/day2/part1.clj @@ -8,22 +8,18 @@ (require '[clojure.string :as str]) -(loop [xpos 0 depth 0] +(loop [data {:xpos 0 :depth 0}] (let [input (read-line)] (if (empty? input) - (println (* xpos depth)) + (println (apply * (vals data))) (let [ins (str/split input #" ") n (Integer/parseInt (second ins)) ] (recur - (if (= (first ins) "forward") - (+ xpos n) - xpos - ) (case (first ins) - "up" (- depth n) - "down" (+ depth n) - depth + "forward" (update-in data [:xpos] + n) + "up" (update-in data [:depth] - n) + "down" (update-in data [:depth] + n) ) ) ) diff --git a/day2/part2.clj b/day2/part2.clj index 35fe3fb..7eeeb21 100644 --- a/day2/part2.clj +++ b/day2/part2.clj @@ -8,26 +8,22 @@ (require '[clojure.string :as str]) -(loop [xpos 0 depth 0 aim 0] +(loop [data {:xpos 0 :depth 0 :aim 0}] (let [input (read-line)] (if (empty? input) - (println (* xpos depth)) + (println (apply * (map data [:xpos :depth]))) (let [ins (str/split input #" ") n (Integer/parseInt (second ins)) ] (recur - (if (= (first ins) "forward") - (+ xpos n) - xpos - ) - (if (= (first ins) "forward") - (+ depth (* aim n)) - depth - ) (case (first ins) - "down" (+ aim n) - "up" (- aim n) - aim) + "forward" (-> data + (update-in [:xpos] + n) + (update-in [:depth] + (* (data :aim) n)) + ) + "up" (update-in data [:aim] - n) + "down" (update-in data [:aim] + n) + ) ) ) )