diff options
author | Oscar Najera <hi@oscarnajera.com> | 2022-12-02 16:31:29 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2022-12-02 16:31:29 +0100 |
commit | 41c69084bf44c3bcd7eaff0b333a8f16638c8687 (patch) | |
tree | c53b424e9345ba33c4e46435e0159f1cba11d0a0 /AoC2022/02 | |
parent | d5df5faacef264a1522860f5a8022007e71028d5 (diff) | |
download | scratch-41c69084bf44c3bcd7eaff0b333a8f16638c8687.tar.gz scratch-41c69084bf44c3bcd7eaff0b333a8f16638c8687.tar.bz2 scratch-41c69084bf44c3bcd7eaff0b333a8f16638c8687.zip |
refactor
Diffstat (limited to 'AoC2022/02')
-rw-r--r-- | AoC2022/02/solver.el | 45 |
1 files changed, 24 insertions, 21 deletions
diff --git a/AoC2022/02/solver.el b/AoC2022/02/solver.el index 573a2d4..0bb92d2 100644 --- a/AoC2022/02/solver.el +++ b/AoC2022/02/solver.el @@ -22,6 +22,7 @@ (require 'seq) (require 'ert) ;; play to order + (defun solver-translate (play) (pcase play ((or ?A ?X) 'rock) @@ -58,27 +59,29 @@ ('draw b) ('win (car (rassq b solver-results))))) -(solver-strategy-a 'win 'rock) +;; uni +(defun solver-fixed-plays (oponent my-game) + (let ((my-game (solver-translate my-game))) + (+ (solver-weight my-game) (solver-result-a my-game oponent)))) -(should (= 12535 - (with-temp-buffer - (insert-file-contents "input") - (seq-reduce - (lambda (acc game) - (let ((oponent (solver-translate (aref game 0))) - (my-game (solver-translate (aref game 2)))) - (+ acc (solver-weight my-game) (solver-result-a my-game oponent)))) - (split-string (buffer-string) "\n" t) - 0)))) +(defun solver-reactive-plays (oponent my-game) + (let ((my-game (solver-strategy-a (solver-translate-strategy my-game) oponent))) + (+ (solver-weight my-game) (solver-result-a my-game oponent)))) -(with-temp-buffer - (insert-file-contents "input") - (seq-reduce - (lambda (acc game) - (let* ((oponent (solver-translate (aref game 0))) - (my-strategy (solver-translate-strategy (aref game 2))) - (my-game (solver-strategy-a my-strategy oponent))) +(defun solver-parse () + (with-temp-buffer + (insert-file-contents "input") + (mapcar (lambda (game) + (cons (solver-translate (aref game 0)) (aref game 2))) + (split-string (buffer-string) "\n" t)))) - (+ acc (solver-weight my-game) (solver-result-a my-game oponent)))) - (split-string (buffer-string) "\n" t) - 0)) +(ert-deftest solver-results () + (let ((games (solver-parse))) + (should (= 12535 + (apply #'+ (mapcar (lambda (game) + (solver-fixed-plays (car game) (cdr game))) + games)))) + (should (= 15457 + (apply #'+ (mapcar (lambda (game) + (solver-reactive-plays (car game) (cdr game))) + games)))))) |