adventofcode-2021/utils.lua

26 lines
437 B
Lua

function split(text, sep)
local parts = {}
local start = 0
local from, to = text:find(sep)
while from ~= nil do
table.insert(parts, text:sub(start, from - 1))
text = text:sub(to + 1)
from, to = text:find(sep)
end
table.insert(parts, text)
return parts
end
function sign(n)
if n == 0 then return 0
elseif n < 0 then return -1
else return 1
end
end
function foreach(t, f)
for k, v in pairs(t) do
t[k] = f(v)
end
end