aoc-2024/template.lua

31 lines
598 B
Lua
Raw Permalink Normal View History

2024-12-01 19:12:21 +00:00
-- see if the file exists
local function file_exists(file)
local f = io.open(file, "rb")
if f then
f:close()
end
return f ~= nil
end
-- get all lines from a file, returns an empty
-- list/table if the file does not exist
local function lines_from(file)
if not file_exists(file) then
return {}
end
local lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
return lines
end
-- tests the functions above
2024-12-02 17:25:59 +00:00
local file = "input"
2024-12-01 19:12:21 +00:00
local lines = lines_from(file)
-- print all line numbers and their contents
for k, v in pairs(lines) do
print("line[" .. k .. "]", v)
end