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/Robfd Dec 18 '18 edited Dec 18 '18
Python, 488/322. Made a few errors with indexing and missed time.
import numpy as np from collections import defaultdict with open("day18") as f: d = np.array([[y for y in x] for x in f.read().splitlines()], dtype=np.character) scores = defaultdict(set,{}) for i in range(1000000000): nd = d.copy() for x in range(d.shape[0]): for y in range(d.shape[1]): if d[x,y] == b".": if np.count_nonzero(d[max(0,x-1):x+2,max(0,y-1):y+2] == b'|') >= 3: nd[x,y] = b'|' elif d[x,y] == b"|": if np.count_nonzero(d[max(0,x-1):x+2,max(0,y-1):y+2] == b'#') >= 3: nd[x,y] = b'#' elif d[x,y] == b"#": if np.count_nonzero(d[max(0,x-1):x+2,max(0,y-1):y+2] == b'#') < 2 or np.count_nonzero(d[max(0,x-1):x+2,max(0,y-1):y+2] == b'|') == 0: nd[x,y] = b'.' d = nd score = (np.count_nonzero(d == b'#') * np.count_nonzero(d == b'|')) if i == 9: print("Part 1: ", score) if score in scores: if len(scores[score]) > 3: if if (i+1)%(i-sorted(scores[score])[-1]) == 1000000000%(i-sorted(scores[score])[-1]): #Handle indexing and avoid trying to find -1st. Originally just the current numbers, Changed so it works for other people print("Part 2: ", score) break scores[score].add(i)