-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart_two.py
51 lines (39 loc) · 1.12 KB
/
part_two.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
import sys
from part_one import get_all_wire_path_positions
def get_steps_for_crossings(
wire_path: list[str],
crossings: set[tuple[int, int]]
) -> dict[tuple[int, int], int]:
crossing_dict = {}
distance = 0
x, y = 0, 0
for i in range(len(wire_path)):
for _ in range(int(wire_path[i][1:])):
direction = wire_path[i][0]
if direction == "R":
x += 1
elif direction == "L":
x -= 1
elif direction == "D":
y += 1
elif direction == "U":
y -= 1
else:
sys.exit('Something weird happened.')
distance += 1
if (x, y) in crossings:
crossing_dict[(x, y)] = distance
return crossing_dict
def steps_to_best_intersection(
wire_1_path: list[str],
wire_2_path: list[str]
) -> int:
wire_1_positions = get_all_wire_path_positions(wire_1_path)
wire_2_positions = get_all_wire_path_positions(wire_2_path)
crossings = wire_1_positions.intersection(wire_2_positions)
crossing_dict_1 = get_steps_for_crossings(wire_1_path, crossings)
crossing_dict_2 = get_steps_for_crossings(wire_2_path, crossings)
return min(
crossing_dict_1[crossing] + crossing_dict_2[crossing]
for crossing in crossings
)