-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.py
executable file
·83 lines (64 loc) · 1.99 KB
/
day3.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
#!/Users/thangdo/Documents/dev/csessh/bin/python
from typing import Tuple
with open("test.txt", "r") as f:
paths = [path.strip().split(",") for path in f.readlines()]
def traverse(steps: list) -> set:
steps_taken = []
x = 0
y = 0
for step in steps:
direction = step[0]
distance = int(step[1:])
for i in range(0, distance):
if direction == "R":
x += 1
elif direction == "L":
x -= 1
elif direction == "U":
y += 1
elif direction == "D":
y -= 1
steps_taken.append(
(
x, # location x
y, # location y
abs(x) + abs(y), # distance to center
)
)
return set(steps_taken)
def retrace(steps: list, destination: Tuple[int, int, int]) -> int:
x = 0
y = 0
count = 0
for step in steps:
direction = step[0]
distance = int(step[1:])
for i in range(0, distance):
if direction == "R":
x += 1
elif direction == "L":
x -= 1
elif direction == "U":
y += 1
elif direction == "D":
y -= 1
count += 1
if x == destination[0] and y == destination[1]:
return count
steps_taken_by_A = traverse(paths[0])
steps_taken_by_B = traverse(paths[1])
cross_overs = list(steps_taken_by_A.intersection(steps_taken_by_B))
cross_overs.sort(key=lambda x: x[2], reverse=False)
# Part 1
print(cross_overs[0][2])
# Part 2
minimum_distance = None
for location in cross_overs:
steps_retraced_by_A = retrace(paths[0], location)
steps_retraced_by_B = retrace(paths[1], location)
if (
minimum_distance is None
or steps_retraced_by_A + steps_retraced_by_B < minimum_distance
):
minimum_distance = steps_retraced_by_A + steps_retraced_by_B
print(minimum_distance)