day 24 part 1 but too slow
This commit is contained in:
parent
60ed6981b8
commit
0b6b3ede21
120
day24/part1.lua
Executable file
120
day24/part1.lua
Executable file
@ -0,0 +1,120 @@
|
||||
#!/usr/bin/env luajit
|
||||
require("utils")
|
||||
|
||||
local function trunc(a)
|
||||
if a < 0 then
|
||||
return math.ceil(a)
|
||||
else
|
||||
return math.floor(a)
|
||||
end
|
||||
end
|
||||
|
||||
local map = { w=1, x=2, y=3, z=4 }
|
||||
local instructions = {}
|
||||
for line in io.lines(arg[3]) do
|
||||
local inst, a1, a2 = unpack(split(line, " "))
|
||||
if inst == "inp" then
|
||||
table.insert(instructions, function(memory, input)
|
||||
local i, o = 9, memory[a1]
|
||||
return function()
|
||||
if i > 1 then
|
||||
i = i - 1
|
||||
memory[a1] = i
|
||||
return input * 10 + i
|
||||
else
|
||||
memory[a1] = o
|
||||
end
|
||||
end
|
||||
end)
|
||||
elseif inst == "add" then
|
||||
table.insert(instructions, function(memory, input)
|
||||
local done, o = false, memory[a1]
|
||||
return function()
|
||||
if not done then
|
||||
memory[a1] = memory[a1] + (memory[a2] or tonumber(a2))
|
||||
done = true
|
||||
return input
|
||||
else
|
||||
memory[a1] = o
|
||||
end
|
||||
end
|
||||
end)
|
||||
elseif inst == "mul" then
|
||||
table.insert(instructions, function(memory, input)
|
||||
local done, o = false, memory[a1]
|
||||
return function()
|
||||
if not done then
|
||||
memory[a1] = memory[a1] * (memory[a2] or tonumber(a2))
|
||||
done = true
|
||||
return input
|
||||
else
|
||||
memory[a1] = o
|
||||
end
|
||||
end
|
||||
end)
|
||||
elseif inst == "div" then
|
||||
table.insert(instructions, function(memory, input)
|
||||
local done, o = false, memory[a1]
|
||||
return function()
|
||||
if not done and (memory[a2] or tonumber(a2)) ~= 0 then
|
||||
memory[a1] = trunc(memory[a1] / (memory[a2] or tonumber(a2)))
|
||||
done = true
|
||||
return input
|
||||
else
|
||||
memory[a1] = o
|
||||
end
|
||||
end
|
||||
end)
|
||||
elseif inst == "mod" then
|
||||
table.insert(instructions, function(memory, input)
|
||||
local done, o = false, memory[a1]
|
||||
return function()
|
||||
local v = memory[a2] or tonumber(a2)
|
||||
if not done and memory[a1] >= 0 and v > 0 then
|
||||
memory[a1] = memory[a1] % v
|
||||
done = true
|
||||
return input
|
||||
else
|
||||
memory[a1] = o
|
||||
end
|
||||
end
|
||||
end)
|
||||
elseif inst == "eql" then
|
||||
table.insert(instructions, function(memory, input)
|
||||
local done, o = false, memory[a1]
|
||||
return function()
|
||||
if not done then
|
||||
if memory[a1] == (memory[a2] or tonumber(a2)) then
|
||||
memory[a1] = 1
|
||||
else
|
||||
memory[a1] = 0
|
||||
end
|
||||
done = true
|
||||
return input
|
||||
else
|
||||
memory[a1] = o
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
local maximum = 0
|
||||
local function backtrack(i, memory, input)
|
||||
if i == #instructions then
|
||||
if memory.z == 0 and input > maximum then
|
||||
print(input)
|
||||
print(os.date())
|
||||
maximum = input
|
||||
end
|
||||
else
|
||||
-- io.write("i "..tostring(i).." ")
|
||||
-- printtable(memory)
|
||||
for newinput in instructions[i](memory, input) do
|
||||
backtrack(i + 1, memory, newinput)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
backtrack(1, { w=0, x=0, y=0, z=0 }, 0)
|
||||
print(maximum)
|
Loading…
Reference in New Issue
Block a user