From 780c199a506d5ebc5b14ee957bdb32a9b6186541 Mon Sep 17 00:00:00 2001 From: ngoomie Date: Sat, 2 Dec 2023 21:05:27 -0700 Subject: [PATCH] day 1: fix formatting a lil and add comments --- 01/main.lua | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/01/main.lua b/01/main.lua index a4ec1e8..1249c38 100755 --- a/01/main.lua +++ b/01/main.lua @@ -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