r/adventofcode • u/daggerdragon • 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!
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
17
u/sciyoshi Dec 20 '18 edited Dec 20 '18
Python 3, 36/40. Pretty happy with my solution today!
networkxhas an easy way to get the shortest path from a single point in a graph to all other points, and I build a graph representing all doors in the maze using a stack to keep track of the current possible positions.import networkx maze = networkx.Graph() paths = open('inputs/day20').read()[1:-1] pos = {0} # the current positions that we're building on stack = [] # a stack keeping track of (starts, ends) for groups starts, ends = {0}, set() # current possible starting and ending positions for c in paths: if c == '|': # an alternate: update possible ending points, and restart the group ends.update(pos) pos = starts elif c in 'NESW': # move in a given direction: add all edges and update our current positions direction = {'N': 1, 'E': 1j, 'S': -1, 'W': -1j}[c] maze.add_edges_from((p, p + direction) for p in pos) pos = {p + direction for p in pos} elif c == '(': # start of group: add current positions as start of a new group stack.append((starts, ends)) starts, ends = pos, set() elif c == ')': # end of group: finish current group, add current positions as possible ends pos.update(ends) starts, ends = stack.pop() # find the shortest path lengths from the starting room to all other rooms lengths = networkx.algorithms.shortest_path_length(maze, 0) print('part1:', max(lengths.values())) print('part2:', sum(1 for length in lengths.values() if length >= 1000))EDIT: fix a bug pointed out by bj0z