aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/05/solver.lisp
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-05 13:57:36 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-05 13:57:36 +0100
commit99d27843c1411202f32605af5037d66ba333ad77 (patch)
treee3ef3d4ec6793493d6b89d8cbf2f21947c4ea3f1 /AoC2022/05/solver.lisp
parentcdf29bac8a451008c587fc8a46099144047be15a (diff)
downloadscratch-99d27843c1411202f32605af5037d66ba333ad77.tar.gz
scratch-99d27843c1411202f32605af5037d66ba333ad77.tar.bz2
scratch-99d27843c1411202f32605af5037d66ba333ad77.zip
[AoC2022] LIPS 05
Diffstat (limited to 'AoC2022/05/solver.lisp')
-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)