aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/02/solver.el
blob: 0bb92d2259d15e03bf1ae62c3d725c890c26b52e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
;;; solver.el --- Second day -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022 Óscar Nájera
;;
;; Author: Óscar Nájera <hi@oscarnajera.com>
;; Maintainer: Óscar Nájera <hi@oscarnajera.com>
;; Created: December 02, 2022
;; Modified: December 02, 2022
;; Version: 0.0.1
;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp
;; Homepage: https://github.com/titan/solver
;; Package-Requires: ((emacs "25.1"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;;  Second day
;;
;;; Code:

(require 'seq)
(require 'ert)
;; play to order

(defun solver-translate (play)
  (pcase play
    ((or ?A ?X) 'rock)
    ((or ?B ?Y) 'paper)
    ((or ?C ?Z) 'scissors)))

(defun solver-weight (play)
  (pcase play
    ('rock 1)
    ('paper 2)
    ('scissors 3)))

(defun solver-result-a (a b)
  (pcase (list a b)
    ((or '(rock scissors)
      '(scissors paper)
      '(paper rock)) 6)
    (`(,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)))))

;; 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-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))))

(defun solver-parse ()
  (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))))

(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))))))