aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--AoC2022/02/solver.el23
1 files changed, 12 insertions, 11 deletions
diff --git a/AoC2022/02/solver.el b/AoC2022/02/solver.el
index 9a1d68b..62dd554 100644
--- a/AoC2022/02/solver.el
+++ b/AoC2022/02/solver.el
@@ -25,9 +25,9 @@
(defun solver-translate (play)
(pcase play
- ((or ?A ?X) 'rock)
- ((or ?B ?Y) 'paper)
- ((or ?C ?Z) 'scissors)))
+ ((or 'A 'X) 'rock)
+ ((or 'B 'Y) 'paper)
+ ((or 'C 'Z) 'scissors)))
(defun solver-weight (play)
(pcase play
@@ -46,9 +46,9 @@
;; play to strategy
(defun solver-translate-strategy (play)
(pcase play
- (?X 'loose)
- (?Y 'draw)
- (?Z 'win)))
+ ('X 'loose)
+ ('Y 'draw)
+ ('Z 'win)))
(defconst solver-results '((rock . scissors) (scissors . paper) (paper . rock))
"Win-lose pairs")
@@ -70,12 +70,13 @@
(defun solver-parse (strategy)
(with-temp-buffer
(insert-file-contents "input")
- (seq-reduce (lambda (acc game)
- (let* ((oponent (solver-translate (aref game 0)))
- (my-game (funcall strategy oponent (aref game 2))))
- (+ acc (solver-weight my-game) (solver-result-a my-game oponent))))
- (split-string (buffer-string) "\n" t) 0)))
+ (goto-char 0)
+ (cl-loop while (not (looking-at (rx (* whitespace) eot)))
+ for oponent = (solver-translate (read (current-buffer)))
+ for my-game = (funcall strategy oponent (read (current-buffer)))
+ sum (+ (solver-weight my-game) (solver-result-a my-game oponent)))))
(ert-deftest solver-results ()
(should (= 12535 (solver-parse #'solver-fixed-plays)))
(should (= 15457 (solver-parse #'solver-reactive-plays))))
+