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 end
function MaxJolt(Battery, Size) function MaxJolt(Battery, Size)
-- TODO - This going to be a rework, I need to loop 12 times debug("Staring Battery Check [" .. Battery .. "] Size [" .. Size .. "]")
local BatteryDigits = {}
for I = 1, tonumber(Size) do -- Load Battery into Array
BatteryDigits[I] = 0 local BatteryTable = {}
local BatterySize = string.len(Battery)
for Cell in Battery:gmatch(".") do
table.insert(BatteryTable, tonumber(Cell))
end end
-- lastdigit = tonumber(string.sub(Battery, -1)) local Digits = {}
-- debug("lastdigit " .. lastdigit) local Start = 1
--
-- BatteryNoLast = string.sub(Battery, 0, -2) -- Outer loop for Digits
-- 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 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
end
end end
return BatteryString
return table.concat(Digits)
end end
function TestMaxJolt(Battery, Test, Size) function TestMaxJolt(Battery, Test, Size)
@@ -71,11 +51,13 @@ TestMaxJolt("987654321111111", "987654321111", 12)
TestMaxJolt("811111111111119", "811111111119", 12) TestMaxJolt("811111111111119", "811111111119", 12)
TestMaxJolt("234234234234278", "434234234278", 12) TestMaxJolt("234234234234278", "434234234278", 12)
TestMaxJolt("818181911112111", "888911112111", 12) TestMaxJolt("818181911112111", "888911112111", 12)
--
print("# Calculate MaxJolt") print("# Calculate MaxJolt")
local TotalJolt = 0 local TotalJolt = 0
--
for battery_line in io.lines("full_input") do 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 end
print("Total " .. TotalJolt) print("Total " .. string.format("%18.0f", TotalJolt))