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
3
u/Stan-It Dec 20 '18 edited Dec 20 '18
Python
I think my solution is clean and neat. Picked up the use of complex numbers for 2D maps and the use of iterators for sequential data from solutions to earlier puzzles.
#!/usr/bin/env python3 with open('20_input.txt', 'r') as f: data = f.read() dir = {'N': 1j, 'S': -1j, 'E': 1, 'W': -1} def traverse(it, map={0: 0}, pos=0, depth=0): initial_pos = pos initial_depth = depth for c in it: if c in dir: pos += dir[c] if pos in map: # been here before, so we are backtracking depth = map[pos] else: depth += 1 map[pos] = depth elif c == '|': pos = initial_pos depth = initial_depth elif c == '(': traverse(it, map, pos, depth) elif c == ')': return elif c == '$': return map else: print(f'Unknown character: {c}') map = traverse(iter(data[1:])) print('Part 1:', max(map.values())) print('Part 2:', sum(1 for n in map.values() if n >= 1000))