Guest User

Untitled

a guest
Dec 9th, 2025
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.67 KB | None | 0 0
  1. # Input: lines like "<#>,<#>"
  2. # Output: largest rectangle with corners at two such points
  3. #         and fully within lines between adjacent points (last/first adjacent)
  4.  
  5. points = []
  6.  
  7. file = open("9_input.txt", "r")
  8. for line in file:
  9.   line = line.replace("\n", "")
  10.   point_str = line.split(",")
  11.   point = [int(point_str[0]), int(point_str[1])]
  12.   points.append(point)
  13.  
  14. x_coords = []
  15. y_coords = []
  16.  
  17. for point in points:
  18.   if not (point[0] in x_coords):
  19.     x_coords.append(point[0])
  20.   if not (point[1] in y_coords):
  21.     y_coords.append(point[1])
  22.  
  23. x_coords = sorted(x_coords)
  24. y_coords = sorted(y_coords)
  25.  
  26. x_indices = {}
  27. y_indices = {}
  28.  
  29. for index in range(0, len(x_coords)):
  30.   x_indices[x_coords[index]] = 2 * index + 2
  31. for index in range(0, len(y_coords)):
  32.   y_indices[y_coords[index]] = 2 * index + 2
  33.  
  34. grid_size_x = 2 * len(x_coords) + 8
  35. grid_size_y = 2 * len(y_coords) + 8
  36.  
  37. grid = []
  38. for x_index in range(0, grid_size_x):
  39.   row = []
  40.   for y_index in range(0, grid_size_y):
  41.     row.append("?")
  42.   grid.append(row)
  43.  
  44. def fill_line(point, point2):
  45.   min_x = min(point[0], point2[0])
  46.   max_x = max(point[0], point2[0])
  47.   min_y = min(point[1], point2[1])
  48.   max_y = max(point[1], point2[1])
  49.   for x_index in range(x_indices[min_x], x_indices[max_x] + 1):
  50.     for y_index in range(y_indices[min_y], y_indices[max_y] + 1):
  51.       grid[x_index][y_index] = "#"
  52.  
  53. for index in range(0, len(points) - 1):
  54.   fill_line(points[index], points[index + 1])
  55.  
  56. fill_line(points[len(points) - 1], points[0])
  57.  
  58. grid[0][0] = "."
  59.  
  60. outside_points = [[0, 0]]
  61.  
  62. def flood_neighbors(outside_point):
  63.   for dx in range(-1, 2):
  64.     for dy in range(-1, 2):
  65.       x = outside_point[0] + dx
  66.       y = outside_point[1] + dy
  67.       if 0 <= x < len(grid):
  68.         if 0 <= y < len(grid[0]):
  69.           if grid[x][y] == "?":
  70.             outside_points.append([x, y])
  71.             grid[x][y] = "."
  72.  
  73. while len(outside_points) > 0:
  74.   outside_point = outside_points.pop(0)
  75.   flood_neighbors(outside_point)
  76.  
  77. def is_filled(point, point2):
  78.   min_x = min(point[0], point2[0])
  79.   max_x = max(point[0], point2[0])
  80.   min_y = min(point[1], point2[1])
  81.   max_y = max(point[1], point2[1])
  82.   for x_index in range(x_indices[min_x], x_indices[max_x] + 1):
  83.     for y_index in range(y_indices[min_y], y_indices[max_y] + 1):
  84.       if grid[x_index][y_index] == ".":
  85.         return False
  86.   return True
  87.  
  88. max_area = 0
  89. for index in range(0, len(points) - 1):
  90.   for index2 in range(index + 1, len(points)):
  91.     point, point2 = points[index], points[index2]
  92.     if is_filled(point, point2):
  93.       area = (abs(point[0] - point2[0]) + 1) * (abs(point[1] - point2[1]) + 1)
  94.       max_area = max(max_area, area)
  95. print (max_area)
Advertisement
Add Comment
Please, Sign In to add comment