From c7f01066164817bbf624f2e7a63c6ac0aeea5f61 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Thu, 26 Jan 2023 22:09:57 +0100 Subject: Learning some julia --- AoC2022/03/solver.el | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) (limited to 'AoC2022/03/solver.el') 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))) -- cgit v1.2.3