day 1: fix formatting a lil and add comments

This commit is contained in:
ngoomie 2023-12-02 21:05:27 -07:00
parent bc39d1bc3d
commit 780c199a50
1 changed files with 8 additions and 1 deletions

View File

@ -1,5 +1,6 @@
input = {}; input = {};
-- import input from external file
for line in io.lines("./input.txt") do for line in io.lines("./input.txt") do
table.insert(input, line) table.insert(input, line)
end end
@ -7,22 +8,28 @@ end
parsed = {}; parsed = {};
for key, value in pairs(input) do for key, value in pairs(input) do
-- strip non-numerical chars from input
local alphastrip = string.gsub(value, "[%a]", ""); 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 if string.len(alphastrip) == 1 then
local doubled = alphastrip .. alphastrip; local doubled = alphastrip .. alphastrip;
table.insert(parsed, doubled); table.insert(parsed, doubled);
-- if remaining number is 2 chars long, put it into the table as-is
elseif string.len(alphastrip) == 2 then elseif string.len(alphastrip) == 2 then
table.insert(parsed, alphastrip); table.insert(parsed, alphastrip);
-- otherwise fetch first and last digits, concat them, and insert them into the table that way
else else
local digitstrip = string.match(alphastrip, "%d", 1) .. string.match(alphastrip, "%d", -1); local digitstrip = string.match(alphastrip, "%d", 1) .. string.match(alphastrip, "%d", -1);
table.insert(parsed, digitstrip); table.insert(parsed, digitstrip);
--print(digitstrip);
end end
end end
result = 0; result = 0;
-- add all the values stored in `parsed` together
for key, value in pairs(parsed) do for key, value in pairs(parsed) do
result = value + result; result = value + result;
end end