35 lines
1.2 KiB
Common Lisp
35 lines
1.2 KiB
Common Lisp
(ql:quickload :cl-ppcre)
|
|
|
|
(setf *stack* '((W P G Z V S B) (F Z C B V J)
|
|
(C D Z N H M L V) (B J F P Z M D L)
|
|
(H Q B J G C F V) (B L S T Q F G) (V Z C G L)
|
|
(G L N) (C H F J)))
|
|
|
|
(defun replace-stack (list pos)
|
|
(setf (nth pos *stack*) list))
|
|
|
|
(defun move-one (src dest)
|
|
(progn
|
|
(replace-stack (cons (car (nth src *stack*)) (nth dest *stack*)) dest)
|
|
(replace-stack (cdr (nth src *stack*)) src)))
|
|
|
|
(defun move-two (count src dest)
|
|
(progn
|
|
(replace-stack (append (subseq (nth src *stack*) 0 count) (nth dest *stack*)) dest)
|
|
(replace-stack (subseq (nth src *stack*) count (length (nth src *stack*))) src)))
|
|
|
|
(defun part-one (input)
|
|
(progn
|
|
(loop :for line :in (uiop:read-file-lines input) :do
|
|
(ppcre:register-groups-bind (count src dest) ("(\\d+).*(\\d+).*(\\d+)" line)
|
|
(dotimes (i (parse-integer count))
|
|
(move-one (- (parse-integer src) 1) (- (parse-integer dest) 1))))))
|
|
(mapcar #'first *stack*))
|
|
|
|
(defun part-two (input)
|
|
(progn
|
|
(loop :for line :in (uiop:read-file-lines input) :do
|
|
(ppcre:register-groups-bind (count src dest) ("(\\d+).*(\\d+).*(\\d+)" line)
|
|
(move-two (parse-integer count) (- (parse-integer src) 1) (- (parse-integer dest) 1)))))
|
|
(mapcar #'first *stack*))
|