Advent of Code 2019 - Lua
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.7 KiB

n_begin, n_end = string.match(arg[1], "(%d+)-(%d+)")
--[[
t_begin = {}
t_end = {}
for c in n_begin:gmatch(".") do
table.insert(t_begin, tonumber(c))
end
for c in n_end:gmatch(".") do
table.insert(t_end, tonumber(c))
end
-- Minimum possible value
highest = 0
for i, x in ipairs(t_begin) do
if x < highest then
t_begin[i] = highest
elseif x > highest then
highest = x
end
end
function compare(tbl1, tbl2)
for i, x in ipairs(tbl1) do
local v = x - tbl2[i]
if v ~= 0 then
return v
end
end
end
num = {table.unpack(t_begin)}
part1 = 0
while compare(num, t_end) < 0 do
for i = 1, #num do
if num[i] < t_end[i] then
if num[i] < num[i + 1] then
num[i] = num[i] + 1
part1 = part1 + 1
end
else
end
end
end
print(part1)
for _, x in ipairs(num) do
io.stdout:write(x)
end
io.stdout:write("\n")
]]--
function is_valid(num)
local str = tostring(num)
local lastchar = nil
local inarow = 0
local gotmatch = false
for char = 1, #str do
if lastchar and str:byte(char) < lastchar then
return false
end
if str:byte(char) == lastchar then
inarow = inarow + 1
--gotmatch = true
else
if inarow == 1 then
gotmatch = true
end
inarow = 0
end
lastchar = str:byte(char)
end
if inarow == 1 then
gotmatch = true
end
return gotmatch
end
possibilities = 0
for i = tonumber(n_begin), tonumber(n_end) do
if is_valid(i) then
possibilities = possibilities + 1
end
end
print(possibilities)