aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--AoC2022/05/solver.lisp47
1 files changed, 47 insertions, 0 deletions
diff --git a/AoC2022/05/solver.lisp b/AoC2022/05/solver.lisp
new file mode 100644
index 0000000..037b7f3
--- /dev/null
+++ b/AoC2022/05/solver.lisp
@@ -0,0 +1,47 @@
+;; this one abuses the file format and white space
+
+(ql:quickload :fiveam)
+
+(defun build-stacks (in)
+ (let* ((input (read-line in))
+ (stack-amount (ceiling (/ (length input) 4)))
+ (stacks (make-array stack-amount :initial-element nil)))
+ (loop for dock = input then (read-line in)
+ while (not (eq (aref dock 1) #\1))
+ do (loop
+ for idx from 0 below stack-amount
+ for entry = (aref dock (+ 1 (* 4 idx)))
+ unless (eq entry #\ ) do (push entry (aref stacks idx))))
+ (map 'vector #'nreverse stacks)))
+
+(defun single-move! (stacks amount from to)
+ (dotimes (_ amount)
+ (push (pop (aref stacks from)) (aref stacks to))))
+
+(defun bulk-move! (stacks amount from to)
+ (setf (aref stacks to)
+ (append (subseq (aref stacks from) 0 amount)
+ (aref stacks to)))
+ (setf (aref stacks from) (subseq (aref stacks from) amount)))
+
+(defun apply-moves (execution in stacks)
+ (loop for action = (read in nil nil)
+ while action
+ do (let ((amount (read in nil nil))
+ (from (progn (read in nil nil) (1- (read in nil nil))))
+ (to (progn (read in nil nil) (1- (read in nil nil)))))
+ (funcall execution stacks amount from to)))
+ stacks)
+
+(defun solver (execution)
+ (with-open-file (in "input")
+ (map 'string #'car
+ (apply-moves execution in
+ (build-stacks in)))))
+
+
+(fiveam:test results
+ (fiveam:is (equal "TPGVQPFDH" (solver #'single-move!)))
+ (fiveam:is (equal "DMRDFRHHH" (solver #'bulk-move!))))
+
+(fiveam:run-all-tests)