aboutsummaryrefslogtreecommitdiff
path: root/2022/Day02/part-two.cl
blob: 40090e2971ed54441a5488ea38bcb25af317f283 (plain) (blame)
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
(defvar *prog-input*)

(let ((ui (read-line)))
  (if (equal ui "")
    (setq *prog-input* *standard-input*)
    (setq *prog-input* (open ui))))

(let
  ((cal (read-line *prog-input* NIL)) (total-score 0)
   (your-rps '(#\0 #\X #\Y #\Z)) (enemy-rps '(#\0 #\A #\B #\C))
   (cy-rps 0) (ce-rps 0))

  (loop until (or (equal cal "end") (not cal)) do
        (setq ce-rps (position (char cal 0) enemy-rps))
        ;; We reuse numbers 1,2,3 to, for now, signify your strategy
        (setq cy-rps (position (char cal 2) your-rps))

        ;; Similarly to part-one.cl, if we write down the table of possible combinations
        ;; (from left to right, columns are "Your strategy", "Enemy choice", "What your choice should be"):
        ;; 1 1 | 3 = 1 + 2
        ;; 1 2 | 1 = 1 + 0
        ;; 1 3 | 2 = 1 + 1
        ;; ----+----------
        ;; 2 1 | 1 = 1 + 0
        ;; 2 2 | 2 = 1 + 1
        ;; 2 3 | 3 = 1 + 2
        ;; ----+----------
        ;; 3 1 | 2 = 1 + 1
        ;; 3 2 | 3 = 1 + 2
        ;; 3 3 | 1 = 1 + 0
        ;; Your choice is a rotation of the numbers 1, 2 and 3, where if your strategy is 1 (lose)
        ;; you rotate once, if your strategy is 2 (draw) you rotate zero times and if it is 3 (win)
        ;; you rotate two times.
        (setq cy-rps (+ (mod (+ cy-rps ce-rps) 3) 1))

        ;; Refer to part-one.cl for explanation on math
        (setq total-score (+ total-score (* 3 (mod (+ (- 4 ce-rps) cy-rps) 3)) cy-rps))

        (setq cal (read-line *prog-input* NIL)))

  (print total-score))

(if (not (eq *prog-input* *standard-input*))
  (close *prog-input*))