From 13bfbef1338e74932b27228d0061580301892836 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Thu, 19 Sep 2024 21:02:13 -0500 Subject: [PATCH] Attempt file io in lua --- docs/src/lua/lib/utils.rst | 18 +++++++++++++++ lua/src/lib/utils.lua | 47 ++++++++++++++++++++++++++++++++++++++ lua/test.lua | 40 +++++++++++++++++--------------- 3 files changed, 86 insertions(+), 19 deletions(-) create mode 100644 docs/src/lua/lib/utils.rst create mode 100644 lua/src/lib/utils.lua diff --git a/docs/src/lua/lib/utils.rst b/docs/src/lua/lib/utils.rst new file mode 100644 index 00000000..49c11b31 --- /dev/null +++ b/docs/src/lua/lib/utils.rst @@ -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: diff --git a/lua/src/lib/utils.lua b/lua/src/lib/utils.lua new file mode 100644 index 00000000..49c54db7 --- /dev/null +++ b/lua/src/lib/utils.lua @@ -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, +} diff --git a/lua/test.lua b/lua/test.lua index 4e1d91bf..a1a2cd8d 100644 --- a/lua/test.lua +++ b/lua/test.lua @@ -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