aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/03/solver.el
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-04 19:40:20 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-04 19:40:20 +0100
commit11cf813afa9c60d7404dc09bd0a72ea95e1a5268 (patch)
tree479f92bc0546dd659a74b04635446a4bb8aaeb5e /AoC2022/03/solver.el
parentf040734a83a241ebb654d48624f649658d9030de (diff)
downloadscratch-11cf813afa9c60d7404dc09bd0a72ea95e1a5268.tar.gz
scratch-11cf813afa9c60d7404dc09bd0a72ea95e1a5268.tar.bz2
scratch-11cf813afa9c60d7404dc09bd0a72ea95e1a5268.zip
[AoC2022] Elisp 03-02
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))))