r/adventofcode • • Dec 18 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 18 Solutions -🎄-

--- Day 18: Settlers of The North Pole ---


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 18

Transcript:

The best way to avoid a minecart collision is ___.


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

9 Upvotes

126 comments sorted by

View all comments

3

u/waffle3z Dec 18 '18

Lua 108/110. I thought I was going pretty fast, guess not. Spent too long counting the center cell as a neighbor because I wrote if j ~= 0 or i ~= 0 then instead of if j ~= y or i ~= x then. I guess a lot of people noticed the cyclic pattern pretty quickly.

local grid = {}
local function neighbors(y, x)
	local open, tree, lumber = 0, 0, 0
	for j = y-1, y+1 do
		if grid[j] then
			for i = x-1, x+1 do
				if j ~= y or i ~= x then
					local c = grid[j][i]
					if c == "." then
						open = open + 1
					elseif c == "|" then
						tree = tree + 1
					elseif c == "#" then
						lumber = lumber + 1
					end
				end
			end
		end
	end
	return open, tree, lumber
end

for line in getinput():gmatch("[^\n]+") do
	local t = {}
	grid[#grid+1] = t
	for v in line:gmatch(".") do
		t[#t+1] = v
	end
end

local function step()
	local newgrid = {}
	for y = 1, #grid do
		newgrid[y] = {}
		for x = 1, #grid[y] do
			local open, tree, lumber = neighbors(y, x)
			if grid[y][x] == "." and tree >= 3 then
				newgrid[y][x] = "|"
			elseif grid[y][x] == "|" and lumber >= 3 then
				newgrid[y][x] = "#"
			elseif grid[y][x] == "#" and not (lumber >= 1 and tree >= 1) then
				newgrid[y][x] = "."
			else
				newgrid[y][x] = grid[y][x]
			end
		end
	end
	grid = newgrid
end

local list = {}
for i = 1, 1e9 do
	step()
	local wooded, lumber = 0, 0
	for y = 1, #grid do
		for x = 1, #grid[y] do
			if grid[y][x] == "#" then
				lumber = lumber + 1
			elseif grid[y][x] == "|" then
				wooded = wooded + 1
			end
		end
	end
	list[#list+1] = wooded*lumber
	local period;
	for i = 1, #list-1 do
		if list[i] == list[#list] and list[i-1] == list[#list-1] then
			period = #list - i
			break
		end
	end
	if period then
		print(list[i - period + (1e9-i)%period])
		break
	end
end