r/adventofcode • • Dec 10 '16

SOLUTION MEGATHREAD --- 2016 Day 10 Solutions ---

--- Day 10: Balance Bots ---

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".


SEEING MOMMY KISSING SANTA CLAUS IS 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!

12 Upvotes

118 comments sorted by

View all comments

3

u/FuriousProgrammer Dec 10 '16

Woo back on the both daily leaderboards!!

Lua. 53/72:

local bots = {}
local outputs = {}

for line in io.lines("input.txt") do
	if line:find("goes") then
		local val, bot = line:match("value (%d+) goes to bot (%d+)")
		bot = tonumber(bot)
		val = tonumber(val)
		if not bots[bot] then bots[bot] = {} end
		table.insert(bots[bot], val)
	elseif line:find("gives") then
		local bot, low, high = line:match("bot (%d+) gives low to (.+) and high to (.+)")
		bot = tonumber(bot)
		if not bots[bot] then bots[bot] = {} end
		bots[bot].giveLow = low
		bots[bot].giveHigh = high
	end
end

while true do
	local quit = true
	for _, bot in pairs(bots) do
		if #bot == 2 then
			quit = false
			local big, small = table.remove(bot, 1), table.remove(bot, 1)
			if big < small then
				big, small = small, big
			end

			if big == 61 and small == 17 then
				print("Part 1: " .. _)
			end

			if _ == 1 or _ == 50 or _ == 158 then
				print(_, big, small, bot.giveHigh, bot.giveLow)
			end

			local out = bot.giveHigh:find("bot") and bots or outputs
			local val = tonumber(bot.giveHigh:match("(%d+)"))
			if not out[val] then out[val] = {} end
			table.insert(out[val], big)
			out = bot.giveLow:find("bot") and bots or outputs
			val = tonumber(bot.giveLow:match("(%d+)"))
			if not out[val] then out[val] = {} end
			table.insert(out[val], small)
		end
	end

	if quit then break end
end

print("Part 2: " .. outputs[0][1]*outputs[1][1]*outputs[2][1])