day2: cleaner approach

master
Clyne 3 years ago
parent cc70fd90f3
commit 5ebea9566f

@ -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)
)
)
)

@ -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)
)
)
)
)

Loading…
Cancel
Save