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/sbjf Dec 13 '18 edited Dec 13 '18

Just got around to doing mine now. Using python's native support for complex numbers is a great idea, shame I forgot about it :P

import numpy as np
import heapq
from itertools import cycle

m = np.array([list(x) for x in input.split("\n")])
dirs = [(0, -1), (1, 0), (0, 1), (-1, 0)]
w,s,e,n = range(len(dirs))
dir_d = {'<': w, '>': e, '^': n, 'v': s}
carts = {}
cartheap = []
for c, d in dir_d.items():
    for loc in np.argwhere(m == c):
        loc = tuple(loc)
        carts[loc] = d, cycle(['left', 'straight', 'right'])
        heapq.heappush(cartheap, loc)
        m[loc] = '-' if c in '<>' else '|'

stop = False
while not stop:
    moved_cartheap = []
    while cartheap:
        loc = heapq.heappop(cartheap)
        cart = carts.pop(loc)
        diridx, turncycler = cart
        loc = loc[0]+dirs[diridx][0], loc[1]+dirs[diridx][1]
        
        if loc in carts:
            print('collision!', loc, tick)
            del carts[loc]
            try:
                cartheap.remove(loc)
            except ValueError:
                moved_cartheap.remove(loc)
            if len(carts) < 2:
                print('part 2', carts)
                stop = True
            continue
        
        track = m[loc]
        if track in '/':
            diridx = {e:n, w:s, n:e, s:w}[diridx]
        elif track == '\\':
            diridx = {e:s, w:n, n:w, s:e}[diridx]
        elif track == '+':
            turndir = next(turncycler)
            if turndir == 'left':
                diridx = (diridx+1)%4
            elif turndir == 'right':
                diridx = (diridx-1)%4

        carts[loc] = diridx, turncycler
        heapq.heappush(moved_cartheap, loc)
        
    cartheap = moved_cartheap

But this way I can use heapq, which wouldn't be possible with complex numbers

Edit: Here's my new and improved version inspired by your use of complex numbers :)

from itertools import cycle

m = {}; carts = {}; dir_d = {'<': -1, '>': 1, '^': 1j, 'v': -1j}
for i, row in enumerate(input.split("\n")):
    for j, c in enumerate(row):
        loc = j-i*1j
        if c in r'/\+':
            m[loc] = c
        elif c in dir_d:
            carts[loc] = dir_d[c], cycle([1j, 1, -1j])

while len(carts) > 1:
    for loc in sorted(carts, key=lambda x: (-x.imag, x.real)):
        if loc not in carts:
            continue  # deleted due to collision
        dxn, turn = carts.pop(loc)  # take out cart
        loc += dxn  # update position

        if loc in carts:  # handle collision
            print('collision!', loc, '(cart', loc - dxn)
            del carts[loc]
            continue

        track = m.get(loc)  # update direction
        if track == '+':
            dxn = dxn * next(turn)
        elif track is not None: #/ or \
            dxn *= 1j * (2*((track == '/') ^ (dxn.real == 0))-1)
            
        carts[loc] = dxn, turn  # put cart back onto tracks

print(carts)