Day3 second

This commit is contained in:
2025-12-04 21:26:21 +01:00
parent febe80cfb5
commit 1785bf5038

View File

@@ -10,51 +10,31 @@ function debug(msg)
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
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
-- 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 = ""
local Digits = {}
local Start = 1
-- Outer loop for Digits
for I = 1, tonumber(Size) do
BatteryString = BatteryString .. tostring(BatteryDigits[I])
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
return BatteryString
end
end
return table.concat(Digits)
end
function TestMaxJolt(Battery, Test, Size)
@@ -71,11 +51,13 @@ 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)
local Jolt = MaxJolt(battery_line, 12)
print(Jolt)
TotalJolt = TotalJolt + Jolt
end
print("Total " .. TotalJolt)
print("Total " .. string.format("%18.0f", TotalJolt))