-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday09.py
More file actions
50 lines (31 loc) · 1.34 KB
/
Copy pathday09.py
File metadata and controls
50 lines (31 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from itertools import permutations
from shapely.geometry.polygon import Polygon
filepath = "day09.txt"
def read_file():
return [list(map(int, line.strip().split(","))) for line in open(filepath)]
def normalize_coordinates(all_coordinates):
min_x = min(x for (x, y) in all_coordinates)
min_y = min(y for (x, y) in all_coordinates)
return [(x - min_x, y - min_y) for x, y in all_coordinates]
def calculate_area(coordinates_pair):
area = (abs(coordinates_pair[0][0] - coordinates_pair[1][0]) + 1) * (abs(coordinates_pair[0][1] - coordinates_pair[1][1]) + 1)
print(f"Pair of coordinates to calculate: {coordinates_pair}, the area is: {area}")
return area
def solve_part_I():
all_coordinates = read_file()
print(all_coordinates)
return max(calculate_area(perm) for perm in permutations(all_coordinates, 2))
# result_1 = solve_part_I()
# print(result_1)
def solve_part_II():
all_coordinates = read_file()
print(all_coordinates)
polygon = Polygon(all_coordinates)
max_area = 0
for perm in permutations(all_coordinates, 2):
polygon_to_prove = Polygon([perm[0], (perm[0][0], perm[1][1]), perm[1], (perm[1][0], perm[0][1])])
if polygon.contains(polygon_to_prove):
max_area = max(max_area, calculate_area(perm))
return max_area
result_2 = solve_part_II()
print(result_2)