aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-02 17:09:21 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-02 17:09:21 +0100
commit9c836851537dfbabcbcbc7b0384c9f4b81d36a85 (patch)
treedea44f7ed34584f1a68dd2f99b2e4ac7a4bc9a07 /AoC2022
parentcb3961b638bd62534cba6fc286b9e36e7db72a3d (diff)
downloadscratch-9c836851537dfbabcbcbc7b0384c9f4b81d36a85.tar.gz
scratch-9c836851537dfbabcbcbc7b0384c9f4b81d36a85.tar.bz2
scratch-9c836851537dfbabcbcbc7b0384c9f4b81d36a85.zip
refactor using symbol reading
Diffstat (limited to 'AoC2022')
-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))))
+