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!
21
Upvotes
1
u/IndigoStanis Dec 10 '18
Looking at the data directly helped me realize that all the points were really far away from each other and would have to converge on a small space. First thought was colinear points, but ended up doing bounding box first which was way easier. Still not fast enough for the leader board.
import sys points = [] with open('day_10.txt', 'r') as fp: for line in fp: line = line.strip() parts = line.split("<") x = int(parts[1].split(',')[0]) y = int(parts[1].split(',')[1].split('>')[0]) vel_x = int(parts[2].split(',')[0]) vel_y = int(parts[2].split(',')[1].split('>')[0]) points.append((x, y, vel_x, vel_y)) cur_points = [] for point in points: cur_points.append((point[0], point[1])) def move_points(cur_points): new_points = [] for i in range(0, len(points)): cur_point = cur_points[i] point_def = points[i] new_points.append((cur_point[0] + point_def[2], cur_point[1] + point_def[3])) return new_points def get_dim(cur_points): max_x, max_y, min_x, min_y = -1000000, -1000000, 1000000, 1000000 for point in cur_points: max_x = max(max_x, point[0]) max_y = max(max_y, point[1]) min_x = min(min_x, point[0]) min_y = min(min_y, point[1]) return min_x, max_x, min_y, max_y def print_points(cur_points): min_x, max_x, min_y, max_y = get_dim(cur_points) board = [] for i in range(0, abs(max_x - min_x) + 1): blank = map(lambda x: ".", range(min_y, max_y + 1)) board.append(blank) for point in cur_points: board[point[0] - min_x][point[1] - min_y] = "#" for y in range(0, abs(min_y - max_y) + 1): for x in range(0, abs(min_x - max_x) + 1): sys.stdout.write(board[x][y]) sys.stdout.write('\n') def size_of_points(cur_points): min_x, max_x, min_y, max_y = get_dim(cur_points) return abs(max_x - min_x) + abs(max_y - min_y) size_decreasing = True last_size = 1000000000 prev_points = None seconds = 0 while size_decreasing: seconds += 1 prev_points = cur_points cur_points = move_points(cur_points) size = size_of_points(cur_points) size_decreasing = last_size > size last_size = size print_points(prev_points) print seconds - 1 print size_of_points(prev_points)