-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.py
78 lines (62 loc) · 1.6 KB
/
day09.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
from day import Day
from aocd import submit
def parse(data):
directions = {"R": 1 + 0j, "L": -1 + 0, "U": 0 + 1j, "D": 0 - 1j}
return directions[data[0]], data[1]
def movement(rope, direction, steps, visited=set()):
for _ in range(steps):
rope[0] += direction
for i in range(len(rope) - 1):
head, tail = rope[i], rope[i + 1]
diff = tail - head
if abs(diff) >= 2:
if diff.real != 0:
tail = tail.real - (diff.real / abs(diff.real)) + tail.imag * 1j
if diff.imag != 0:
tail = tail.real + (tail.imag - (diff.imag / abs(diff.imag))) * 1j
rope[i + 1] = tail
visited.add(rope[-1])
return rope
def wiggle(data, rope_length):
rope = [0j] * rope_length
visited = set([0])
for direction, steps in data:
rope = movement(rope, direction, steps, visited)
visited.add(rope[-1])
return visited
def main(day, part=1):
day.parse_list_of_lists(sep2=" ", sep="\n", typing=(str, int))
day.apply(parse)
if part == 1:
return len(wiggle(day.data, rope_length=2))
if part == 2:
return len(wiggle(day.data, rope_length=10))
if __name__ == "__main__":
day = Day(9)
day.download()
data = """R 4
U 4
L 3
D 1
R 4
D 1
L 5
R 2"""
# day.load(data)
day.load()
p1 = main(day)
print(p1)
submit(p1, part="a", day=9, year=2022)
data = """R 5
U 8
L 8
D 3
R 17
D 10
L 25
U 20"""
# day.load(data)
day.load()
p2 = main(day, part=2)
print(p2)
submit(p2, part="b", day=9, year=2022)