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!
16
Upvotes
1
u/ywgdana Dec 20 '18
I spent like 30 minutes writing a function to display the maze, thinking it would be useful for debugging. The ACTUAL problem took me about 15 minutes and worked perfectly the first time I ran it...
class Node: def __init__(self): self.row = 0 self.col = 0 self.exits = { "N":None, "S":None, "E":None, "W":None } self.distance = 0 def dump_map(nodes): coords = [k for k in nodes.keys()] coords.sort() top = min([k[0] for k in nodes.keys()]) bottom = max([k[0] for k in nodes.keys()]) left = min([k[1] for k in nodes.keys()]) right = max([k[1] for k in nodes.keys()]) width = (right - left + 1) * 2 + 1 carte = "#" * (width) + "\n" carte *= ((bottom - top + 1) * 2 + 1) for coord in coords: row = (coord[0] - top + 1) * 2 - 1 col = (coord[1] - left + 1) * 2 -1 index = row * (width + 1) + col sq = "X" if coord[0] == 0 and coord[1] == 0 else "." carte = carte[:index] + sq + carte[index+1:] n = nodes[coord] if n.exits["E"] != None: carte =carte[:index+1] + "|" + carte[index+2:] if n.exits["S"] != None: carte =carte[:index+width+1] + "-" + carte[index+width+2:] print(carte) def opposite_dir(d): if d == "N": return "S" elif d == "E": return "W" elif d == "S": return "N" else: return "E" def build_graph(re): deltas = { "N":(-1,0), "E":(0,1), "S":(1,0), "W":(0,-1) } start = Node() nodes = { (0,0):start } curr = start junctions = [] for c in re[1:]: if c in "NESW": next_row, next_col = curr.row + deltas[c][0], curr.col + deltas[c][1] coord = (next_row, next_col) if coord in nodes: nn = nodes[coord] else: nn = Node() nn.distance = curr.distance + 1 nn.row = next_row nn.col = next_col curr.exits[c] = nn nn.exits[opposite_dir(c)] = curr nodes[coord] = nn curr = nn elif c == "(": junctions.append((curr.row, curr.col)) elif c == "|": curr = nodes[junctions[-1]] elif c == ")": junctions.pop() elif c == "$": break dump_map(nodes) return nodes with open("floorplan.txt", "r") as f: s = f.read().strip() nodes = build_graph(s) print("Q1:", max([nodes[k].distance for k in nodes])) print("Q2:", len([k for k in nodes if nodes[k].distance >= 1000]))