Files
adventofcode/2025/Day6/Day6_2nd.lua

46 lines
926 B
Lua

-- Day 6 Lua
--
require("functions")
local input_file = "Day6/full"
local input = file_characters_to_table(input_file)
local rows = tablelength(input)
local cols = tablelength(input[1])
local grandtotal = 0
local math = {}
for col = cols, 1, -1 do
local num = ""
local calc = ""
for row = 1, rows do
-- print("Checking R" .. row .. "C" .. col .. " " .. input[row][col])
v = input[row][col]
if tonumber(v) ~= nil then
num = num .. input[row][col]
elseif v == "*" or v == "+" then
calc = v
end
end
table.insert(math, tonumber(num))
-- print("Adding " .. num)
if calc == "*" or calc == "+" then
local t = 0
for k, v in ipairs(math) do
if t == 0 then
t = v
else
if calc == "+" then
t = t + v
end
if calc == "*" then
t = t * v
end
end
end
print(calc .. " " .. t)
grandtotal = grandtotal + t
math = {}
end
end
print()
print("Grand total = " .. grandtotal)