aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-02 16:11:26 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-02 16:11:26 +0100
commitd5df5faacef264a1522860f5a8022007e71028d5 (patch)
tree4eb4b5d2f5848d2666b2cc9828e198931f8dfe1e
parent7963aeb90f43207b47cadc4a996462383cd74fc4 (diff)
downloadscratch-d5df5faacef264a1522860f5a8022007e71028d5.tar.gz
scratch-d5df5faacef264a1522860f5a8022007e71028d5.tar.bz2
scratch-d5df5faacef264a1522860f5a8022007e71028d5.zip
[AoC2022] 02-02 elisp
-rw-r--r--AoC2022/02/solver.el31
1 files changed, 30 insertions, 1 deletions
diff --git a/AoC2022/02/solver.el b/AoC2022/02/solver.el
index 18fe64e..573a2d4 100644
--- a/AoC2022/02/solver.el
+++ b/AoC2022/02/solver.el
@@ -21,7 +21,7 @@
(require 'seq)
(require 'ert)
-
+;; play to order
(defun solver-translate (play)
(pcase play
((or ?A ?X) 'rock)
@@ -42,6 +42,24 @@
(`(,c ,c) 3)
(_ 0)))
+;; play to strategy
+(defun solver-translate-strategy (play)
+ (pcase play
+ (?X 'loose)
+ (?Y 'draw)
+ (?Z 'win)))
+
+(defconst solver-results '((rock . scissors) (scissors . paper) (paper . rock))
+ "Win-lose pairs")
+
+(defun solver-strategy-a (a b)
+ (pcase a
+ ('loose (cdr (assq b solver-results)))
+ ('draw b)
+ ('win (car (rassq b solver-results)))))
+
+(solver-strategy-a 'win 'rock)
+
(should (= 12535
(with-temp-buffer
(insert-file-contents "input")
@@ -53,3 +71,14 @@
(split-string (buffer-string) "\n" t)
0))))
+(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)))
+
+ (+ acc (solver-weight my-game) (solver-result-a my-game oponent))))
+ (split-string (buffer-string) "\n" t)
+ 0))