master
Raw Download raw file
 1(require '[clojure.string :as str])
 2
 3(def e1i (slurp "../input/02-e1i.txt"))
 4(def p1i (slurp "../input/02-p1i.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") rock
34    (= char "Y") paper
35    (= char "Z") scissor))
36
37(defn translate
38  "translate input values into RPS, part 1"
39  [coll]
40  (map RPS coll))
41
42(defn p1s
43  ([input]
44   (->> input
45        str/split-lines             ; newline split
46        (map #(str/split % #" "))   ; split on space
47        (map #(translate %))        ; turn ascii into RPS values
48        (map #(score %))            ; score each round
49        (apply +))))                ; sum scores
50
51
52(println "e1i" (p1s e1i))
53(println "p1i" (p1s p1i))