-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20.b.py
90 lines (80 loc) · 2.46 KB
/
20.b.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
with open("2024/20.input.txt", encoding="utf-8") as file:
data = file.read()
lines = data.splitlines()
lines = list(filter(lambda x: len(x) > 0, lines))
for y, line in enumerate(lines):
for x, char in enumerate(line):
if char == "S":
start = (x, y)
lines[y] = lines[y][:x] + "." + lines[y][x + 1 :]
if "end" in locals():
break
if char == "E":
end = (x, y)
lines[y] = lines[y][:x] + "." + lines[y][x + 1 :]
if "start" in locals():
break
distances: dict[tuple[int, int], int] = {}
pos = end
distance = 0
distances[pos] = distance
while pos != start:
distance += 1
if (
pos[1] > 0
and lines[pos[1] - 1][pos[0]] == "."
and (pos[0], pos[1] - 1) not in distances
):
pos = (pos[0], pos[1] - 1)
distances[pos] = distance
elif (
pos[1] < len(lines) - 1
and lines[pos[1] + 1][pos[0]] == "."
and (pos[0], pos[1] + 1) not in distances
):
pos = (pos[0], pos[1] + 1)
distances[pos] = distance
elif (
pos[0] > 0
and lines[pos[1]][pos[0] - 1] == "."
and (pos[0] - 1, pos[1]) not in distances
):
pos = (pos[0] - 1, pos[1])
distances[pos] = distance
elif (
pos[0] < len(lines[pos[1]]) - 1
and lines[pos[1]][pos[0] + 1] == "."
and (pos[0] + 1, pos[1]) not in distances
):
pos = (pos[0] + 1, pos[1])
distances[pos] = distance
else:
exit(1)
reachable: set[tuple[int, int, int]] = set()
for i in range(21):
for j in range(21 - i):
reachable.add((i, j, i + j))
reachable.add((-i, j, i + j))
reachable.add((i, -j, i + j))
reachable.add((-i, -j, i + j))
# def search(x: int, y: int, d: int) -> None:
# reachable.append((x, y, d))
# if d < 20:
# search(x + 1, y, d + 1)
# search(x, y + 1, d + 1)
# search(x - 1, y, d + 1)
# search(x, y - 1, d + 1)
# search(0, 0, 0)
total = 0
for y, line in enumerate(lines):
for x, char in enumerate(line):
if char == ".":
for dx, dy, d in reachable:
if (
0 <= x + dx < len(line)
and 0 <= y + dy < len(lines)
and lines[y + dy][x + dx] == "."
and distances[(x + dx, y + dy)] - distances[(x, y)] > 99 + d
):
total += 1
print(total)