-
Notifications
You must be signed in to change notification settings - Fork 0
/
21-1.py
39 lines (30 loc) · 855 Bytes
/
21-1.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
from collections import deque
DIRECTIONS = ((-1, 0), (1, 0), (0, -1), (0, 1))
with open("input.txt") as f:
data = f.read()
lines = data.splitlines()
S = divmod(data.find("S"), len(lines) + 1)
queue = deque([(*S, 64)])
seen = {S}
res = 0
while queue:
row, column, steps = queue.popleft()
if not steps & 1:
res += 1
if not steps:
continue
for dr, dc in DIRECTIONS:
new_row = row + dr
new_column = column + dc
if (
new_row < 0
or new_row >= len(lines)
or new_column < 0
or new_column >= len(lines[0])
or lines[new_row][new_column] == "#"
or (new_row, new_column) in seen
):
continue
queue.append((new_row, new_column, steps - 1))
seen.add((new_row, new_column))
print(res)