hhoppe

Advent of code 2025 day 9

Dec 9th, 2025
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. def day9(s, *, part2=False):
  2.   points = [tuple(map(int, line.split(','))) for line in s.splitlines()]
  3.   pairs = [
  4.       (min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2))
  5.       for (x1, y1), (x2, y2) in itertools.combinations(points, 2)
  6.   ]
  7.  
  8.   def area(x1, y1, x2, y2):
  9.     return (x2 - x1 + 1) * (y2 - y1 + 1)
  10.  
  11.   if not part2:
  12.     return max(area(*pair) for pair in pairs)
  13.  
  14.   edges = [
  15.       (min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2))
  16.       for (x1, y1), (x2, y2) in itertools.pairwise(points + [points[0]])
  17.   ]
  18.   length = lambda p: p[2] - p[0] + p[3] - p[1]
  19.   edges = sorted(edges, key=length, reverse=True)  # Optional, provides 4x speedup.
  20.  
  21.   return max(
  22.       area(x1, y1, x2, y2)
  23.       for x1, y1, x2, y2 in pairs
  24.       if all(xx1 >= x2 or xx2 <= x1 or yy1 >= y2 or yy2 <= y1 for xx1, yy1, xx2, yy2 in edges)
  25.   )
Advertisement
Add Comment
Please, Sign In to add comment