1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
local f = assert(io.open(arg[1], "r"))
if (arg[1] == "example") then
ROW = 10
elseif (arg[1] == "input") then
ROW = 2000000
end
local nodes = {}
local minx, miny, maxx, maxy
for line in f:lines() do
local prev = nil
for x,y in line:gmatch("x=(-?%d+), y=(-?%d+)") do
x = tonumber(x)
y = tonumber(y)
minx = math.min(x, minx and minx or x)
maxx = math.max(x, maxx and maxx or x)
miny = math.min(y, miny and miny or y)
maxy = math.max(y, maxy and maxy or y)
if (prev) then
table.insert(nodes, {x=prev.x, y=prev.y, bx=x, by=y})
end
prev = {x=x,y=y};
end
end
print(minx,maxx)
print(miny,maxy)
local cntr = {}
for _,v in ipairs(nodes) do
local d = math.abs(v.bx - v.x) + math.abs(v.by - v.y)
dy = math.abs(v.y - ROW)
if (dy <= d) then
for i=v.x-(d-dy),v.x+(d-dy) do
if not (v.bx == i and v.by == ROW) then -- can't have two beacons in the same place
cntr[i] = 1
end
end
end
end
local c = 0
for _ in pairs(cntr) do
c = c + 1
end
print(c)
|