Compare commits
No commits in common. "4b9196370a7408e46b7deff3f237de07f98aecf4" and "2ce473c58d190fc115475db0649a4e10f201d40a" have entirely different histories.
4b9196370a
...
2ce473c58d
@ -1,42 +0,0 @@
|
||||
--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)
|
@ -1,67 +0,0 @@
|
||||
--Lua 5.4.2
|
||||
local File = io.open("input.txt", "r")
|
||||
|
||||
if not File then
|
||||
print("File not found")
|
||||
return
|
||||
end
|
||||
|
||||
local function IsReportSafe(Numbers)
|
||||
local IsSafe = true
|
||||
local LastSign = 0
|
||||
local LastNumber
|
||||
for _, Number in next, Numbers 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
|
||||
return IsSafe
|
||||
end
|
||||
|
||||
local function Clone(t) --surface level copy
|
||||
local Cloned = {}
|
||||
for Key, Value in next, t do
|
||||
Cloned[Key] = Value
|
||||
end
|
||||
return Cloned
|
||||
end
|
||||
|
||||
local SafeReports = 0
|
||||
|
||||
for Line in File:lines() do
|
||||
local Numbers = {}
|
||||
for Number in Line:gmatch("%d+") do
|
||||
table.insert(Numbers, tonumber(Number))
|
||||
end
|
||||
if IsReportSafe(Numbers) then
|
||||
SafeReports = SafeReports + 1
|
||||
else
|
||||
for Index = 1, #Numbers do
|
||||
local NumbersCopy = Clone(Numbers) --yikes
|
||||
table.remove(NumbersCopy, Index)
|
||||
if IsReportSafe(NumbersCopy) then
|
||||
SafeReports = SafeReports + 1
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
print("Safe reports:", SafeReports)
|
1000
day2/input.txt
1000
day2/input.txt
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user