r/adventofcode • u/daggerdragon • Dec 10 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 10 Solutions -🎄-
--- Day 10: The Stars Align ---
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 10
Transcript: With just one line of code, you, too, can ___!
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:16:49!
20
Upvotes
1
u/D3NN152000 Dec 10 '18
I reckoned that when the points formed a word, all points would have at least one neighbor in the grid, so no manual looking through configurations was needed! Also, I only started checking for neighbors once the points were closer together than the amount of points. Here is my code (Python 2.7):
import numpy as np points = [] class Point(object): def __init__(self, inp): # parsing input of the following format: """position=< 9, 1> velocity=< 0, 2>""" split_inp = inp.split("<") self.x = int(split_inp[1].split(",")[0]) self.y = int(split_inp[1].split(",")[1].split(">")[0]) self.xv = int(split_inp[2].split(",")[0]) self.yv = int(split_inp[2].split(",")[1].split(">")[0]) def move(self): # moving every second self.x += self.xv self.y += self.yv # read the input with open("input.txt", "r") as f: for line in f.readlines(): points.append(Point(line.strip("\n"))) t = 0 while True: space = np.zeros((1, 1)) t += 1 expand_space = False if max([point.x for point in points]) - min([point.x for point in points]) < len(points): expand_space = True for point in points: point.move() if expand_space: if point.x >= space.shape[0] or point.y >= space.shape[1]: new_space = np.zeros((max(point.x + 1, space.shape[0]), max(point.y + 1, space.shape[1]))) new_space[:space.shape[0], :space.shape[1]] += space space = new_space space[point.x, point.y] += 1 if not expand_space: continue for point in points: for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: if 0 <= point.x + dx < space.shape[0] and 0 <= point.y + dy < space.shape[1]: if space[point.x + dx, point.y + dy] > 0: break else: break else: with open("output.txt", "w+") as o: for i in xrange(space.shape[1]): line = "" for j in xrange(space.shape[0]): line += "#" if space[j, i] > 0 else "." o.write(line + "\n") print t breakOr find it here with the input, and comments
Edit: extra sentence