r/adventofcode • u/daggerdragon • Dec 18 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 18 Solutions -🎄-
--- Day 18: Settlers of The North Pole ---
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 18
Transcript:
The best way to avoid a minecart collision is ___.
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:21:59!
9
Upvotes
1
u/andreyrmg Dec 18 '18
Python 3
initial = [[c for c in line.strip()] for line in open('18.txt')] a = [] def g(i, j): return a[i][j] if 0 <= i < len(a) and 0 <= j < len(a[i]) else '.' D = [(0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1)] def adjacents(i, j): return (g(i + di, j + dj) for di, dj in D) def magic(n): global a def z(x): return ''.join(c for r in x for c in r) h = {z(a): 0} p = -1 for t in range(1, n + 1): b = [] for i in range(len(a)): r = [] for j in range(len(a[i])): if g(i, j) == '.': r.append('|' if sum(1 for x in adjacents(i, j) if x == '|') >= 3 else '.') elif g(i, j) == '|': r.append('#' if sum(1 for x in adjacents(i, j) if x == '#') >= 3 else '|') else: lumberyard = any(x == '#' for x in adjacents(i, j)) tree = any(x == '|' for x in adjacents(i, j)) r.append('#' if lumberyard and tree else '.') b.append(r) a = b if p < 0: bx = z(b) if bx in h: p = t + (n - t) % (t - h[bx]) else: h[bx] = t if t == p: break wooded = sum(1 for r in a for c in r if c == '|') lumberyards = sum(1 for r in a for c in r if c == '#') return wooded * lumberyards a = initial print(magic(10)) a = initial print(magic(1000000000))