diff options
Diffstat (limited to 'AoC2022/02/solver.lisp')
-rw-r--r-- | AoC2022/02/solver.lisp | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/AoC2022/02/solver.lisp b/AoC2022/02/solver.lisp index 52f611b..df1bedc 100644 --- a/AoC2022/02/solver.lisp +++ b/AoC2022/02/solver.lisp @@ -3,6 +3,9 @@ (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) @@ -17,35 +20,25 @@ (defun match (my other) (cond + ((eq my (wins-to other)) (values 6 'win)) ((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)) +(defun fixed-plays (my other) + (declare (ignore other)) (translate my)) -(defun reactive-plays (oponent my) - (pick-move (strategy my) oponent)) +(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 n :from 0 - :for op = (translate (read in nil nil)) + (loop :for op = (translate (read in nil nil)) :while op - :for my = (funcall strategy op (read in nil nil)) + :for my = (funcall strategy (read in nil nil) op) :sum (+ (weight my) (match my op))))) (fiveam:test results |