64 lines
1.5 KiB
Lua
Executable File
64 lines
1.5 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)
|
|
debug("Staring Battery Check [" .. Battery .. "] Size [" .. Size .. "]")
|
|
|
|
-- Load Battery into Array
|
|
local BatteryTable = {}
|
|
local BatterySize = string.len(Battery)
|
|
for Cell in Battery:gmatch(".") do
|
|
table.insert(BatteryTable, tonumber(Cell))
|
|
end
|
|
|
|
local Digits = {}
|
|
local Start = 1
|
|
|
|
-- Outer loop for Digits
|
|
for I = 1, tonumber(Size) do
|
|
debug("Checking digits " .. Start .. " till " .. (BatterySize - (Size - I)))
|
|
Digits[I] = 0
|
|
for Check = Start, BatterySize - (Size - I) do
|
|
if Digits[I] < BatteryTable[Check] then
|
|
Digits[I] = BatteryTable[Check]
|
|
Start = Check + 1
|
|
end
|
|
end
|
|
end
|
|
|
|
return table.concat(Digits)
|
|
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
|
|
local Jolt = MaxJolt(battery_line, 12)
|
|
print(Jolt)
|
|
TotalJolt = TotalJolt + Jolt
|
|
end
|
|
print("Total " .. string.format("%18.0f", TotalJolt))
|