Files
adventofcode/2025/Day3/Day3_second.lua
2025-12-04 08:16:42 +01:00

82 lines
1.9 KiB
Lua
Executable File

#!/usr/bin/lua
-- See https://adventofcode.com/2025/day/3
local DEBUG = false
function debug(msg)
if DEBUG then
print(msg)
end
end
function MaxJolt(Battery, Size)
-- TODO - This going to be a rework, I need to loop 12 times
local BatteryDigits = {}
for I = 1, tonumber(Size) do
BatteryDigits[I] = 0
end
-- lastdigit = tonumber(string.sub(Battery, -1))
-- debug("lastdigit " .. lastdigit)
--
-- BatteryNoLast = string.sub(Battery, 0, -2)
-- BatteryNoLast:gsub(".", function(c)
-- table.insert(b, c)
-- return c
-- end)
--
-- for key, value in pairs(b) do
-- checkdigit = tonumber(value)
-- if firstdigit < checkdigit then
-- firstdigit = checkdigit
-- end
-- end
--
-- local passedfirst = false
-- for key, value in pairs(b) do
-- checkdigit = tonumber(value)
-- if passedfirst and seconddigit < checkdigit then
-- seconddigit = checkdigit
-- end
-- if checkdigit == firstdigit then
-- passedfirst = true
-- end
-- end
--
-- if seconddigit < lastdigit then
-- seconddigit = lastdigit
-- end
--
-- debug("First " .. firstdigit)
-- debug("Second " .. seconddigit)
-- return tostring(firstdigit) .. tostring(seconddigit)
local BatteryString = ""
for I = 1, tonumber(Size) do
BatteryString = BatteryString .. tostring(BatteryDigits[I])
end
return BatteryString
end
function TestMaxJolt(Battery, Test, Size)
local MJ = MaxJolt(Battery, Size)
if MJ == Test then
print("WIN - " .. Battery .. " " .. MJ .. " == " .. Test)
else
print("FAIL - " .. Battery .. " " .. MJ .. " != " .. Test)
end
end
print("# Self Test")
TestMaxJolt("987654321111111", "987654321111", 12)
TestMaxJolt("811111111111119", "811111111119", 12)
TestMaxJolt("234234234234278", "434234234278", 12)
TestMaxJolt("818181911112111", "888911112111", 12)
print("# Calculate MaxJolt")
local TotalJolt = 0
for battery_line in io.lines("full_input") do
TotalJolt = TotalJolt + MaxJolt(battery_line)
end
print("Total " .. TotalJolt)