diff options
Diffstat (limited to 'AoC2022/03')
-rw-r--r-- | AoC2022/03/solver.el | 40 | ||||
-rw-r--r-- | AoC2022/03/solver.jl | 12 |
2 files changed, 26 insertions, 26 deletions
diff --git a/AoC2022/03/solver.el b/AoC2022/03/solver.el index 355ddf7..62b91e2 100644 --- a/AoC2022/03/solver.el +++ b/AoC2022/03/solver.el @@ -15,6 +15,7 @@ ;; ;;; Code: (require 'subr-x) +(require 'ert) (defsubst solver-recover-value (cross) (let ((result 0)) (while (= 0 (logand cross 1)) @@ -34,45 +35,32 @@ (defun solver-line-priority (str) (cl-assert (length> str 0)) - (let ((mid (/ (length str) 2)) - (left-pack 0) (right-pack 0)) - (cl-loop for l across (substring str 0 mid) - for r across (substring str mid) - do (progn - (setf left-pack (logior left-pack (ash 1 (- l 64)))) ;; ASCII letters start at 65=A - (setf right-pack (logior right-pack(ash 1 (- r 64))))) - for cross = (logand left-pack right-pack) - when (< 0 cross) - return (solver-to-priority (solver-recover-value cross))))) + (let ((mid (/ (length str) 2))) + (thread-first + (cl-intersection (string-to-list (substring str 0 mid)) + (string-to-list (substring str mid))) + (car) (- 64) + (solver-to-priority)))) (ert-deftest test-line-priority () (should (= 16 (solver-line-priority "vJrwpWtwJgWrhcsFMMfFFhFp"))) (should (= 19 (solver-line-priority "hMHLcmGLMLhHmsRMsSvsQSqrsrlJTTdV" ))) (should (= 20 (solver-line-priority "ttgJtRGJQctTZtZT")))) -(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) + (mapcar #'string-to-list) + (cl-reduce #'cl-intersection) + (car) (+ -64) (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)))) + (let ((packs (split-string (buffer-string)))) + (apply #'+ + (mapcar task (if (= lines 1) + packs (seq-partition packs 3))))))) (ert-deftest test-problems () (should (= 8072 (solver #'solver-line-priority 1))) diff --git a/AoC2022/03/solver.jl b/AoC2022/03/solver.jl new file mode 100644 index 0000000..1ebadbf --- /dev/null +++ b/AoC2022/03/solver.jl @@ -0,0 +1,12 @@ +using Test + +data = open("input") do f + split(read(f, String)) +end + +priority(x) = islowercase(x) ? x - 'a' + 1 : x - 'A' +27 + +@testset "solutions" begin + @test map(s-> intersect(s[1:length(s)÷2], s[length(s)÷2+1:end])[1] |> priority, data) |> sum == 8072 + @test [intersect(data[i:i+2]...)[1] for i ∈ 1:3:length(data)] .|> priority |> sum == 2567 +end |