-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09_p1.py
36 lines (29 loc) · 1.13 KB
/
day09_p1.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
def is_adjacent(head, tail):
return abs(head[0] - tail[0]) < 2 and abs(head[1] - tail[1]) < 2
def main():
file_lines = open('day9_input.txt', 'r').readlines()
positions_visited, head, tail = {(0, 0)}, (0, 0), (0, 0)
for line in file_lines:
line = line.strip()
direction, num = line.split()
for i in range(int(num)):
if line[0] == 'R':
head = head[0] + 1, head[1]
if not is_adjacent(head, tail):
tail = head[0] - 1, head[1]
if line[0] == 'L':
head = head[0] - 1, head[1]
if not is_adjacent(head, tail):
tail = head[0] + 1, head[1]
if line[0] == 'U':
head = head[0], head[1] + 1
if not is_adjacent(head, tail):
tail = head[0], head[1] - 1
if line[0] == 'D':
head = head[0], head[1] - 1
if not is_adjacent(head, tail):
tail = head[0], head[1] + 1
positions_visited.add(tail)
print(len(positions_visited))
if __name__ == '__main__':
main()