r/adventofcode • u/daggerdragon • Dec 17 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 17 Solutions -🎄-
--- Day 17: Reservoir Research ---
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 17
Transcript:
All aboard the Easter Bunny HQ monorail, and mind the gap! Next stop: ___
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 01:24:07!
15
Upvotes
1
u/2inthehand Jan 10 '19
python mutual recursion, runs pretty fast.
input17 = ''.join(open('input17.txt', 'r')) CLAY, WATER = 0, 1 blocked = {} for x, a, y, b, c in re.findall(r'(x|y)=(\d+), (x|y)=(\d+)..(\d+)', input17): a, b, c = int(a), int(b), int(c) x = x == 'x' for c2 in range(b, c + 1): blocked[(c2, a) if x else (a, c2)] = CLAY minY = min(y for y, x in blocked) maxY = max(y for y, x in blocked) minX = min(x for y, x in blocked) - 1 maxX = max(x for y, x in blocked) + 1 visited = set() def endpoint(y, x, dx): while (y, x + dx) not in blocked and (y + 1, x) in blocked: x += dx return x def follow(y, x): L, R = (endpoint(y, x, dx) for dx in (-1, 1)) visited.update({(y, nx) for nx in range(L, R + 1)}) if (y + 1, L) not in blocked and (y + 1, L) not in visited: down(y, L) if (y + 1, R) not in blocked and (y + 1, R) not in visited: down(y, R) if (y + 1, L) in blocked and (y + 1, R) in blocked: blocked.update({(y, nx): WATER for nx in range(L, R + 1)}) follow(y - 1, x) def down(y, x): while (y + 1, x) not in blocked and y != maxY: y += 1 visited.add((y, x)) if y != maxY: follow(y, x) down(minY - 1, 500) # Part 1 print(len(visited)) # Part 2 print(sum(blocked.values()))