-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
60 lines (55 loc) · 1.69 KB
/
main.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
import collections
with open('test.txt') as file:
LINES = file.read().strip().splitlines()
# start_coords = None
# for x, row in enumerate(LINES):
# for y, char in enumerate(row):
# if char == 'S':
# start_coords = (x, y)
#
# queue = collections.deque()
# queue.append((start_coords, 1))
# HEIGHT = len(LINES)
# WIDTH = len(LINES[0])
# visited = set()
# seen = set()
# while queue:
# coords, steps = queue.popleft()
# x, y = coords
# for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
# nx, ny = x + dx, y + dy
# if 0 <= nx < HEIGHT and 0 <= ny < WIDTH and LINES[nx][ny] != '#':
# if steps == 64:
# visited.add((nx, ny))
# else:
# add = ((nx, ny), steps + 1)
# if add not in seen:
# queue.append(add)
# seen.add(add)
#
# print(len(visited))
start_coords = None
for x, row in enumerate(LINES):
for y, char in enumerate(row):
if char == 'S':
start_coords = (x, y)
queue = collections.deque()
queue.append((start_coords, 1))
HEIGHT = len(LINES)
WIDTH = len(LINES[0])
visited = set()
seen = set()
while queue:
coords, steps = queue.popleft()
x, y = coords
for dx, dy in ((0, 1), (1, 0), (0, -1), (-1, 0)):
nx, ny = x + dx, y + dy
if 0 <= nx < HEIGHT and 0 <= ny < WIDTH and LINES[nx][ny] != '#':
if steps == 64:
visited.add((nx, ny))
else:
add = ((nx, ny), steps + 1)
if add not in seen:
queue.append(add)
seen.add(add)
print(len(visited))