diff options
author | Oscar Najera <hi@oscarnajera.com> | 2022-12-13 14:29:18 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2022-12-13 14:29:18 +0100 |
commit | ae43db2e6342aa7f392b958ec6d0f98e7b04d95f (patch) | |
tree | 92b136920c12d5fc62646aa1b1560fdaabc2d174 | |
parent | 1ca84c3abd40e81b4165b34ec0ca0a2ae317154c (diff) | |
download | scratch-ae43db2e6342aa7f392b958ec6d0f98e7b04d95f.tar.gz scratch-ae43db2e6342aa7f392b958ec6d0f98e7b04d95f.tar.bz2 scratch-ae43db2e6342aa7f392b958ec6d0f98e7b04d95f.zip |
[AoC2022] Elisp 13
-rw-r--r-- | AoC2022/13/solver.el | 52 |
1 files changed, 38 insertions, 14 deletions
diff --git a/AoC2022/13/solver.el b/AoC2022/13/solver.el index c7452bb..b3b636b 100644 --- a/AoC2022/13/solver.el +++ b/AoC2022/13/solver.el @@ -15,20 +15,19 @@ ;; ;;; Code: -(with-temp-buffer - (insert-file-contents "input") - (cl-loop - for i from 1 - for a = (ignore-errors - (list - (json-parse-buffer :array-type 'list) - (json-parse-buffer :array-type 'list))) - while a - when (apply #'solver-compare a) - sum i - ;; when (= 84 i) - ;; do (apply #'solver-compare a) - )) +(defun solver-input (filename &optional prepend) + (with-temp-buffer + (when prepend + (insert "[[2]]\n[[6]]\n")) + (insert-file-contents filename) + (goto-char (point-min)) + (cl-loop + for a = (ignore-errors + (list + (json-parse-buffer :array-type 'list) + (json-parse-buffer :array-type 'list))) + while a + nconc a))) (defun solver-compare (f s) (cond @@ -53,3 +52,28 @@ ('((1) (2 3 4)) '((1) 4) t) ('() '(5) t))) (should (eq (solver-compare f s) r)))) + +(defun solver-p1 (filename) + (cl-loop + for i from 1 + for (a b) on (solver-input filename) by #'cddr + when (solver-compare a b) + sum i)) + +(ert-deftest test-1 () + (should (= 13 (solver-p1 "eg-in"))) + (should (= 5330 (solver-p1 "input")))) + +(defun solver-p2 (filename) + (let ((sorted-packets (cl-sort (solver-input filename t) #'solver-compare))) + (cl-flet ((pick (item) + (1+ (cl-position + (cl-ecase item + ('start '((2))) + ('end '((6)))) + sorted-packets :test #'equal)))) + (* (pick 'start) (pick 'end))))) + +(ert-deftest test-2 () + (should (= 140 (solver-p2 "eg-in"))) + (should (= 27648 (solver-p2 "input")))) |