-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRopeBridge.py
154 lines (135 loc) · 4.51 KB
/
RopeBridge.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/python3
"""Template.py
Author: Bissallah Ekele - bAe
Date: 13/12/2022
Description:
"""
# Modeling rope physics - figure out where not to step
# Knot determines head and tail of rope
# Nebulous reasoning involving Plank Lengths
# Series of head motions - puzzle input
# Determine how tail will move
#
# H and T always touching, diagonally adjacent or overlapping
#
# Pseudocode
# R, H -> y R
# T.check(H)
# if False, T.set_x(R-1)
# T.track((yR-1)) --> set.add()
#
# U, H -> U x
# T.check(H)
# if False, T.set_y(U-1), T.set_u(x-1)
# T.track
class Knot:
def __init__(self, name):
self.name = name
self.x = self.y = 0
self.track = None
def is_vertical(self, knot):
return (
abs(self.y - knot.y) <= 1 and
self.x == knot.x
)
def is_horizontal(self, knot):
return (
abs(self.x - knot.x) <= 1 and
self.y == knot.y
)
def is_diagonal(self, knot):
return (
abs(self.x - knot.x) <= 1 and
abs(self.y - knot.y) <= 1
)
def move_x(self, steps):
self.x += steps
def move_y(self, steps):
self.y += steps
def get_part_1(input_list):
tail_cells = set()
head = Knot("H")
tail = Knot("T")
print(head.x, head.y, tail.x, tail.y)
for motion in input_list:
direction, steps = motion.split(" ")
steps = int(steps)
if direction == "R":
for step in range(1, steps+1):
head.move_x(1)
if not tail.is_horizontal(head) and not tail.is_diagonal(head):
if tail.y == head.y:
tail.move_x(1)
else:
tail.move_x(1)
if tail.y < head.y:
tail.move_y(1)
else:
tail.move_y(-1)
tail_cells.add((tail.x,tail.y))
print("R", step, "-->", head.x, head.y, tail.x, tail.y)
if direction == "L":
for step in range(1, steps+1):
head.move_x(-1)
if not tail.is_horizontal(head) and not tail.is_diagonal(head):
if tail.y == head.y:
tail.move_x(-1)
else:
tail.move_x(-1)
if tail.y < head.y:
tail.move_y(1)
else:
tail.move_y(-1)
tail_cells.add((tail.x,tail.y))
print("L", step, "-->", head.x, head.y, tail.x, tail.y)
if direction == "U":
for step in range(1, steps+1):
head.move_y(1)
if not tail.is_vertical(head) and not tail.is_diagonal(head):
if tail.x == head.x:
tail.move_y(1)
else:
tail.move_y(1)
if tail.x < head.x:
tail.move_x(1)
else:
tail.move_x(-1)
tail_cells.add((tail.x,tail.y))
print("U", step, "-->", head.x, head.y, tail.x, tail.y)
if direction == "D":
for step in range(1, steps+1):
head.move_y(-1)
if not tail.is_vertical(head) and not tail.is_diagonal(head):
if tail.x == head.x:
tail.move_y(-1)
else:
tail.move_y(-1)
if tail.x < head.x:
tail.move_x(1)
else:
tail.move_x(-1)
tail_cells.add((tail.x,tail.y))
print("D", step, "-->", head.x, head.y, tail.x, tail.y)
print(tail_cells)
tail.track = tail_cells
return len(tail.track)
def get_part_2(input_list):
# Psuedocode
# Link-list of 10 named knots
# For each motion
# Sliding window to move in twos through link-list
# Store visited cell in tail(two-of-one) track-set
# Return track-set size of 10th knot (named "9")
pass
def main():
test_input = "test-input"
puzzle_input = "puzzle-input"
file_name = puzzle_input #test_input
with open(file_name) as f:
input_list = f.read().splitlines()
print("Part_1: "
+ str(get_part_1(input_list))
+ "\nPart_2: "
+ str(get_part_2(input_list)))
if __name__ == "__main__":
main()