From 99d27843c1411202f32605af5037d66ba333ad77 Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Mon, 5 Dec 2022 13:57:36 +0100 Subject: [AoC2022] LIPS 05 --- AoC2022/05/solver.lisp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 AoC2022/05/solver.lisp (limited to 'AoC2022/05/solver.lisp') 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) -- cgit v1.2.3