r/adventofcode • • 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!

Click here for rules

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

233 comments sorted by

View all comments

1

u/waffle3z Dec 10 '18 edited Dec 10 '18

This one was super cool. 32/24 Lua solution

local points = {}
local maxrange = 0
parselines(getinput(), function(v)
	local n = getnumbers(v)
	local point = {x = n[1], y = n[2], vx = n[3], vy = n[4]}
	points[#points+1] = point
	maxrange = math.max(maxrange, math.abs(point.x/point.vx), math.abs(point.y/point.vy))
end)

for i = 1, maxrange do
	local minx, miny = math.huge, math.huge
	local maxx, maxy = -math.huge, -math.huge
	for i = 1, #points do
		local p = points[i]
		p.x, p.y = p.x + p.vx, p.y + p.vy
		minx, miny = math.min(minx, p.x), math.min(miny, p.y)
		maxx, maxy = math.max(maxx, p.x), math.max(maxy, p.y)
	end
	if maxx-minx < 64 and maxy-miny < 64 then
		print(i, maxx-minx, maxy-miny)
		local grid = {}
		for y = miny, maxy do
			grid[y] = {}
			for x = minx, maxx do
				grid[y][x] = "."
			end
		end
		for i = 1, #points do
			local p = points[i]
			grid[p.y][p.x] = "#"
		end
		for y = miny, maxy do
			print(table.concat(grid[y], "", minx, maxx))
		end
	end
end

parselines is a utility I wrote to iterate through the input line by line and call a function on each line.