Skip to content

Commit

Permalink
Attempt file io in lua
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Sep 20, 2024
1 parent 122915e commit 13bfbef
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 19 deletions.
18 changes: 18 additions & 0 deletions docs/src/lua/lib/utils.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
utils.lua
=========

View source code :source:`lua/src/lib/utils.lua`

.. lua:function:: get_data_file(name, mode)
:return: The contents of a file in ``/_data``
:rtype: string

.. lua:function:: get_answer(id)
:return: The answer to a specific problem, as stored in ``/_data/answers.tsv``
:rtype: number | string

.. literalinclude:: ../../../../lua/src/lib/utils.lua
:language: Lua
:linenos:
47 changes: 47 additions & 0 deletions lua/src/lib/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local function get_data_file(name, mode)
local file = io.open("../_data/" .. name, mode)

if not file then
print("Could not open file: " .. filename)
return
end

local contents = file:read("*a") -- Read all contents
file:close()
return contents
end

local function get_answer(id)
local id_str = tostring(id)
for line in string.gmatch(get_data_file("answers.tsv", "r"), "[^\r\n]+") do
local row = {} -- {id, type, length, value}
for val in string.gmatch(line, "[^\t]+") do
table.insert(row, val)
end
if id_str == row[1] then
local type_ = row[2]
local length = tonumber(row[3])
local value = row[4]

if type_ == 'str' then
return value
else if type_ == 'int' then
return tonumber(value)
else if type_ == 'uint' then
if length < 64 then
return tonumber(value)
end
local parsed = tonumber(value)
if tostring(parsed) == value then
return parsed
end
return nil, "Value too big to properly parse"
end
end
end
end

return {
get_data_file = get_data_file,
get_answer = get_answer,
}
40 changes: 21 additions & 19 deletions lua/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,29 @@ local function check_problem(file_name, expected_answer, is_slow, problem_name)
print("Problem " .. problem_name .. " passed (in ~" .. string.format("%.3f", elapsed_time) .. "s).")
end

local get_answer = loadlib("utils").get_answer

-- Problems configuration: filename -> {expected_answer, is_slow}
local problems = {
["p0001.lua"] = {233168, false},
["p0002.lua"] = {4613732, false},
["p0003.lua"] = {6857, false},
["p0004.lua"] = {906609, false},
["p0005.lua"] = {232792560, false},
["p0006.lua"] = {25164150, false},
["p0007.lua"] = {104743, false},
["p0008.lua"] = {23514624000, false},
["p0009.lua"] = {31875000, false},
["p0010.lua"] = {142913828922, false},
["p0011.lua"] = {70600674, false},
["p0013.lua"] = {5537376230, false},
["p0014.lua"] = {837799, false},
["p0015.lua"] = {137846528820, false},
["p0017.lua"] = {21124, false},
["p0028.lua"] = {669171001, false},
["p0034.lua"] = {40730, false},
["p0836.lua"] = {"aprilfoolsjoke", false},
["p0076.lua"] = {190569291, true},
["p0001.lua"] = {get_answer(1), false},
["p0002.lua"] = {get_answer(2), false},
["p0003.lua"] = {get_answer(3), false},
["p0004.lua"] = {get_answer(4), false},
["p0005.lua"] = {get_answer(5), false},
["p0006.lua"] = {get_answer(6), false},
["p0007.lua"] = {get_answer(7), false},
["p0008.lua"] = {get_answer(8), false},
["p0009.lua"] = {get_answer(9), false},
["p0010.lua"] = {get_answer(10), false},
["p0011.lua"] = {get_answer(11), false},
["p0013.lua"] = {get_answer(13), false},
["p0014.lua"] = {get_answer(14), false},
["p0015.lua"] = {get_answer(15), false},
["p0017.lua"] = {get_answer(17), false},
["p0028.lua"] = {get_answer(28), false},
["p0034.lua"] = {get_answer(34), false},
["p0076.lua"] = {get_answer(76), true},
["p0836.lua"] = {get_answer(836), false},
}

-- Fast testing loop
Expand Down

0 comments on commit 13bfbef

Please sign in to comment.