-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
68 lines (66 loc) · 1.91 KB
/
day9.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
from pathlib import Path
movements = [
(cols[0], int(cols[1]))
for line in Path("input9.txt").read_text().splitlines()
if (cols := line.split())
]
t_x = 0
t_y = 0
h_x = 0
h_y = 0
visited_positions = set((t_x, t_y))
for (direction, n) in movements:
for _ in range(n):
match direction:
case "R":
h_x += 1
case "U":
h_y -= 1
case "D":
h_y += 1
case "L":
h_x -= 1
case _:
raise ValueError("Invalid move: {direction}")
dist_x = h_x - t_x
dist_y = h_y - t_y
match (dist_x, dist_y):
case (2, 0):
t_x += 1
visited_positions.add((t_x, t_y))
case (-2, 0):
t_x -= 1
visited_positions.add((t_x, t_y))
case (2, 1) | (1, 2):
t_x += 1
t_y += 1
visited_positions.add((t_x, t_y))
case (2, -1) | (1, -2):
t_x += 1
t_y -= 1
visited_positions.add((t_x, t_y))
case (0, 2):
t_y += 1
visited_positions.add((t_x, t_y))
case (0, -2):
t_y -= 1
visited_positions.add((t_x, t_y))
case (-1, 2) | (-2, 1):
t_x -= 1
t_y += 1
visited_positions.add((t_x, t_y))
case (-2, -1) | (-1, -2):
t_x -= 1
t_y -= 1
visited_positions.add((t_x, t_y))
case (1, 1) | (-1, -1) | (0, 1) | (1, 0) | (0, -1) | (-1, 0) | (1, -1) | (
0,
0,
) | (
-1,
1,
):
pass
case _:
raise ValueError(f"Invalid move {direction} {dist_x, dist_y}")
print(len(visited_positions))