aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-02 16:39:49 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-02 16:39:49 +0100
commitcb3961b638bd62534cba6fc286b9e36e7db72a3d (patch)
treeabb4771cacc302edf49321e0a442df40faefad0c /AoC2022
parent41c69084bf44c3bcd7eaff0b333a8f16638c8687 (diff)
downloadscratch-cb3961b638bd62534cba6fc286b9e36e7db72a3d.tar.gz
scratch-cb3961b638bd62534cba6fc286b9e36e7db72a3d.tar.bz2
scratch-cb3961b638bd62534cba6fc286b9e36e7db72a3d.zip
refactor
Diffstat (limited to 'AoC2022')
-rw-r--r--AoC2022/02/solver.el30
1 files changed, 12 insertions, 18 deletions
diff --git a/AoC2022/02/solver.el b/AoC2022/02/solver.el
index 0bb92d2..9a1d68b 100644
--- a/AoC2022/02/solver.el
+++ b/AoC2022/02/solver.el
@@ -60,28 +60,22 @@
('win (car (rassq b solver-results)))))
;; 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))))
+
+(defun solver-fixed-plays (_oponent my-game)
+ (solver-translate my-game))
(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))))
+ (solver-strategy-a (solver-translate-strategy my-game) oponent))
-(defun solver-parse ()
+(defun solver-parse (strategy)
(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))))
+ (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)))
(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))))))
+ (should (= 12535 (solver-parse #'solver-fixed-plays)))
+ (should (= 15457 (solver-parse #'solver-reactive-plays))))