-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.py
79 lines (65 loc) · 2.66 KB
/
9.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
# --- Day 9: Rope Bridge ---
# I have to simulate the movement of a robe and count the number of positions the rope's tail visits
# Open and read file
f = open("9_input.txt", "r")
lines = f.readlines()
f.close()
class Knot:
row = 0
col = 0
def simulate_rope(num_knots):
# Create rope
rope = []
for _ in range(num_knots):
rope.append(Knot())
visited_coordinates = []
for line in lines:
direction, steps = line.split()
for step in range(int(steps)):
# Move head
match direction:
case "U":
rope[0].row += 1
case "D":
rope[0].row -= 1
case "L":
rope[0].col -= 1
case "R":
rope[0].col += 1
# Store tail location
visited_coordinates.append((rope[-1].row, rope[-1].col))
# Move remainder of rope
# Not elegant but it works :)
for i in range(len(rope) - 1):
# Move knot to follow the knot in front of it
if (rope[i].row - rope[i + 1].row) > 1:
# Head is above tail
rope[i + 1].row += 1
if rope[i].col > rope[i + 1].col:
rope[i + 1].col += 1
elif rope[i].col < rope[i + 1].col:
rope[i + 1].col -= 1
elif (rope[i + 1].row - rope[i].row) > 1:
# Head is below tail
rope[i + 1].row -= 1
if rope[i].col > rope[i + 1].col:
rope[i + 1].col += 1
elif rope[i].col < rope[i + 1].col:
rope[i + 1].col -= 1
elif (rope[i].col - rope[i + 1].col) > 1:
# Head is right of tail
rope[i + 1].col += 1
if rope[i].row > rope[i + 1].row:
rope[i + 1].row += 1
elif rope[i].row < rope[i + 1].row:
rope[i + 1].row -= 1
elif (rope[i + 1].col - rope[i].col) > 1:
# Head is left of tail
rope[i + 1].col -= 1
if rope[i].row > rope[i + 1].row:
rope[i + 1].row += 1
elif rope[i].row < rope[i + 1].row:
rope[i + 1].row -= 1
return len(set(visited_coordinates))
print("Part one number of visited position:", simulate_rope(2))
print("Part two number of visited position:", simulate_rope(10) + 1)