;;; solver.el --- Second day -*- lexical-binding: t; -*- ;; ;; Copyright (C) 2022 Óscar Nájera ;; ;; Author: Óscar Nájera ;; Maintainer: Óscar Nájera ;; 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) (solver-translate my-game)) (defun solver-reactive-plays (oponent my-game) (solver-strategy-a (solver-translate-strategy my-game) oponent)) (defun solver-parse (strategy) (with-temp-buffer (insert-file-contents "input") (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))))