AdventOfCode/2022/day2.clj

26 lines
946 B
Clojure
Raw Permalink Normal View History

2022-12-02 18:54:42 +10:30
(require '[clojure.string :as str])
2022-12-03 17:22:53 +10:30
(def filename "input/2")
2022-12-02 18:54:42 +10:30
(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]
2022-12-02 19:00:44 +10:30
(let [
[opposing-move our-move] move-pair
op (get move-map opposing-move)
us (get move-map our-move)
]
2022-12-02 18:54:42 +10:30
(+ (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]
2022-12-02 19:00:44 +10:30
(let [
[opposing-move our-move] move-pair
op (get move-map opposing-move)
us-move-offset (- (get move-map our-move) 2)
us (+ (mod (+ (- op 1) us-move-offset) 3) 1)
]
2022-12-02 18:54:42 +10:30
(+ (draw-win-lose-score us op) us)))
(println (reduce + (map part-2-round-score move-pairs))) ; Part 2