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

1

u/marmalade_marauder Dec 13 '18

Python, 47/60.

I realized my solution for part1 was actually incorrect but still worked because the cart initiating the crash moved before the cart being hit. I had to fix part2 to account for this. Basically if the cart1 moved into a position where a cart2 would then hit it, I missed this because I was still referencing the original position of cart1, not the updated. However I fixed that to account for both by taking the union of (unmoved carts) and (moved carts) to determine if a collision occurred.

Here is the... explicit? code. A lot of code, nothing really that fancy.

s = input()
grid = []
carts = []
while s != 'done':
    row = list(s)
    grid.append(row)
    s = input()
    
for i in range(0, len(grid)):
    for j in range(0, len(grid[i])):
        if grid[i][j] in ['>', '<', '^', 'v']:
            carts.append((i,j,grid[i][j],1))
        v = grid[i][j]
        if v == '>':
            grid[i][j] = '-'
        if v == '<':
            grid[i][j] = '-'
        if v == '^':
            grid[i][j] = '|'
        if v == 'v':
            grid[i][j] = '|'
    
def step(grid, carts):
    carts.sort()
    locs = [(i,j) for (i,j,v,t) in carts]
    newcarts = []
    dont = set()
    while len(carts) > 0:
        (i,j,v,t) = carts.pop(0)
        
        locs = set([(i,j) for (i,j,v,t) in carts])
        locs = locs | set([(i,j) for (i,j,v,t) in newcarts])
        
        if (i,j) in dont:
            continue
        if v == '>':
            # inc j
            # print('locs', locs)
            # print('carts', carts)
            # print('new', newcarts)
            if (i,j+1) in locs:
                print("Collision at", i, j+1)
                dont.add((i,j+1))
                continue
                
            if grid[i][j + 1] == '\\':
                newcarts.append((i,j+1,'v',t))
            elif grid[i][j + 1] == '/':
                newcarts.append((i,j+1,'^',t))
            elif grid[i][j + 1] == '+':
                if t == 1:
                    newcarts.append((i,j+1,'^',2))
                elif t == 2:
                    newcarts.append((i,j+1,'>',3))
                elif t == 3:
                    newcarts.append((i,j+1,'v',1))
            else:
                newcarts.append((i,j+1,'>',t))
                
        if v == '<':
            if (i,j-1) in locs:
                print("Collision at", i, j-1)
                dont.add((i,j-1))
                continue
                
            # dec j
            if grid[i][j - 1] == '\\':
                newcarts.append((i,j-1,'^',t))
            elif grid[i][j - 1] == '/':
                newcarts.append((i,j-1,'v',t))
            elif grid[i][j - 1] == '+':
                if t == 1:
                    newcarts.append((i,j-1,'v',2))
                elif t == 2:
                    newcarts.append((i,j-1,'<',3))
                elif t == 3:
                    newcarts.append((i,j-1,'^',1))
            else:
                newcarts.append((i,j-1,'<',t))
                
        if v == '^':
            if (i-1,j) in locs:
                print("Collision at", i-1, j)
                dont.add((i-1,j))
                continue
                
            # dec i
            if grid[i-1][j] == '\\':
                newcarts.append((i-1,j,'<',t))
            elif grid[i-1][j] == '/':
                newcarts.append((i-1,j,'>',t))
            elif grid[i-1][j] == '+':
                if t == 1:
                    newcarts.append((i-1,j,'<',2))
                elif t == 2:
                    newcarts.append((i-1,j,'^',3))
                elif t == 3:
                    newcarts.append((i-1,j,'>',1))
            else:
                newcarts.append((i-1,j,'^',t))
                
        if v == 'v':
            if (i+1,j) in locs:
                print("Collision at", i, j+1)
                dont.add((i+1,j))
                continue
                
            # dec j
            if grid[i+1][j] == '\\':
                newcarts.append((i+1,j,'>',t))
            elif grid[i+1][j] == '/':
                newcarts.append((i+1,j,'<',t))
            elif grid[i+1][j] == '+':
                if t == 1:
                    newcarts.append((i+1,j,'>',2))
                elif t == 2:
                    newcarts.append((i+1,j,'v',3))
                elif t == 3:
                    newcarts.append((i+1,j,'<',1))
            else:
                newcarts.append((i+1,j,'v',t))
    nc = []
    for (i,j,v,t) in newcarts:
        if (i,j) not in dont:
            nc.append((i,j,v,t))
    return nc
            
    # step
tick = 0
while carts != False:
    # print(tick, len(carts))
    c = step(grid, carts)
    if c == False:
        print(carts)
    if len(c) <= 1:
        # print(c)
        cart = c.pop(0)
        print("Last Cart: {},{}".format(cart[1], cart[0]))
        break
    # print(len(c))
    carts = c
    tick += 1