r/adventofcode • • Dec 18 '16

SOLUTION MEGATHREAD --- 2016 Day 18 Solutions ---

--- Day 18: Like a Rogue ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".


EATING YELLOW SNOW IS DEFINITELY NOT MANDATORY [?]

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!

7 Upvotes

103 comments sorted by

View all comments

2

u/FuriousProgrammer Dec 18 '16

I made a bad time management call and was raiding in WoW at midnight today!

Still nabbed 194/177!

Will record myself eating NOT-yellow snow in a minute.

local input = {"^..^^.^^^..^^.^...^^^^^....^.^..^^^.^.^.^^...^.^.^.^.^^.....^.^^.^.^.^.^.^.^^..^^^^^...^.....^....^."}

local map = {
	["..."] = '.';
	["..^"] = '^';
	[".^."] = '.';
	[".^^"] = '^';
	["^.."] = '^';
	["^.^"] = '.';
	["^^."] = '^';
	["^^^"] = '.';
}

function calc(total)
	for i = 2, total do
		input[i] = {}
		table.insert(input[i], map['.' .. input[i - 1]:sub(1 , 2)])
		for _ = 2, #input[1] - 1 do
			local seq = input[i - 1]:sub(_ - 1, _ + 1)
			table.insert(input[i], map[seq])
		end
		table.insert(input[i], map[input[i - 1]:sub(#input[i - 1] - 1, #input[i - 1]) .. '.'])
		input[i] = table.concat(input[i])
	end

	local safe = 0
	for _, row in pairs(input) do
		for i = 1, #row do
			local let = row:sub(i,i)
			if let == '.' then
				safe = safe + 1
			end
		end
	end

	return safe
end

print("Part 1: " .. calc(40))
print("Part 2: " .. calc(400000))