Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Input: lines like "<#>,<#>"
- # Output: largest rectangle with corners at two such points
- # and fully within lines between adjacent points (last/first adjacent)
- points = []
- file = open("9_input.txt", "r")
- for line in file:
- line = line.replace("\n", "")
- point_str = line.split(",")
- point = [int(point_str[0]), int(point_str[1])]
- points.append(point)
- x_coords = []
- y_coords = []
- for point in points:
- if not (point[0] in x_coords):
- x_coords.append(point[0])
- if not (point[1] in y_coords):
- y_coords.append(point[1])
- x_coords = sorted(x_coords)
- y_coords = sorted(y_coords)
- x_indices = {}
- y_indices = {}
- for index in range(0, len(x_coords)):
- x_indices[x_coords[index]] = 2 * index + 2
- for index in range(0, len(y_coords)):
- y_indices[y_coords[index]] = 2 * index + 2
- grid_size_x = 2 * len(x_coords) + 8
- grid_size_y = 2 * len(y_coords) + 8
- grid = []
- for x_index in range(0, grid_size_x):
- row = []
- for y_index in range(0, grid_size_y):
- row.append("?")
- grid.append(row)
- def fill_line(point, point2):
- min_x = min(point[0], point2[0])
- max_x = max(point[0], point2[0])
- min_y = min(point[1], point2[1])
- max_y = max(point[1], point2[1])
- for x_index in range(x_indices[min_x], x_indices[max_x] + 1):
- for y_index in range(y_indices[min_y], y_indices[max_y] + 1):
- grid[x_index][y_index] = "#"
- for index in range(0, len(points) - 1):
- fill_line(points[index], points[index + 1])
- fill_line(points[len(points) - 1], points[0])
- grid[0][0] = "."
- outside_points = [[0, 0]]
- def flood_neighbors(outside_point):
- for dx in range(-1, 2):
- for dy in range(-1, 2):
- x = outside_point[0] + dx
- y = outside_point[1] + dy
- if 0 <= x < len(grid):
- if 0 <= y < len(grid[0]):
- if grid[x][y] == "?":
- outside_points.append([x, y])
- grid[x][y] = "."
- while len(outside_points) > 0:
- outside_point = outside_points.pop(0)
- flood_neighbors(outside_point)
- def is_filled(point, point2):
- min_x = min(point[0], point2[0])
- max_x = max(point[0], point2[0])
- min_y = min(point[1], point2[1])
- max_y = max(point[1], point2[1])
- for x_index in range(x_indices[min_x], x_indices[max_x] + 1):
- for y_index in range(y_indices[min_y], y_indices[max_y] + 1):
- if grid[x_index][y_index] == ".":
- return False
- return True
- max_area = 0
- for index in range(0, len(points) - 1):
- for index2 in range(index + 1, len(points)):
- point, point2 = points[index], points[index2]
- if is_filled(point, point2):
- area = (abs(point[0] - point2[0]) + 1) * (abs(point[1] - point2[1]) + 1)
- max_area = max(max_area, area)
- print (max_area)
Advertisement
Add Comment
Please, Sign In to add comment