57 lines
1.2 KiB
Lua
Executable File
57 lines
1.2 KiB
Lua
Executable File
#!/usr/bin/lua
|
|
-- See https://adventofcode.com/2025/day/3
|
|
|
|
local DEBUG = true
|
|
|
|
function debug(msg)
|
|
if DEBUG then
|
|
print(msg)
|
|
end
|
|
end
|
|
|
|
function MaxJolt(Battery)
|
|
local b = {}
|
|
local firstdigit = 0
|
|
local seconddigit = 0
|
|
local maxdigit = 0
|
|
|
|
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
|
|
elseif seconddigit < checkdigit then
|
|
seconddigit = checkdigit
|
|
end
|
|
end
|
|
if seconddigit < lastdigit then
|
|
seconddigit = lastdigit
|
|
end
|
|
debug("First " .. firstdigit)
|
|
debug("Second " .. seconddigit)
|
|
return tostring(firstdigit) .. tostring(seconddigit)
|
|
end
|
|
|
|
function TestMaxJolt(Battery, Test)
|
|
local MJ = MaxJolt(Battery)
|
|
if MJ == Test then
|
|
print("WIN - " .. Battery .. " " .. MJ .. " == " .. Test)
|
|
else
|
|
print("FAIL - " .. Battery .. " " .. MJ .. " != " .. Test)
|
|
end
|
|
end
|
|
|
|
print("# Self Test")
|
|
TestMaxJolt("987654321111111", "98")
|
|
TestMaxJolt("811111111111119", "89")
|
|
TestMaxJolt("234234234234278", "78")
|
|
TestMaxJolt("818181911112111", "89")
|