-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathq2.py
105 lines (73 loc) · 2.96 KB
/
q2.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
Q2: Box Constellations
I assume by "opposite" in the question they mean parallel.
Since the number of points is very small, I think my approach is going to be to iterate
over all pairs of points, generate a dictionary of gradient to pairs of points with that
gradient, then iterate over those to find the largest.
The example from the diagram is
10
0 3 1 4 2 4 3 4 4 5 4 3 5 3 2 2 3 1 3 2
"""
import itertools
import math
from typing import List, Tuple
from collections import defaultdict
def group_by_gradient(points: List[Tuple[int, int]]):
"""Transform a list of points into a dictionary from gradient to a list of pairs of
points that gradient."""
grouped = defaultdict(list)
points = sorted(points)
for i, left in enumerate(points):
left_x, left_y = left
for right in points[i + 1 :]:
right_x, right_y = right
# since we're sorting, and iterating from i + 1,
assert right_x >= left_x
assert left_x != right_x or left_y != right_y
if right_x - left_x == 0:
gradient = None
else:
gradient = (right_y - left_y) / (right_x - left_x)
grouped[gradient].append((left, right))
return grouped
def parse_pairs(input_line):
"""Transforms a string of the form 'x y x y x y' into a list of the form
`[(x, y), (x, y), (x, y)]`"""
for i, (x, y) in enumerate(itertools.pairwise(input_line.split(" "))):
if i % 2 == 0:
yield (int(x), int(y))
def intercept(pair, gradient):
"""Returns the y-intercept for a pair of points, where x_2 != x_1"""
((x_1, y_1), (_x_2, _y_2)) = pair
return y_1 - gradient * x_1
def vector_length(pair):
"""Returns the length of the vector between the pair of points `pair`."""
((x_1, y_1), (x_2, y_2)) = pair
return math.sqrt((x_1 - x_2) ** 2 + (y_1 - y_2) ** 2)
def size(first_pair, second_pair, gradient):
"""Returns the size of the quadrilateral formed by the pair of pairs of points
`first_pair` and `second_pair`."""
if gradient is None:
# x is the same for each pair, so just take the difference
perpindicular_distance = abs(first_pair[0][0] - second_pair[0][0])
else:
perpindicular_distance = abs(
intercept(first_pair, gradient) - intercept(second_pair, gradient)
) / math.sqrt(1 + (gradient**2))
return (
(vector_length(first_pair) + vector_length(second_pair)) / 2
) * perpindicular_distance
def main():
_count = input()
points = list(parse_pairs(input()))
# then, pick the largest
best_size = None
for gradient, pairs in group_by_gradient(points).items():
for i, left in enumerate(pairs):
for right in pairs[i + 1 :]:
this_size = size(left, right, gradient)
if best_size is None or this_size > best_size:
best_size = this_size
print(f"{best_size:.2f}")
if __name__ == "__main__":
main()