-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21-2.py
50 lines (36 loc) · 1.25 KB
/
21-2.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
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()
rows = len(lines)
columns = len(lines[0])
S = divmod(data.find("S"), rows + 1)
def loop(steps):
queue = deque([(*S, steps, 0, 0)])
seen = {(*S, 0, 0)}
res = 0
while queue:
row, column, steps, row_wraps, col_wraps = queue.popleft()
if not steps & 1:
res += 1
if not steps:
continue
for dr, dc in DIRECTIONS:
new_row_wraps, new_row = divmod(row + dr, rows)
new_col_wraps, new_column = divmod(column + dc, columns)
new_row_wraps += row_wraps
new_col_wraps += col_wraps
if (
lines[new_row][new_column] == "#"
or (new_row, new_column, new_row_wraps, new_col_wraps) in seen
):
continue
queue.append((new_row, new_column, steps - 1, new_row_wraps, new_col_wraps))
seen.add((new_row, new_column, new_row_wraps, new_col_wraps))
return res
n = 26501365 // rows
t1 = loop(S[0])
t2 = loop(S[0] + rows)
t3 = loop(S[0] + 2 * rows)
print((n**2 - n) * ((t3 + t1) // 2 - t2) + n * (t2 - t1) + t1)