aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/02/solver.el
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022/02/solver.el')
-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))