-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09.py
151 lines (102 loc) · 3.01 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
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
from itertools import islice
from enum import Enum
from typing import Generator, Tuple, Iterable
class Dir(Enum):
Up = 1
Down = 2
Left = 3
Right = 4
char_to_dir = {
"U": Dir.Up,
"D": Dir.Down,
"L": Dir.Left,
"R": Dir.Right,
}
Pos = Tuple[int, int]
Motion = Tuple[Dir, int]
def read_input(path) -> Generator[Motion, None, None]:
with open(path) as f:
for line in f.readlines():
direction, steps = line.split(" ")
yield char_to_dir[direction], int(steps)
def apply_motion(pos: Pos, motion: Motion) -> Generator[Pos, None, None]:
x, y = pos
match motion[0]:
case Dir.Up:
yield from ((x, y - i) for i in range(1, motion[1] + 1))
case Dir.Down:
yield from ((x, y + i) for i in range(1, motion[1] + 1))
case Dir.Left:
yield from ((x - i, y) for i in range(1, motion[1] + 1))
case Dir.Right:
yield from ((x + i, y) for i in range(1, motion[1] + 1))
def get_positions(
initial: Pos, motions: Iterable[Motion]
) -> Generator[Pos, None, None]:
current = initial
for motion in motions:
for pos in apply_motion(current, motion):
current = pos
yield current
def touching(head: Pos, tail: Pos) -> bool:
delta_x = head[0] - tail[0]
delta_y = head[1] - tail[1]
return abs(delta_x) <= 1 and abs(delta_y) <= 1
def sign(n: int) -> int:
if 0 <= n:
return 1
else:
return -1
def trail(head: Pos, tail: Pos) -> Pos:
if touching(head, tail):
return tail
delta_x = head[0] - tail[0]
delta_y = head[1] - tail[1]
if delta_y == 0:
return tail[0] + sign(delta_x), tail[1]
elif delta_x == 0:
return tail[0], tail[1] + sign(delta_y)
return tail[0] + sign(delta_x), tail[1] + sign(delta_y)
def test_trail():
head = 3, 1
tail = 1, 1
assert trail(head, tail) == (2, 1)
def test_trail2():
head = 1, 3
tail = 1, 1
assert trail(head, tail) == (1, 2)
def test_trail3():
head = 2, 1
tail = 1, 3
assert trail(head, tail) == (2, 2)
def test_trail4():
head = 3, 2
tail = 1, 3
assert trail(head, tail) == (2, 2)
def positions_visited(motions: Iterable[Motion], n_knots: int) -> int:
visited = set()
initial = 0, 0
knots = [initial] * n_knots
for head in get_positions(initial, motions):
for i, knot in enumerate(knots):
tracked = head if i == 0 else knots[i - 1]
knots[i] = trail(tracked, knot)
visited.add(knots[-1])
return len(visited)
def test_part2():
initial = 0, 0
motions = [
(Dir.Right, 5),
(Dir.Up, 8),
(Dir.Left, 8),
(Dir.Down, 3),
(Dir.Right, 17),
(Dir.Down, 10),
(Dir.Left, 25),
(Dir.Up, 20),
]
assert positions_visited(motions, 9) == 36
if __name__ == "__main__":
motions = list(read_input("./day09.txt"))
print(positions_visited(motions, 1))
print(positions_visited(motions, 9))