day2: cleaner approach

master
Clyne 3 years ago
parent cc70fd90f3
commit 5ebea9566f

@ -8,22 +8,18 @@
(require '[clojure.string :as str]) (require '[clojure.string :as str])
(loop [xpos 0 depth 0] (loop [data {:xpos 0 :depth 0}]
(let [input (read-line)] (let [input (read-line)]
(if (empty? input) (if (empty? input)
(println (* xpos depth)) (println (apply * (vals data)))
(let [ins (str/split input #" ") (let [ins (str/split input #" ")
n (Integer/parseInt (second ins)) n (Integer/parseInt (second ins))
] ]
(recur (recur
(if (= (first ins) "forward")
(+ xpos n)
xpos
)
(case (first ins) (case (first ins)
"up" (- depth n) "forward" (update-in data [:xpos] + n)
"down" (+ depth n) "up" (update-in data [:depth] - n)
depth "down" (update-in data [:depth] + n)
) )
) )
) )

@ -8,26 +8,22 @@
(require '[clojure.string :as str]) (require '[clojure.string :as str])
(loop [xpos 0 depth 0 aim 0] (loop [data {:xpos 0 :depth 0 :aim 0}]
(let [input (read-line)] (let [input (read-line)]
(if (empty? input) (if (empty? input)
(println (* xpos depth)) (println (apply * (map data [:xpos :depth])))
(let [ins (str/split input #" ") (let [ins (str/split input #" ")
n (Integer/parseInt (second ins)) n (Integer/parseInt (second ins))
] ]
(recur (recur
(if (= (first ins) "forward")
(+ xpos n)
xpos
)
(if (= (first ins) "forward")
(+ depth (* aim n))
depth
)
(case (first ins) (case (first ins)
"down" (+ aim n) "forward" (-> data
"up" (- aim n) (update-in [:xpos] + n)
aim) (update-in [:depth] + (* (data :aim) n))
)
"up" (update-in data [:aim] - n)
"down" (update-in data [:aim] + n)
)
) )
) )
) )

Loading…
Cancel
Save