-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
122915e
commit 13bfbef
Showing
3 changed files
with
86 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters