r/adventofcode • • Dec 13 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 13 Solutions -🎄-

--- Day 13: Mine Cart Madness ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 13

Transcript:

Elven chronomancy: for when you absolutely, positively have to ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:44:25!

24 Upvotes

148 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Dec 13 '18 edited Dec 13 '18

Complex numbers and multiple dispatch, sweet, like that. Mine's less slick, brutal case galore. Will try to come up with a nice generic solution over the next couple of days.

(defstruct cart x y dir opt)

(defun parse-input (grid)
  (with-open-file (in "13.input")
    (loop with carts
          for line = (read-line in nil) while line
          for y from 0 do
            (loop for char across line
                  for x from 0 do
                    (case char
                      (#\^ (setf (aref grid x y) #\|) (push (make-cart :x x :y y :dir 'north) carts))
                      (#\v (setf (aref grid x y) #\|) (push (make-cart :x x :y y :dir 'south) carts))
                      (#\< (setf (aref grid x y) #\-) (push (make-cart :x x :y y :dir 'west) carts))
                      (#\> (setf (aref grid x y) #\-) (push (make-cart :x x :y y :dir 'east) carts))
                      (t   (setf (aref grid x y) char))))
          finally (return (reverse carts)))))

(defun move (cart)
  (with-slots (x y dir) cart
    (ecase dir (north (decf y)) (west (decf x)) (south (incf y)) (east (incf x)))))

(defun collision? (c1 carts)
  (loop for c2 in carts
        when (and (not (eq c1 c2))
                  (= (cart-x c1) (cart-x c2))
                  (= (cart-y c1) (cart-y c2)))
          return (list c1 c2)))

(defun turn (cart grid)
  (with-slots (x y dir opt) cart
    (setf dir (case (aref grid x y)
                (#\\ (ecase dir (north 'west) (west 'north) (south 'east) (east 'south)))
                (#\/ (ecase dir (north 'east) (west 'south) (south 'west) (east 'north)))
                (#\+ (ecase opt
                       (left (setf opt 'straight) dir)
                       (straight (setf opt 'right)
                        (ecase dir (north 'east) (west 'north) (south 'west) (east 'south)))
                       ((right nil) (setf opt 'left)
                        (ecase dir (north 'west) (west 'south) (south 'east) (east 'north)))))
                (otherwise dir)))))

(defun main ()
  (let* ((grid (make-array '(150 150) :initial-element nil))
         (carts (parse-input grid))
         (initial-collision t))
    (loop until (= 1 (length carts)) do
      (loop for cart in carts do
        (move cart)
        (let ((coll (collision? cart carts)))
          (if coll
              (progn (setf carts (remove-if (lambda (cx) (member cx coll)) carts))
                     (when initial-collision
                       (format t "Result 13a: ~d,~d~%" (cart-x cart) (cart-y cart))
                       (setf initial-collision nil)))
              (turn cart grid)))))
    (with-slots (x y) (first carts)
      (format t "Result 13b: ~d,~d~%" x y))))

;; CL-USER> (time(main))
;; Result 13a: 39,52
;; Result 13b: 133,146
;; Evaluation took:
;;   0.041 seconds of real time
;;   0.040631 seconds of total run time (0.040631 user, 0.000000 system)
;;   100.00% CPU
;;   89,267,178 processor cycles
;;   309,648 bytes consed