diff --git a/day09/sol.py b/day09/sol.py index dd85c3f..007c574 100644 --- a/day09/sol.py +++ b/day09/sol.py @@ -13,6 +13,55 @@ def solve(input): 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