aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/13
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022/13')
-rw-r--r--AoC2022/13/solver.el52
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"))))