/
p02.clj
69 lines (58 loc) · 1.03 KB
/
p02.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
;; # Day 2
(ns p02
(:require [clojure.string :as str]))
(defn parse [s]
(map keyword (str/split s #" ")))
(def data
(->> (str/split-lines (slurp "../input/02.txt"))
(map parse)))
(def lookup
{:A :rock
:B :paper
:C :scissors
:X :rock
:Y :paper
:Z :scissors})
(def beat
{:rock :scissors
:scissors :paper
:paper :rock})
;; Each choice has a shape-score.
(def scores
{:rock 1
:paper 2
:scissors 3})
(defn score
"The score for a round is the sum of the score for the shape and the outcome."
[[a b]]
(+ (scores b)
(cond
;; lose
(= b (beat a)) 0
;; tie
(= a b) 3
;; win
:else 6)))
(def ans1
(transduce
(comp
(map #(map lookup %))
(map score))
+
data))
(defn complete [[a b]]
(let [a (lookup a)]
(case b
:X [a (beat a)]
:Y [a a]
:Z [a (beat (beat a))])))
(def ans2
(transduce
(comp
(map complete)
(map score))
+
data))
(defn -main [& _]
(println "Answer1:" ans1)
(println "Answer2:" ans2))