diff options
author | Oscar Najera <hi@oscarnajera.com> | 2022-12-02 18:22:22 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2022-12-02 18:22:22 +0100 |
commit | 08e588c7e3ec7606ddd66a968c6aa586b54edf69 (patch) | |
tree | 188cbd48ecca2f903788bf18791536ee8d7bb701 /AoC2022/02 | |
parent | 4901abde40a67d4c7844569bc896d9330d863324 (diff) | |
download | scratch-08e588c7e3ec7606ddd66a968c6aa586b54edf69.tar.gz scratch-08e588c7e3ec7606ddd66a968c6aa586b54edf69.tar.bz2 scratch-08e588c7e3ec7606ddd66a968c6aa586b54edf69.zip |
[AoC2022] 02 doing it in Common lisp
Diffstat (limited to 'AoC2022/02')
-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) |