master
1(require '[clojure.string :as str])
2
3(def e2i (slurp "../input/02-e2i.txt"))
4(def p2i (slurp "../input/02-p2i.txt"))
5
6(def loss 0)
7(def draw 3)
8(def win 6)
9(def rock 1)
10(def paper 2)
11(def scissor 3)
12
13(defn score
14 "Score for user given RPS input"
15 [input]
16 (let [opponent-choice (nth input 0) user-choice (nth input 1)]
17 (+ user-choice
18 (cond
19 (= opponent-choice user-choice) draw
20 (and (= opponent-choice rock) (= user-choice scissor)) loss
21 (and (= opponent-choice rock) (= user-choice paper)) win
22 (and (= opponent-choice paper) (= user-choice rock)) loss
23 (and (= opponent-choice paper) (= user-choice scissor)) win
24 (and (= opponent-choice scissor) (= user-choice paper)) loss
25 (and (= opponent-choice scissor) (= user-choice rock)) win ))))
26
27(defn RPS
28 [char]
29 (cond
30 (= char "A") rock
31 (= char "B") paper
32 (= char "C") scissor
33 (= char "X") loss
34 (= char "Y") draw
35 (= char "Z") win))
36
37(defn translate
38 "translate input values into RPS, part 2"
39 [input]
40 (let [opponent-choice (RPS (nth input 0)) outcome (RPS (nth input 1))]
41 (cond
42 (= outcome draw) [opponent-choice opponent-choice]
43 (and (= opponent-choice rock) (= outcome loss)) [opponent-choice scissor]
44 (and (= opponent-choice rock) (= outcome win)) [opponent-choice paper]
45 (and (= opponent-choice paper) (= outcome loss)) [opponent-choice rock]
46 (and (= opponent-choice paper) (= outcome win)) [opponent-choice scissor]
47 (and (= opponent-choice scissor) (= outcome loss)) [opponent-choice paper]
48 (and (= opponent-choice scissor) (= outcome win)) [opponent-choice rock]
49 )))
50
51(defn p2s
52 ([input]
53 (->> input
54 str/split-lines ; newline split
55 (map #(str/split % #" ")) ; split on space
56 (map #(translate %)) ; turn ascii into values
57 (map #(score %)) ; score each round
58 (apply +)))) ; sum scores
59
60(println "e2i" (p2s e2i))
61(println "p2i" (p2s p2i))