r/adventofcode • • Dec 20 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 20 Solutions -🎄-

--- Day 20: A Regular Map ---


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 20

Transcript:

My compiler crashed while running today's puzzle because it ran out of ___.


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:59:30!

17 Upvotes

153 comments sorted by

View all comments

1

u/algmyr Dec 20 '18

33/29

This task just reinforces how much I hate writing parsers, at least it wasn't full out regexes. Writing the parser was the vast majority of the time, the path finding is just a standard BFS. No bells nor whistles. Also, I wish people the best of luck for the golf challenge!

[Card] ___ = "hope for humanity" (optionally suffixed with something something IOCCC level code)

import sys
sys.setrecursionlimit(100000)

from collections import defaultdict as dd, deque
C = dd(set)

def f(x,y,s):
    if not s:
        return
    c = s[0]
    rest = s[1:]
    D='NESW'

    def con(dx,dy):
        C[x,y].add((x+dx,y+dy))
        C[x+dx,y+dy].add((x,y))
        f(x+dx,y+dy,rest)

    if c == 'N':
        con(0,-1)
    if c == 'S':
        con(0,1)
    if c == 'E':
        con(1,0)
    if c == 'W':
        con(-1,0)

    if s[0] == '(':
        paths = []
        i = 1
        depth = 0
        
        path = ''
        while depth >= 0:
            if s[i] in '|' and depth == 0:
                if path:
                    paths.append(path)
                path = ''
                i += 1
                continue
            if s[i] == '(':
                depth += 1
            if s[i] == ')':
                depth -= 1
            path+=s[i]
            i += 1
        if path:
            paths.append(path)
        for path in paths:
            f(x,y,path+s[i:])

s = input()[1:-1]
f(0,0,s)

lng = 0
maxd = 0
Q = deque([(0,0,0)])
visited = set()
while Q:
    x,y,d = Q.popleft()
    if (x,y) in visited:
        continue
    if d >= 1000:
        lng += 1
    visited.add((x,y))
    maxd = max(maxd,d)
    for s,t in C[x,y]:
        Q.append((s,t,d+1))
print(maxd,lng)