Skip to content

Instantly share code, notes, and snippets.

@roman01la
Created December 2, 2021 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roman01la/69a310bdc3cd4265b077378aacc3f5f7 to your computer and use it in GitHub Desktop.
Save roman01la/69a310bdc3cd4265b077378aacc3f5f7 to your computer and use it in GitHub Desktop.
(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