This commit is contained in:
Felix Van der Jeugt 2021-12-05 10:11:11 +01:00
parent 15bceda7c4
commit f2abc065ad
No known key found for this signature in database
GPG Key ID: 58B209295023754D
2 changed files with 89 additions and 0 deletions

41
day05/part1.lua Normal file
View File

@ -0,0 +1,41 @@
function readline()
local x1 = io.read("*n")
io.read(1) -- ","
local y1 = io.read("*n")
io.read(4) -- " -> "
local x2 = io.read("*n")
io.read(1) -- ","
local y2 = io.read("*n")
io.read("*l") -- "...\n"
return x1, y1, x2, y2
end
io.input(arg[3])
local x1, y1, x2, y2 = readline()
local field = {}
while x1 ~= nil do
if x1 == x2 then
for y = math.min(y1, y2), math.max(y1, y2) do
local row = field[x1] or {}
row[y] = (row[y] or 0) + 1
field[x1] = row
end
elseif y1 == y2 then
for x = math.min(x1, x2), math.max(x1, x2) do
local row = field[x] or {}
row[y1] = (row[y1] or 0) + 1
field[x] = row
end
end
x1, y1, x2, y2 = readline()
end
local count = 0
for r, row in pairs(field) do
for c, col in pairs(row) do
if col >= 2 then
count = count + 1
end
end
end
print(count)

48
day05/part2.lua Normal file
View File

@ -0,0 +1,48 @@
function readline()
local x1 = io.read("*n")
io.read(1) -- ","
local y1 = io.read("*n")
io.read(4) -- " -> "
local x2 = io.read("*n")
io.read(1) -- ","
local y2 = io.read("*n")
io.read("*l") -- "...\n"
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
end
end
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)
end
x1, y1, x2, y2 = readline()
end
local count = 0
for r, row in pairs(field) do
for c, col in pairs(row) do
if col >= 2 then
count = count + 1
end
end
end
print(count)