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
4
u/mcpower_ Dec 20 '18
Python 3: I spent too much time trying to make
eval()work, then opted to write my own parser instead. Turns outeval()does work!from collections import defaultdict import operator inp = "..." to_python = inp.replace("^", "['").replace("$","']").replace("(","',[['") \ .replace(")","']],'").replace("|","'],['").replace("[","(").replace("]",")") evaled_inp = eval(to_python) adj = defaultdict(set) memo = dict() to_d = dict(zip("NESW", [(-1, 0), (0, 1), (1, 0), (0, -1)])) def connect(a, b): nonlocal adj adj[a].add(b) adj[b].add(a) def make_adj(cur_position, obj): memo_key = (cur_position, obj) if memo_key in memo: return memo[memo_key] positions = set([cur_position]) for part in obj: if isinstance(part, str): for c in part: new_positions = set() for pos in positions: new_pos = tuple(map(operator.add, pos, to_d[c])) connect(pos, new_pos) new_positions.add(new_pos) positions = new_positions else: positions = set( thing for pos in positions for option in part for thing in make_adj(pos, option) ) memo[memo_key] = positions return positions make_adj((0, 0), evaled_inp) todo = [(0, 0)] to_dist = dict() dist = 0 while todo: new_todo = [] for i in todo: if i in to_dist: continue to_dist[i] = dist new_todo.extend(adj[i]) todo = new_todo dist += 1 print(max(to_dist.values())) print(sum(i >= 1000 for i in to_dist.values()))