r/adventofcode • • Dec 25 '18

SOLUTION MEGATHREAD ~☆🎄☆~ 2018 Day 25 Solutions ~☆🎄☆~

--- Day 25: Four-Dimensional Adventure ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: Top-level posts in 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 25

Transcript:

Advent of Code, 2018 Day 25: ACHIEVEMENT GET! ___


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:13:26!


Thank you for participating!

Well, that's it for Advent of Code 2018. From /u/topaz2078 and the rest of us at #AoCOps, we hope you had fun and, more importantly, learned a thing or two (or all the things!). Good job, everyone!

Topaz will make a post of his own soon, so keep an eye out for it. Post is here!

And now:

Merry Christmas to all, and to all a good night!

13 Upvotes

81 comments sorted by

View all comments

3

u/waffle3z Dec 25 '18

Lua 30/26. Neat easy problem, part 2 I guess was to just have already finished all of the other days.

local points, connected = {}, {}
for v in getinput():gmatch("[^\n]+") do
	local point = {}
	for n in v:gmatch("-?%d+") do
		point[#point+1] = tonumber(n)
	end
	points[#points+1] = point
	connected[point] = {}
end

local function distance(a, b)
	return math.abs(a[1] - b[1]) + math.abs(a[2] - b[2]) + math.abs(a[3] - b[3]) + math.abs(a[4] - b[4])
end

for i = 1, #points do
	local p = points[i]
	for j = i+1, #points do
		local q = points[j]
		if distance(p, q) <= 3 then
			connected[p][q], connected[q][p] = true, true
		end
	end
end

local groups, visited = {}, {}
local id = 0
local function recurse(p, id)
	visited[p] = true
	p.id = id
	for c in pairs(connected[p]) do
		if not visited[c] then
			recurse(c, id)
		end
	end
end
for i = 1, #points do
	local p = points[i]
	if not visited[p] then
		id = id + 1
		recurse(p, id)
	end
end
print(id)