day2: revisit, make good

master
Clyne 3 years ago
parent b2d8d90014
commit 2c026e06a3

@ -1,4 +1,4 @@
; Day 2, part 2 ; Day 2, part 1
; Read a list of instructions from stdin: ; Read a list of instructions from stdin:
; "down X" increases depth number by X, ; "down X" increases depth number by X,
; "up X" decreases depth by X, ; "up X" decreases depth by X,
@ -8,24 +8,19 @@
(require '[clojure.string :as str]) (require '[clojure.string :as str])
(loop [data {:xpos 0 :depth 0}] (println
(let [input (read-line)] (keduce *
(if (empty? input) (vals
(println (apply * (vals data))) (reduce
(let [ins (str/split input #" ") #(case (first %2)
n (Integer/parseInt (second ins)) "forward" (update %1 :xpos + (second %2))
] "up" (update %1 :depth - (second %2))
(recur "down" (update %1 :depth + (second %2))
(apply (partial update-in data)
(case (first ins)
"forward" [[:xpos] + n]
"up" [[:depth] - n]
"down" [[:depth] + n]
)
)
)
)
)
)
) )
{:xpos 0 :depth 0}
(->> (slurp "./in")
str/split-lines
(map #(str/split % #" "))
(map #(update % 1 read-string))
)))))

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

Loading…
Cancel
Save