From 1785bf503814d36003120a8449dc69d5d230a395 Mon Sep 17 00:00:00 2001 From: Bas Grolleman Date: Thu, 4 Dec 2025 21:26:21 +0100 Subject: [PATCH] Day3 second --- 2025/Day3/Day3_second.lua | 72 +++++++++++++++------------------------ 1 file changed, 27 insertions(+), 45 deletions(-) diff --git a/2025/Day3/Day3_second.lua b/2025/Day3/Day3_second.lua index 1bdf826..fd2ff53 100755 --- a/2025/Day3/Day3_second.lua +++ b/2025/Day3/Day3_second.lua @@ -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 + end end - return BatteryString + + 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))