aoc/day2/day2_part1.lua

43 lines
948 B
Lua
Raw Permalink Normal View History

2024-12-02 13:52:38 +00:00
--Lua 5.4.2
local File = io.open("input.txt", "r")
if not File then
print("File not found")
return
end
local SafeReports = 0
for Line in File:lines() do
local IsSafe = true
local LastSign = 0
local LastNumber
for Number in Line:gmatch("%d+") do
if not LastNumber then
LastNumber = Number
goto continue
end
local Difference = math.abs(Number - LastNumber)
if Difference > 3 or Difference == 0 then
IsSafe = false
break
end
local Sign = (Number - LastNumber) > 0 and 1 or -1
if LastSign == 0 then
LastSign = Sign
else
if Sign ~= LastSign then
IsSafe = false
break
end
end
LastNumber = Number
::continue::
end
if IsSafe then
SafeReports = SafeReports + 1
end
end
print("Safe reports:", SafeReports)