day 17
This commit is contained in:
parent
1cc102ee1a
commit
6005fb089d
19
day17/part1.lua
Executable file
19
day17/part1.lua
Executable file
@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env luajit
|
||||
require("utils")
|
||||
|
||||
local xmin, xmax, ymin, ymax = string.match(io.open(arg[3]):read("*a"), "target area: x=(.*)%.%.(.*), y=(.*)%.%.(.*)")
|
||||
xmin, xmax, ymin, ymax = tonumber(xmin), tonumber(xmax), tonumber(ymin), tonumber(ymax)
|
||||
|
||||
local maxdy = 0
|
||||
|
||||
if ymax > 0 then
|
||||
maxdy = ymax
|
||||
end
|
||||
|
||||
if ymin < 0 then
|
||||
if -ymin - 1 > maxdy then
|
||||
maxdy = -ymin - 1
|
||||
end
|
||||
end
|
||||
|
||||
print(euler(maxdy))
|
34
day17/part2.lua
Executable file
34
day17/part2.lua
Executable file
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env luajit
|
||||
require("utils")
|
||||
|
||||
local xmin, xmax, ymin, ymax = string.match(io.open(arg[3]):read("*a"), "target area: x=(.*)%.%.(.*), y=(.*)%.%.(.*)")
|
||||
xmin, xmax, ymin, ymax = tonumber(xmin), tonumber(xmax), tonumber(ymin), tonumber(ymax)
|
||||
|
||||
local mindy = ymin
|
||||
|
||||
local maxdy = 0
|
||||
if ymax > 0 then maxdy = ymax end
|
||||
if ymin < 0 then if -ymin - 1 > maxdy then maxdy = -ymin - 1 end end
|
||||
|
||||
local mindx = 0
|
||||
while euler(mindx) < xmin do mindx = mindx + 1 end
|
||||
|
||||
local maxdx = xmax
|
||||
|
||||
local count = 0
|
||||
for odx = mindx, maxdx do
|
||||
for ody = mindy, maxdy do
|
||||
local x, y, dx, dy = 0, 0, odx, ody
|
||||
while x <= xmax and y >= math.min(0, ymin) do
|
||||
x, y = x + dx, y + dy
|
||||
if xmin <= x and x <= xmax and ymin <= y and y <= ymax then
|
||||
count = count + 1
|
||||
break
|
||||
end
|
||||
if dx > 0 then dx = dx - 1 end
|
||||
dy = dy - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
print(count)
|
Loading…
Reference in New Issue
Block a user