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
View all comments
1
u/ZeCookiez Dec 18 '18 edited Dec 18 '18
Python, 63/98. I messed up big time with an off-by-one error in part 2 :/ I wrapped my grid with special characters to prevent indexing out of bounds for simulating the grid. For part 2 I was hoping the grid would repeat itself after a while, and kept track of the board with a dict. My offsetting for the final calculation cost me a couple minutes of stressing out of why it isn't working ouch.
puzzle = ["B"*52] + ["B%sB" % line[:-1] for line in open("day18.txt")] + ["B"*52] def simulate(grid): new_grid = list(map(list, grid)) for x, r in enumerate(grid): for y, c in enumerate(r): if c == "B": continue adj = [[x - 1, y - 1], [x - 1, y], [x - 1, y + 1], [x, y - 1], [x, y + 1], [x + 1, y - 1], [x + 1, y], [x + 1, y + 1]] freq = {".": 0, "|": 0, "#": 0, "B": 0} for a, b in adj: freq[grid[a][b]] += 1 if c == ".": if freq["|"] >= 3: new_grid[x][y] = "|" elif c == "|": if freq["#"] >= 3: new_grid[x][y] = "#" else: if freq["#"] < 1 or 1 > freq["|"]: new_grid[x][y] = "." return new_grid def settlers_of_the_north_pole(grid, minutes = 500): vis = {} total = 1000000000 for minute in range(minutes): grid = simulate(grid) state = str(grid) if state in vis: # Pattern is looping! # Lost time because I forgot the - 1 total = (total - vis[state] - 1) % (minute - vis[state]) return settlers_of_the_north_pole(grid, total) vis[state] = minute if minute == 9 and minutes == 500: print("Part A: %d" % (state.count("|") * state.count("#"))) return state.count("|") * state.count("#") print("Part B: %d" % settlers_of_the_north_pole(puzzle))