aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/02/solver.el
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-02 15:57:51 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-02 15:57:51 +0100
commit7963aeb90f43207b47cadc4a996462383cd74fc4 (patch)
tree250171117404a46c4f85c67ff8e4387342956cdd /AoC2022/02/solver.el
parentad4be3275a5fa46725292cfeee72fad6835159f4 (diff)
downloadscratch-7963aeb90f43207b47cadc4a996462383cd74fc4.tar.gz
scratch-7963aeb90f43207b47cadc4a996462383cd74fc4.tar.bz2
scratch-7963aeb90f43207b47cadc4a996462383cd74fc4.zip
[AoC2022] 02-01 elisp
Diffstat (limited to 'AoC2022/02/solver.el')
-rw-r--r--AoC2022/02/solver.el55
1 files changed, 55 insertions, 0 deletions
diff --git a/AoC2022/02/solver.el b/AoC2022/02/solver.el
new file mode 100644
index 0000000..18fe64e
--- /dev/null
+++ b/AoC2022/02/solver.el
@@ -0,0 +1,55 @@
+;;; 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)
+
+(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)))
+
+(should (= 12535
+ (with-temp-buffer
+ (insert-file-contents "input")
+ (seq-reduce
+ (lambda (acc game)
+ (let ((oponent (solver-translate (aref game 0)))
+ (my-game (solver-translate (aref game 2))))
+ (+ acc (solver-weight my-game) (solver-result-a my-game oponent))))
+ (split-string (buffer-string) "\n" t)
+ 0))))
+