blob: df1bedc1cb41588426bf2613b98aa12113217235 (
plain)
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
|
(ql:quickload :fiveam)
(defconstant results '((rock . scissors) (scissors . paper) (paper . rock))
"Win-lose pairs")
(defun looses-to (play) (cdr (assoc play results)))
(defun wins-to (play) (car (rassoc play results)))
(defun translate (play)
(case play
((A X) 'rock)
((B Y) 'paper)
((C Z) 'scissors)))
(defun weight (play)
(case play
(rock 1)
(paper 2)
(scissors 3)))
(defun match (my other)
(cond
((eq my (wins-to other)) (values 6 'win))
((eq my other) (values 3 'draw))
(t (values 0 'loose))))
(defun fixed-plays (my other)
(declare (ignore other))
(translate my))
(defun reactive-plays (my other)
(ecase my
(X (looses-to other))
(Y other)
(Z (wins-to other))))
(defun solver (strategy)
(with-open-file (in "input")
(loop :for op = (translate (read in nil nil))
:while op
:for my = (funcall strategy (read in nil nil) op)
:sum (+ (weight my) (match my op)))))
(fiveam:test results
(fiveam:is (= 12535 (solver #'fixed-plays)))
(fiveam:is (= 15457 (solver #'reactive-plays))))
|