r/adventofcode • • Dec 20 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 20 Solutions -🎄-

--- Day 20: A Regular Map ---


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 20

Transcript:

My compiler crashed while running today's puzzle because it ran out of ___.


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:59:30!

17 Upvotes

153 comments sorted by

View all comments

1

u/waffle3z Dec 21 '18

Lua solution, don't know why it took so long to get it working right. 1% solving, 99% debugging

local rooms = {}
local function move(s, y, x)
	for i = 1, #s do
		local c = s:sub(i, i)
		rooms[y] = rooms[y] or {}
		rooms[y][x] = rooms[y][x] or {}
		rooms[y][x][c] = true
		y = y + (c == "S" and -1 or c == "N" and 1 or 0)
		x = x + (c == "W" and -1 or c == "E" and 1 or 0)
	end
	return y, x
end

local function parse(s, y, x)
	local p = 0
	local front, new = s:match("^(%w*)(.*)")
	y, x = move(front, y, x)
	if #new == 0 then return end
	local groups = {}
	local start, after = 2
	for i = 1, #new do
		local c = new:sub(i, i)
		if c == "(" then
			p = p + 1
		elseif c == ")" then
			p = p - 1
			if p == 0 then
				after = i
				break
			end
		elseif c == "|" and p == 1 then
			groups[#groups+1] = new:sub(start, i-1)
			start = i+1
		end
	end
	groups[#groups+1] = new:sub(start, after-1)
	local has0 = false
	for i = 1, #groups do if #groups[i] == 0 then has0 = true end end
	for i = 1, #groups do
		local a, b = groups[i], new:sub(after+1)
		if has0 and #a ~= 0 then
			local py, px = move(a, y, x)
			if py ~= y or px ~= x then
				parse(a..b, y, x)
			else
				parse(a, y, x)
			end
		else
			parse(a..b, y, x)
		end
	end
end
parse(getinput():sub(2, -2), 0, 0)

local maxdist, count = 0, 0
function traverse(y, x, dist)
	if not rooms[y] then rooms[y] = {} end
	if not rooms[y][x] then rooms[y][x] = {} end
	if rooms[y][x].visited then return end
	if dist >= 1000 then count = count + 1 end
	rooms[y][x].visited = true
	if dist > maxdist then maxdist = dist end
	for c, _ in pairs(rooms[y][x]) do
		local dy = c == "N" and 1 or c == "S" and -1 or 0
		local dx = c == "E" and 1 or c == "W" and -1 or 0
		traverse(y + dy, x + dx, dist + 1)
	end
end
traverse(0, 0, 0)
print(maxdist, count)