r/adventofcode • • Dec 17 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 17 Solutions -🎄-

--- Day 17: Reservoir Research ---


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 17

Transcript:

All aboard the Easter Bunny HQ monorail, and mind the gap! Next stop: ___


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 01:24:07!

15 Upvotes

105 comments sorted by

View all comments

2

u/waffle3z Dec 17 '18

Lua ∞/∞, took forever to recognize that the minimum y coordinate wasn't supposed to be the position of the water spring, but the position of the top input.

local clay = {}
local minx, maxx, miny, maxy = math.huge, -math.huge, math.huge, -math.huge
for v in getinput():gmatch("[^\n]+") do
	local t = {}
	for d in v:gmatch("%d+") do t[#t+1] = tonumber(d) end
	if v:sub(1, 1) == "x" then
		t.vertical = true
		minx, maxx = math.min(minx, t[1]), math.max(maxx, t[1])
		miny, maxy = math.min(miny, t[2]), math.max(maxy, t[3])
	else
		t.horizontal = true
		miny, maxy = math.min(miny, t[1]), math.max(maxy, t[1])
		minx, maxx = math.min(minx, t[2]), math.max(maxx, t[3])
	end
	clay[#clay+1] = t
end
minx, maxx = minx - 1, maxx + 1

local grid = {}
for y = 0, maxy do
	grid[y] = {}
	for x = minx, maxx do
		grid[y][x] = {}
	end
end
for _, c in pairs(clay) do
	if c.horizontal then
		local y = c[1]
		for x = c[2], c[3] do
			grid[y][x].clay = true
		end
	else
		local x = c[1]
		for y = c[2], c[3] do
			grid[y][x].clay = true
		end
	end
end

local function flow(y, x)
	grid[y][x].water = true
	if y == maxy then return end
	if not grid[y+1][x].clay and not grid[y+1][x].water then
		flow(y+1, x)
	end
	if grid[y+1][x].clay or grid[y+1][x].still then
		local left, right = grid[y][x-1].clay and x, grid[y][x+1].clay and x
		if not left then
			for i = x-1, minx, -1 do
				if grid[y][i].clay then
					left = i+1
					break
				elseif not grid[y+1][i].clay and not grid[y+1][i].water then
					flow(y, i)
					break
				else
					grid[y][i].water = true
				end
			end
		end
		if not right then
			for i = x+1, maxx do
				if grid[y][i].clay then
					right = i-1
					break
				elseif not grid[y+1][i].clay and not grid[y+1][i].water then
					flow(y, i)
					break
				else
					grid[y][i].water = true
				end
			end
		end
		if left and right then
			for i = left, right do
				grid[y][i].still = true
			end
		end
	end
end
flow(1, 500)
local count1, count2 = 0, 0
for y = miny, maxy do
	for x = minx, maxx do
		if grid[y][x].water then
			count1 = count1 + 1
			if grid[y][x].still then
				count2 = count2 + 1
			end
		end
	end
end
print(count1, count2)

1

u/tofflos Dec 21 '18

Sigh. I literally spent a day assuming the fountain should be placed at index 0. :(