From efcc7c32228d87f9f41cd5963491e9d11ee30dfb Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Sat, 11 Feb 2023 19:34:20 +0100 Subject: Clojure day05 --- AoC2022/05/solver.el | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) (limited to 'AoC2022/05/solver.el') diff --git a/AoC2022/05/solver.el b/AoC2022/05/solver.el index ed42bb2..b81e4be 100644 --- a/AoC2022/05/solver.el +++ b/AoC2022/05/solver.el @@ -15,6 +15,7 @@ ;; ;;; Code: (require 'subr-x) +(require 'cl-lib) (defun solver-build-stacks () (let* ((stacks-len (length @@ -27,38 +28,31 @@ (aref stacks (/ (- next (line-beginning-position) 1) 4)))) stacks)) -(defun solver-single-move! (stacks amount from to) - (dotimes (_ amount) - (push (pop (aref stacks from)) (aref stacks to)))) - -(defun solver-bulk-move! (stacks amount from to) - (setf (aref stacks to) - (append (seq-take (aref stacks from) amount) - (aref stacks to))) - (setf (aref stacks from) (seq-drop (aref stacks from) amount))) - -(defun solver-apply-moves (execution stacks) +(defun solver-apply-moves! (mover-f stacks) (while (re-search-forward (rx bol "move " (group (+ digit)) " from " (group (+ digit)) " to " (group (+ digit))) nil t) (let ((amount (string-to-number (match-string 1))) (from (1- (string-to-number (match-string 2)))) (to (1- (string-to-number (match-string 3))))) - (funcall execution stacks amount from to))) + (setf (aref stacks to) + (append (funcall mover-f (seq-take (aref stacks from) amount)) + (aref stacks to))) + (setf (aref stacks from) (seq-drop (aref stacks from) amount)))) stacks) -(defun solver (execution) +(defun solver (bulkp) (with-temp-buffer (insert-file-contents "input") (re-search-forward "^ 1") (thread-last (solver-build-stacks) - (solver-apply-moves execution) + (solver-apply-moves! (if bulkp #'identity #'reverse)) (cl-map 'string #'car)))) (ert-deftest solver-solutions () (should (equal "TPGVQPFDH" - (solver #'solver-single-move!))) + (solver nil))) (should (equal "DMRDFRHHH" - (solver #'solver-bulk-move!)))) + (solver t)))) (provide 'solver) -- cgit v1.2.3