(ns edition2022.day2
  (:require [clojure.string :as str]))

(def input-mapping
  {"A" :rock
   "B" :paper
   "C" :scissors
   "X" :rock
   "Y" :paper
   "Z" :scissors})

(def need-for-win-mapping
  {:rock {"X" :scissors, "Y" :rock, "Z" :paper}
   :paper {"X" :rock, "Y" :paper, "Z" :scissors}
   :scissors {"X" :paper, "Y" :scissors, "Z" :rock}})

(defn read-input []
  (let [in-str (slurp "resources/day2.in")
        rows (str/split-lines in-str)
        split-rows (map #(str/split % #" ") rows)]
    split-rows))

(defn translate-input-part1 [rounds]
  (map #(map input-mapping %) rounds))

(defn translate-round-part2 [[opponent need-for-win]]
  (let [opponent-trn (input-mapping opponent)
        you-tn (get-in need-for-win-mapping [opponent-trn need-for-win])]
    [opponent-trn you-tn]))

(defn translate-input-part2 [rounds]
  (map translate-round-part2 rounds))

(def selected-score
  {:rock 1, :paper 2, :scissors 3})

(defn result-score [round]
  (let [table {[:rock :rock]         3,
               [:paper :paper]       3,
               [:scissors :scissors] 3,
               [:rock :paper]        6,
               [:paper :scissors]    6,
               [:scissors :rock]     6}
        score (table round)]
    (or score 0)))

(defn compute-round-score [round]
  (let [selected (selected-score (second round))
        result (result-score round)]
    (+ selected result)))

(defn compute-total-score [rounds]
  (apply + (map compute-round-score rounds)))

(comment
  (compute-total-score [[:rock :paper] [:paper :rock] [:scissors :scissors]])
  (read-input)
  (compute-total-score (translate-input-part1 (read-input)))
  (translate-round-part2 ["A" "Y"])
  (compute-total-score (translate-input-part2 (read-input))))