37 lines
1014 B
Lua
Executable File
37 lines
1014 B
Lua
Executable File
input = {};
|
|
|
|
-- import input from external file
|
|
for line in io.lines("../input/01.txt") do
|
|
table.insert(input, line)
|
|
end
|
|
|
|
parsed = {};
|
|
|
|
for key, value in pairs(input) do
|
|
-- strip non-numerical chars from input
|
|
local alphastrip = string.gsub(value, "[%a]", "");
|
|
|
|
-- if remaining number is 1 char long, double it and then put it into the table
|
|
if string.len(alphastrip) == 1 then
|
|
local doubled = alphastrip .. alphastrip;
|
|
table.insert(parsed, doubled);
|
|
|
|
-- if remaining number is 2 chars long, put it into the table as-is
|
|
elseif string.len(alphastrip) == 2 then
|
|
table.insert(parsed, alphastrip);
|
|
|
|
-- otherwise fetch first and last digits, concat them, and insert them into the table that way
|
|
else
|
|
local digitstrip = string.match(alphastrip, "%d", 1) .. string.match(alphastrip, "%d", -1);
|
|
table.insert(parsed, digitstrip);
|
|
end
|
|
end
|
|
|
|
result = 0;
|
|
|
|
-- add all the values stored in `parsed` together
|
|
for key, value in pairs(parsed) do
|
|
result = value + result;
|
|
end
|
|
|
|
print(result); |