From c11120407f700c4b6523974f2c4ea677fce0cd9b Mon Sep 17 00:00:00 2001 From: Adrian Braemer <11058200+NobodysHero@users.noreply.github.com> Date: Fri, 14 Dec 2018 20:38:19 +0100 Subject: [PATCH] Fixed Day 13 Welp sort is destructive - now I know. Also very minor changes to day14.lisp --- day13.lisp | 75 +++++++++++++++++++----------------------------------- day14.lisp | 10 ++++---- 2 files changed, 31 insertions(+), 54 deletions(-) diff --git a/day13.lisp b/day13.lisp index 64a0315..ac60e98 100644 --- a/day13.lisp +++ b/day13.lisp @@ -62,33 +62,19 @@ (#\+ (day13-turn cart))) cart))) -(defun day13-check-collision! (cart carts) +(defun day13-check-collision! (cart minecarts) (let ((collisions (remove-if #'minecart-crashed - (remove (minecart-pos cart) carts :key #'minecart-pos :test (complement #'=))))) - (unless (<= 1 (length collisions)) - (format t "Collisions: ~%~{~S~%~}~%" collisions) + (remove (minecart-pos cart) minecarts :key #'minecart-pos :test (complement #'=))))) + (unless (= 1 (length collisions)) (mapc (lambda (c) (setf (minecart-crashed c) t)) collisions)))) (defun day13-step! (board minecarts) (loop - :for minecart :in (sort minecarts #'< :key #'minecart-pos) + :for minecart :in (sort (copy-list minecarts) #'< :key #'minecart-pos) :unless (minecart-crashed minecart) :do (day13-move-cart! board minecart) - :and :do (day13-check-collision! minecart (remove-if #'minecart-crashed minecarts)))) - -(defun day13-print-state (board minecarts) - (let* ((width (array-dimension board 1)) - (copy (make-array (array-dimensions board) - :displaced-to (copy-seq - (make-array (array-total-size board) :displaced-to board))))) - (loop :for cart :in minecarts - :do (setf (row-major-aref copy (minecart-pos cart)) (minecart-direction cart)) - :finally - (format t "~{~a~%~}~%" - (loop :for y :below (array-dimension board 0) - :collect (coerce (make-array width :displaced-to copy :displaced-index-offset (* y width)) - 'string)))))) + :and :do (day13-check-collision! minecart minecarts))) (defun day13-pos->coord (board pos) (multiple-value-bind (y x) (floor pos (array-dimension board 1)) @@ -96,39 +82,30 @@ (defun day13 () (destructuring-bind (board carts) (day13-parse-input) - (format t "The first crash occurs at (~{~a~^,~}).~%" - (loop - :until (some #'minecart-crashed carts) - :do (day13-step! board carts) - :finally (return (day13-pos->coord - board - (minecart-pos (find-if #'minecart-crashed carts)))))) + (loop + :until (some #'minecart-crashed carts) + :do (day13-step! board carts) + :finally + (format t "The first crash occurs at (~{~a~^,~}).~%" + (day13-pos->coord board (minecart-pos (find-if #'minecart-crashed carts))))) (loop :for uncrashed := (remove-if #'minecart-crashed carts) :until (= 1 (length uncrashed)) :do (day13-step! board uncrashed) :finally - (progn - (format t "After the last crash the last minecart is at (~{~a~^,~}).~%" - (day13-pos->coord board (minecart-pos (first uncrashed)))) - (format t "~S~%~%" (first uncrashed)))) - carts)) - -(defparameter test-inp - (split-seq - "/->-\\ -| | /----\\ -| /-+--+-\\ | -| | | | v | -\\-+-/ \\-+--/ - \\------/ " #\Newline)) + (format t "After the last crash the last minecart is at (~{~a~^,~}).~%" + (day13-pos->coord board (minecart-pos (first uncrashed))))))) -(defparameter test-inp2 - (split-seq - "/>-<\\ -| | -| /<+-\\ -| | | v -\\>+/" #\Newline)) +;;helper - bit ugly +(defun day13-print-state (board minecarts) + (let* ((width (array-dimension board 1)) + (copy (make-array (array-dimensions board) + :displaced-to (copy-seq + (make-array (array-total-size board) :displaced-to board))))) + (loop :for cart :in minecarts + :do (setf (row-major-aref copy (minecart-pos cart)) (minecart-direction cart)) + :finally + (format t "~{~a~%~}~%" + (loop :for y :below (array-dimension board 0) + :collect (coerce (make-array width :displaced-to copy :displaced-index-offset (* y width)) + 'string)))))) diff --git a/day14.lisp b/day14.lisp index b6af4a8..1c80f3f 100644 --- a/day14.lisp +++ b/day14.lisp @@ -16,7 +16,7 @@ :for (rest digit) := (multiple-value-list (floor number 10)) :then (multiple-value-list (floor rest 10)) :collect digit - :while (> rest 0)))) + :while (> rest 0))))) (defun day14 (&optional (input 327901)) (loop :with recipes := (day14-prepare-recipes input) @@ -33,16 +33,16 @@ (defun day14-extra (&optional (input 327901)) (loop :with recipes := (day14-prepare-recipes 21000000) :with code := (day14-digits input) - :with at := 0 - :for elf1 := 0 :then (mod (+ elf1 1 recipe1) (length recipes)) - :for elf2 := 1 :then (mod (+ elf2 1 recipe2) (length recipes)) + :with at fixnum := 0 + :for elf1 fixnum := 0 :then (mod (+ elf1 1 recipe1) (length recipes)) + :for elf2 fixnum := 1 :then (mod (+ elf2 1 recipe2) (length recipes)) :for recipe1 := (aref recipes elf1) :for recipe2 := (aref recipes elf2) :do (dolist (new-recipe (day14-digits (+ recipe1 recipe2))) (vector-push-extend new-recipe recipes 10000) (cond ((null (nth at code)) (incf at)) - ((eql (nth at code) new-recipe) (incf at)) + ((= (nth at code) new-recipe) (incf at)) (t (setf at 0)))) :while (nth at code) :finally (format t "Answer Part2: ~a~%"