Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def day9(s, *, part2=False):
- points = [tuple(map(int, line.split(','))) for line in s.splitlines()]
- pairs = [
- (min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2))
- for (x1, y1), (x2, y2) in itertools.combinations(points, 2)
- ]
- def area(x1, y1, x2, y2):
- return (x2 - x1 + 1) * (y2 - y1 + 1)
- if not part2:
- return max(area(*pair) for pair in pairs)
- edges = [
- (min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2))
- for (x1, y1), (x2, y2) in itertools.pairwise(points + [points[0]])
- ]
- length = lambda p: p[2] - p[0] + p[3] - p[1]
- edges = sorted(edges, key=length, reverse=True) # Optional, provides 4x speedup.
- return max(
- area(x1, y1, x2, y2)
- for x1, y1, x2, y2 in pairs
- if all(xx1 >= x2 or xx2 <= x1 or yy1 >= y2 or yy2 <= y1 for xx1, yy1, xx2, yy2 in edges)
- )
Advertisement
Add Comment
Please, Sign In to add comment