-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsquared_shortest_distance.py
More file actions
32 lines (24 loc) · 1.01 KB
/
squared_shortest_distance.py
File metadata and controls
32 lines (24 loc) · 1.01 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
from typing import List
def squared_shortest_distance(
num_robots: int, position_x: List[int], position_y: List[int]
):
coordinates = list(zip(position_x, position_y))
coordinates = list(set(coordinates))
distances = {}
print(f"{coordinates=}")
for robot_one in range(len(coordinates)):
for robot_two in range(len(coordinates)):
if robot_one == robot_two:
continue
if str(robot_two) + str(robot_one) in distances:
continue
distance = (coordinates[robot_two][0] - coordinates[robot_one][0]) ** 2 + (
coordinates[robot_two][1] - coordinates[robot_one][1]
) ** 2
distances[str(robot_one) + str(robot_two)] = distance
print(f"{distances=}")
distances_list = list(distances.items())
distances_list.sort(key=lambda x: x[1])
return distances_list[0][1]
print(squared_shortest_distance(3, [0, 1, 2], [0, 1, 4])) # 2
print(squared_shortest_distance(3, [0, 2, 0], [0, 3, 0])) # 13