adventofcode-2021/utils.lua

41 lines
668 B
Lua
Raw Normal View History

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
2021-12-07 08:29:45 +01:00
function foreach(t, f)
for k, v in pairs(t) do
t[k] = f(v)
end
end
2021-12-08 08:37:36 +01:00
function printtable(t)
for k, v in pairs(t) do
io.write(k..":"..v.." ")
end
io.write("\n")
end
function count(t, pred)
local count = 0
for k, v in pairs(t) do
if pred(v) then count = count + 1 end
end
return count
end