day 1: fix formatting a lil and add comments
This commit is contained in:
parent
bc39d1bc3d
commit
780c199a50
|
@ -1,5 +1,6 @@
|
|||
input = {};
|
||||
|
||||
-- import input from external file
|
||||
for line in io.lines("./input.txt") do
|
||||
table.insert(input, line)
|
||||
end
|
||||
|
@ -7,22 +8,28 @@ 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);
|
||||
--print(digitstrip);
|
||||
end
|
||||
end
|
||||
|
||||
result = 0;
|
||||
|
||||
-- add all the values stored in `parsed` together
|
||||
for key, value in pairs(parsed) do
|
||||
result = value + result;
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue