2025-12-09 06:15:12 +00:00

72 lines
1.9 KiB
Python

from math import dist
def solve(input):
points = []
for line in open(input):
x,y = map(int, line.strip().split(","))
points.append((x,y))
def areas():
for p in points:
for q in points:
if p != q:
yield area(p,q)
print(max(areas()))
lines = list(zip(points, points[1:]+[points[0]]))
# keep only vertical lines, and sort so the uppermost point (lowest x coord) is the first of the pair
lines = [(min(p,q),max(p,q)) for p,q in lines if p[1] != q[1]]
# sort by y coord
lines.sort(key=lambda l: (l[0][1],l[1][1],l[0][0],l[1][0]))
print(lines)
# we want to know if the rectangle formed by a pair of points
# is completely contained within the axis-aligned polygon defined
# by the list of points.
# we can do that with a scanline algorithm:
# for each x position in the list of points,
#
bounds = {}
ys = sorted(set(p[1] for l in lines for p in l))
for y in ys:
# select lines which intersect with the scanline at y
mylines = []
for p,q in lines:
if p[1] <= y <= q[1]:
mylines.append(p[0])
bounds[y] = (min(mylines),max(mylines))
print(ys)
print(bounds)
def inbounds(p,q):
y0 = min(p[1],q[1])
y1 = max(p[1],q[1])
x0 = min(p[0],q[0])
x1 = max(p[0],q[0])
#print(y0, y1, ys)
i = ys.index(y0)
j = ys.index(y1) + 1
for y in ys[i:j]:
if not bounds[y][0] <= x0 <= x1 <= bounds[y][1]:
return False
return True
def areas2():
for p in points:
for q in points:
if p != q and inbounds(p,q):
yield area(p,q)
print(max(areas2()))
def area(p,q):
dx = abs(p[0] - q[0]) + 1
dy = abs(p[1] - q[1]) + 1
return dx*dy
solve("sample")
solve("input")