diff options
-rw-r--r-- | AoC2022/02/makefile | 2 | ||||
-rw-r--r-- | AoC2022/02/solver.lisp | 56 |
2 files changed, 57 insertions, 1 deletions
diff --git a/AoC2022/02/makefile b/AoC2022/02/makefile index e358a2c..73929a6 100644 --- a/AoC2022/02/makefile +++ b/AoC2022/02/makefile @@ -10,4 +10,4 @@ run: emacs -batch -l ert -l solver.el -f ert-run-tests-batch-and-exit - # sbcl --load ~/.sbclrc --script solver.lisp + sbcl --load ~/.sbclrc --script solver.lisp diff --git a/AoC2022/02/solver.lisp b/AoC2022/02/solver.lisp new file mode 100644 index 0000000..58abd1b --- /dev/null +++ b/AoC2022/02/solver.lisp @@ -0,0 +1,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) |