clean up with sign function

This commit is contained in:
Felix Van der Jeugt 2021-12-05 11:48:31 +01:00
parent f2abc065ad
commit 87637a4682
No known key found for this signature in database
GPG Key ID: 58B209295023754D

View File

@ -10,13 +10,16 @@ function readline()
return x1, y1, x2, y2
end
function mark(field, x, y, count, dx, dy)
local i = 0
while i <= count do
local row = field[x+i*dx] or {}
row[y+i*dy] = (row[y+i*dy] or 0) + 1
field[x+i*dx] = row
i = i + 1
function mark(field, x, y)
local row = field[x] or {}
row[y] = (row[y] or 0) + 1
field[x] = row
end
function sign(n)
if n == 0 then return 0
elseif n < 0 then return -1
else return 1
end
end
@ -24,15 +27,11 @@ io.input(arg[3])
local x1, y1, x2, y2 = readline()
local field = {}
while x1 ~= nil do
local x, y, count, dx, dy
if x1 == x2 then
mark(field, x1, math.min(y1, y2), math.abs(y1 - y2), 0, 1)
elseif y1 == y2 then
mark(field, math.min(x1, x2), y1, math.abs(x2 - x1), 1, 0)
elseif x1 + y1 == x2 + y2 then
mark(field, math.min(x1, x2), math.max(y1, y2), math.abs(x1 - x2), 1, -1)
else
mark(field, math.min(x1, x2), math.min(y1, y2), math.abs(x1 - x2), 1, 1)
local x, y, dx, dy = x1, y1, sign(x2 - x1), sign(y2 - y1)
mark(field, x, y)
while x ~= x2 or y ~= y2 do
x, y = x + dx, y + dy
mark(field, x, y)
end
x1, y1, x2, y2 = readline()
end