aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/03/solver.el
diff options
context:
space:
mode:
Diffstat (limited to 'AoC2022/03/solver.el')
-rw-r--r--AoC2022/03/solver.el41
1 files changed, 27 insertions, 14 deletions
diff --git a/AoC2022/03/solver.el b/AoC2022/03/solver.el
index d2c7c05..355ddf7 100644
--- a/AoC2022/03/solver.el
+++ b/AoC2022/03/solver.el
@@ -14,7 +14,7 @@
;; Day 03
;;
;;; Code:
-
+(require 'subr-x)
(defsubst solver-recover-value (cross)
(let ((result 0))
(while (= 0 (logand cross 1))
@@ -50,17 +50,30 @@
(should (= 19 (solver-line-priority "hMHLcmGLMLhHmsRMsSvsQSqrsrlJTTdV" )))
(should (= 20 (solver-line-priority "ttgJtRGJQctTZtZT"))))
-(ert-deftest test-problems ()
- (should (= 8072
- (with-temp-buffer
- ;; (insert "vJrwpWtwJgWrhcsFMMfFFhFp")
- (insert-file-contents "input")
- (goto-char (point-min))
- (let ((result 0))
- (while (not (eobp))
- (cl-incf result (solver-line-priority
- (buffer-substring-no-properties
- (line-beginning-position) (line-end-position))))
- (forward-line))
- result)))))
+(defsubst solver-badge (packs)
+ (seq-reduce (lambda (acc item)
+ (logior acc (ash 1 (- item 64))))
+ packs 0))
+
+(defun solver-badge-priority (group-packs)
+ (thread-last group-packs
+ (split-string)
+ (mapcar #'solver-badge)
+ (apply #'logand)
+ (solver-recover-value)
+ (solver-to-priority)))
+(defun solver (task lines)
+ (with-temp-buffer
+ (insert-file-contents "input")
+ (goto-char (point-min))
+ (cl-loop while (not (eobp))
+ sum
+ (funcall task
+ (buffer-substring-no-properties (point)
+ (line-end-position lines)))
+ do (forward-line lines))))
+
+(ert-deftest test-problems ()
+ (should (= 8072 (solver #'solver-line-priority 1)))
+ (should (= 2567 (solver #'solver-badge-priority 3))))