Skip to content

Commit

Permalink
Fixed Day 13
Browse files Browse the repository at this point in the history
Welp sort is destructive - now I know.
Also very minor changes to day14.lisp
  • Loading branch information
abraemer committed Dec 14, 2018
1 parent 668ae72 commit c111204
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 54 deletions.
75 changes: 26 additions & 49 deletions day13.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -62,73 +62,50 @@
(#\+ (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))
(list x y)))

(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))))))
10 changes: 5 additions & 5 deletions day14.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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~%"
Expand Down

0 comments on commit c111204

Please sign in to comment.