2022 Day 2 Clojure

This commit is contained in:
Luke Hubmayer-Werner 2022-12-02 18:54:42 +10:30
parent 188dd149a7
commit bcc22ab414
1 changed files with 21 additions and 0 deletions

21
2022/day2.clj Normal file
View File

@ -0,0 +1,21 @@
(require '[clojure.string :as str])
(def filename "day2-input")
(def move-pairs (map #(str/split % #" ") (str/split-lines (slurp filename))))
(def move-map {"A" 1 "B" 2 "C" 3 "X" 1 "Y" 2 "Z" 3})
(defn draw-win-lose-score [our-move their-move]
(get [3 6 0] (mod (- our-move their-move) 3)))
(defn part-1-round-score
[move-pair]
(let [[opposing-move our-move] move-pair]
(def op (get move-map opposing-move))
(def us (get move-map our-move))
(+ (draw-win-lose-score us op) us)))
(println (reduce + (map part-1-round-score move-pairs))) ; Part 1
(defn part-2-round-score
[move-pair]
(let [[opposing-move our-move] move-pair]
(def op (get move-map opposing-move))
(def us-move-offset (- (get move-map our-move) 2))
(def us (+ (mod (+ (- op 1) us-move-offset) 3) 1))
(+ (draw-win-lose-score us op) us)))
(println (reduce + (map part-2-round-score move-pairs))) ; Part 2