-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday09.py
More file actions
57 lines (35 loc) · 1.2 KB
/
Copy pathday09.py
File metadata and controls
57 lines (35 loc) · 1.2 KB
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
#!/usr/bin/env python3
"""
Snake.
Timing:
python3 = ~90 ms
"""
from aoc import map_list, read_input, sign
# ------------------------------------------------------------------------------
def move_snake(data: list, tail_size: int) -> set:
V = {"R": 1, "D": -1j, "L": -1, "U": 1j}
snake = [0] * (1 + tail_size)
tail_pos = set()
for direction, amount in data:
for _ in range(int(amount)):
snake[0] += V[direction]
for i, tail in enumerate(snake[1:], 1):
diff = snake[i - 1] - tail
if abs(diff) >= 2:
tail += complex(sign(diff.real), sign(diff.imag))
snake[i] = tail
tail_pos.add(snake[-1])
return tail_pos
def solve(day=9, test=False):
txt = read_input(day, test).splitlines()
data = map_list(str.split, txt)
part1 = len(move_snake(data, 1))
part2 = len(move_snake(data, 9))
return part1, part2
# ------------------------------------------------------------------------------
# res = solve(test=True)
# assert res == (13, 1)
res = solve()
print(*res)
assert res == (6406, 2643)
# ------------------------------------------------------------------------------