-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday_01.py
36 lines (27 loc) · 988 Bytes
/
day_01.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
with open('./inputs/01.txt', 'r') as infile:
directions = infile.readline().split(', ')
ROTATION = {
'L': 1j,
'R': -1j
}
current_direction = 1j
location = 0+0j
visited_locations = set()
passed_twice = False
def find_manhattan(loc):
return int(abs(loc.real) + abs(loc.imag))
for instruction in directions:
rot, dist = instruction[0], int(instruction[1:])
current_direction *= ROTATION[rot]
for _ in range(dist):
location += current_direction
if not passed_twice and location in visited_locations:
print("This looks familiar! "
f"I must have been at {location} before!")
print("The distance from the start is:", find_manhattan(location))
passed_twice = True
else:
visited_locations.add(location)
print('....')
print("Ok, I've come to the end of your instructions and I'm at:", location)
print(f"That's {find_manhattan(location)} blocks away from the the start.")