advent-of-code/12022/21/21.lua

28 lines
724 B
Lua

--io.input("test")
io.input("input")
local m = {}
for line in io.lines() do
local pattern = "^(%a+): (.+)$"
local name, arg = string.match(line, pattern)
local num = tonumber(arg)
if num then
m[name] = function() return num end
else
local pattern = "(%a+) ([%+%-%*%/]) (%a+)"
local a, op, b = string.match(arg, pattern)
if op == "+" then
m[name] = function() return m[a]()+m[b]() end
elseif op == "-" then
m[name] = function() return m[a]()-m[b]() end
elseif op == "*" then
m[name] = function() return m[a]()*m[b]() end
elseif op == "/" then
m[name] = function() return m[a]()//m[b]() end
end
end
end
local result1 = m["root"]()
print("part 1", result1)