Created
December 2, 2021 13:03
-
-
Save pradeepbishnoi/59345a6716f0c5e5d0a47ba05dbd84a3 to your computer and use it in GitHub Desktop.
Advent Of Code 2021 - Problem 2 sovled in Clojure
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
| ;; Advent Of Code 2021 | |
| ;; Author : Pradeep Bishnoi | |
| ;; Solution for Problem 2a and 2b | |
| ;; Done via REPL, it can be perfected further. | |
| (def input-data '("forward 5" "down 5" "forward 8" "up 3" "down 8" "forward 2")) | |
| (def input-list | |
| (clojure.string/split-lines (slurp "/tmp/advent/2a.txt"))) | |
| (defn process-row [input-row] | |
| (let [split-data (clojure.string/split input-row #" ") | |
| [action action-val] split-data] | |
| (list (keyword action) (Integer/parseInt action-val)))) | |
| ;; Part 1 | |
| (defn process-reduce [accumulator [action action-val]] | |
| (condp = action | |
| :forward (update accumulator :horizontal + action-val) | |
| :down (update accumulator :depth + action-val) | |
| :up (update accumulator :depth - action-val))) | |
| (->> input-list | |
| (map process-row) | |
| (reduce process-reduce {:horizontal 0 :depth 0}) | |
| (#(* (:horizontal %) (:depth %)))) | |
| ;; Part2 | |
| (defn process-reduce-aim [accumulator [action action-val]] | |
| (condp = action | |
| :forward (-> accumulator | |
| (update :horizontal + action-val) | |
| (update :depth + (* (:aim accumulator) action-val))) | |
| :down (update accumulator :aim + action-val) | |
| :up (update accumulator :aim - action-val))) | |
| (->> input-list | |
| #_input-data | |
| (map process-row) | |
| (reduce process-reduce-aim {:horizontal 0 :depth 0 :aim 0}) | |
| (#(* (:horizontal %) (:depth %)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment