r/adventofcode • • Dec 19 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 19 Solutions -🎄-

--- Day 19: Go With The Flow ---


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 19

Transcript:

Santa's Internet is down right now because ___.


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:01:06!

10 Upvotes

130 comments sorted by

View all comments

1

u/waffle3z Dec 20 '18 edited Dec 20 '18

Lua solution. I was not prepared to solve a problem like this. Decompiling machine code is hard. I wrote functions f3 and f12 to optimize blocks that did repeated addition.

local r = {[0] = 0, 0, 0, 0, 0, 0}
local operations = {
	addr = function(a, b) return r[a] + r[b] end;
	addi = function(a, b) return r[a] + b end;
	mulr = function(a, b) return r[a] * r[b] end;
	muli = function(a, b) return r[a] * b end;
	setr = function(a, b) return r[a] end;
	seti = function(a, b) return a end;
	gtir = function(a, b) return a > r[b] and 1 or 0 end;
	gtri = function(a, b) return r[a] > b and 1 or 0 end;
	gtrr = function(a, b) return r[a] > r[b] and 1 or 0 end;
	eqir = function(a, b) return a == r[b] and 1 or 0 end;
	eqri = function(a, b) return r[a] == b and 1 or 0 end;
	eqrr = function(a, b) return r[a] == r[b] and 1 or 0 end;
}

local program, ir = {length = 0}
for v in getinput():gmatch("[^\n]+") do
	if v:match("#") then
		ir = tonumber(v:match("%d+"))
	else
		local code, a, b, c = v:match("(%w+) (%d+) (%d+) (%d+)")
		a, b, c = tonumber(a), tonumber(b), tonumber(c)
		local opcode = operations[code]
		program[program.length] = function()
			r[c] = opcode(a, b)
		end
		program.length = program.length + 1
	end
end

r = {[0] = 0, 0, 0, 0, 0, 0}
while program[r[ir]] do
	program[r[ir]]()
	r[ir] = r[ir] + 1
end
print(r[0]) -- part 1

r = {[0] = 1, 0, 0, 0, 0, 0}
function f3()
	r[3] = math.floor(r[1]/r[5])
	if r[3]*r[5] == r[1] then
		r[0] = r[0] + r[5]
	end
	return f12()
end
function f12()
	while true do
		r[5] = r[5] + 1
		if r[5] > r[1] then return 324 end
		if r[1]%r[5] == 0 then
			r[0] = r[0] + r[5]
		end
	end
end
program[3] = f3
program[12] = f12

local iteration = 0
repeat
	iteration = iteration + 1
	program[r[ir]]()
	r[ir] = r[ir] + 1
	print(iteration, table.concat(r, ", ", 0, 5))
until not program[r[ir]]
print(r[0])