aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/02/solver.lisp
blob: 58abd1b3672da713fde85d65eb376723190e2815 (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
50
51
52
53
54
55
56
(ql:quickload :fiveam)

(defconstant results '((rock . scissors) (scissors . paper) (paper . rock))
  "Win-lose pairs")

(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 other) (values 3 'draw))
    ((eq other (cdr (assoc my results))) (values 6 'win))
    (t (values 0 'loose))))

(defun strategy (play)
  (ecase play
      (X 'loose)
      (Y 'draw)
      (Z 'win)))

(defun pick-move (strategy other)
  (ecase strategy
    (loose (cdr (assoc other results)))
    (draw other)
    (win (car (rassoc other results)))))

(defun fixed-plays (oponent my)
  (declare (ignore oponent))
  (translate my))

(defun reactive-plays (oponent my)
  (pick-move (strategy my) oponent))

(defun solver (strategy)
  (with-open-file (in "input")
    (loop :for n :from 0
          :for op = (translate (read in nil nil))
          :while op
          :for my = (funcall strategy op (read in nil nil))
          :sum (+ (weight my) (match my op)))))

(fiveam:test results
  (fiveam:is (= 12535 (solver #'fixed-plays)))
  (fiveam:is (= 15457 (solver #'reactive-plays))))


(fiveam:run-all-tests)