day-9-common-lisp
cow 2022-12-06 00:31:12 -05:00
parent b8d0e56a6c
commit fce0ca949a
No known key found for this signature in database
11 changed files with 2660 additions and 10 deletions

View File

@ -19,10 +19,10 @@ set -e
PATH="$(pwd):${PATH}"
source ../../aoc-utils.sh
source ../aoc-utils.sh
aoc_expect part-1.awk ../test-input.txt 24000
aoc_expect part-2.awk ../test-input.txt 45000
aoc_expect part-1.awk ../../input/day-1-example.txt 24000
aoc_expect part-2.awk ../../input/day-1-example.txt 45000
aoc_expect part-1.awk ../input.txt 68292
aoc_expect part-2.awk ../input.txt 203203
aoc_expect part-1.awk ../../input/day-1.txt 68292
aoc_expect part-2.awk ../../input/day-1.txt 203203

View File

@ -25,7 +25,13 @@
(defmacro aoc-debug (expr)
`(let ((e ,expr))
(debug e)
(princ (format "%s\n\n" e) 'external-debugging-output)
(unless noninteractive
(debug e))
e))
(provide 'aoc)
(provide 'advent-of-code)
;; Local Variables:
;; read-symbol-shorthands: (("aoc-" . "advent-of-code-"))
;; End:

View File

@ -17,15 +17,15 @@
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(add-to-list 'load-path "../../")
(add-to-list 'load-path "../")
(require 'ert)
(require 'cl-lib)
(require 'aoc)
(defvar input-file "../input.txt")
(defvar input-file "../../input/day-1.txt")
(defvar test-input-file "../test-input.txt")
(defvar test-input-file "../../input/day-1-example.txt")
(defun eat (data)
(let ((lines (string-lines data)))

141
elisp/day-2/solution.el Executable file
View File

@ -0,0 +1,141 @@
#!/usr/bin/env -S emacs --script
;; -*- lexical-binding: t; -*-
;; Copyright (C) 2022 notroot
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
(add-to-list 'load-path "../")
(require 'ert)
(require 'advent-of-code)
(defvar solution-example-input
"../../input/day-2-example.txt")
(defvar solution-input
"../../input/day-2.txt")
(defvar solution-shapes
'(rock paper scissors))
(defvar solution-outcomes
'(win lose draw))
(defvar solution-opponent-letter-to-shape-alist
'(("A" . rock)
("B" . paper)
("C" . scissors)))
(defvar solution-player-letter-to-shape-alist
'(("X" . rock)
("Y". paper)
("Z" . scissors)))
(defvar solution-player-letter-to-outcome-alist
'(("X" . lose)
("Y" . draw)
("Z" . win)))
(defvar solution-shape-score-alist
'((rock . 1)
(paper . 2)
(scissors . 3)))
(defvar solution-outcome-score-alist
'((win . 6)
(draw . 3)
(lose . 0)))
;; Each shape points to another shape creating a cyclical list.
;; Each shape points to the shape that it beats, so deciding a game
;; outcome is a simple list search!
(defvar solution-shape-outcome-alist
'((rock . scissors)
(scissors . paper)
(paper . rock)))
(defun solution-decide-game (a b)
(cond ((equal a b)
'draw)
((equal (cdr (assoc a solution-shape-outcome-alist)) b)
'win)
(t
'lose)))
(defun solution-shape-for-outcome (a outcome)
(pcase outcome
('lose
(cdr (assoc a solution-shape-outcome-alist)))
('draw
a)
('win
(car (seq-find (lambda (c)
(equal (cdr c) a))
solution-shape-outcome-alist)))))
(defun solution-part-1 (input)
(let ((rows (seq-map (lambda (s)
(string-split s " " t))
input)))
(seq-reduce (lambda (acc row)
(let* ((their-shape (cdr (assoc (nth 0 row) solution-opponent-letter-to-shape-alist)))
(our-shape (cdr (assoc (nth 1 row) solution-player-letter-to-shape-alist)))
(outcome (solution-decide-game our-shape their-shape)))
(+ acc
(cdr (assoc our-shape solution-shape-score-alist))
(cdr (assoc outcome solution-outcome-score-alist)))))
rows
0)))
(defun solution-part-2 (input)
(let ((rows (seq-map (lambda (s)
(string-split s " " t))
input)))
(seq-reduce (lambda (acc row)
(let* ((their-shape (cdr (assoc (nth 0 row) solution-opponent-letter-to-shape-alist)))
(desired-outcome (cdr (assoc (nth 1 row) solution-player-letter-to-outcome-alist)))
(our-shape (solution-shape-for-outcome their-shape desired-outcome)))
(+ acc
(cdr (assoc our-shape solution-shape-score-alist))
(cdr (assoc desired-outcome solution-outcome-score-alist)))))
rows
0)))
(ert-deftest solution-test-decide-game ()
(should (equal (solution-decide-game 'rock 'paper) 'lose))
(should (equal (solution-decide-game 'paper 'rock) 'win))
(should (equal (solution-decide-game 'rock 'rock) 'draw)))
(ert-deftest solution-test-part-1-example ()
(should (equal (solution-part-1 (aoc-read-file-lines solution-example-input)) 15)))
(ert-deftest solution-test-part-1-input ()
(should (equal (solution-part-1 (aoc-read-file-lines solution-input)) 12740)))
(ert-deftest solution-test-part-2-example ()
(should (equal (solution-part-2 (aoc-read-file-lines solution-example-input)) 12)))
(ert-deftest solution-test-part-2 ()
(should (equal (solution-part-2 (aoc-read-file-lines solution-input)) 11980)))
(when noninteractive
(ert-run-tests-batch-and-exit))
;; Local Variables:
;; read-symbol-shorthands: (("solution-" . "advent-of-code-day-2-") ("aoc-" . "advent-of-code-"))
;; End:

3
input/day-2-example.txt Normal file
View File

@ -0,0 +1,3 @@
A Y
B X
C Z

2500
input/day-2.txt Normal file

File diff suppressed because it is too large Load Diff