Created
December 2, 2021 12:56
-
-
Save roman01la/69a310bdc3cd4265b077378aacc3f5f7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn parse-input [input] | |
(->> input | |
(str/split-lines) | |
(map #(let [[v n] (str/split % #" ")] | |
[v (Integer/parseInt n)])))) | |
(defn part-1 [input] | |
(->> (parse-input input) | |
(reduce (fn [[h d] [v n]] | |
(case v | |
"forward" [(+ h n) d] | |
"down" [h (+ d n)] | |
"up" [h (- d n)])) | |
[0 0]) | |
(apply *))) | |
(defn part-2 [input] | |
(->> (parse-input input) | |
(reduce (fn [[h d a] [v n]] | |
(case v | |
"forward" [(+ h n) (+ d (* a n)) a] | |
"down" [h d (+ a n)] | |
"up" [h d (- a n)])) | |
[0 0 0]) | |
(take 2) | |
(apply *))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment