-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.py
69 lines (62 loc) · 1.6 KB
/
21.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
from collections import deque
with open("21.txt", "r") as f:
lines = f.read().split("\n")
map = [list(line) for line in lines]
m = len(map)
n = len(map[0])
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
start_i = None
start_j = None
for i in range(m):
for j in range(n):
if map[i][j] == "S":
start_i = i
start_j = j
q = deque([(0, start_i, start_j)])
seen = set()
ans = set()
steps = 64
while q:
cnt, i, j = q.popleft()
if (i, j) in seen:
continue
seen.add((i, j))
if cnt % 2 == 0:
ans.add((i, j))
if cnt == steps:
continue
for di, dj in directions:
ii, jj = i + di, j + dj
if 0 <= ii < m and 0 <= jj < n and map[ii][jj] != "#" and (ii, jj) not in seen:
# if map[ii % m][jj] != "#" and (ii, jj) not in seen:
q.append((cnt + 1 ,ii, jj))
for i, j in ans:
map[i][j] = "O"
for row in map:
print("".join(row))
print(len(ans))
def f(steps):
q = deque([(0, start_i, start_j)])
seen = set()
ans = set()
while q:
cnt, i, j = q.popleft()
if (i, j) in seen:
continue
seen.add((i, j))
if cnt % 2 == steps % 2:
ans.add((i, j))
if cnt == steps:
continue
for di, dj in directions:
ii, jj = i + di, j + dj
if map[ii % m][jj % n] != "#" and (ii, jj) not in seen:
q.append((cnt + 1 ,ii, jj))
return len(ans)
print()
print(f(65 + 0 * 131))
print(f(65 + 1 * 131))
print(f(65 + 2 * 131))
print(f(65 + 3 * 131))
x = 26501365 // 131
print(3691 + 14694 * x + 14590 * x ** 2)