-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.py
69 lines (47 loc) · 1.38 KB
/
maze.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
import sys
from astar import astar
def build_maze(text):
"""Builds maze list from input string """
maze = []
lines = text.split()
for line in lines:
row = []
row[:] = line
maze.append(row)
return maze
def find_start(maze):
"""Finds entrace at the top"""
return 0, maze[0].index('_')
def find_end(maze):
"""Finds exit at the bottom row"""
return len(maze)-1, maze[-1].index('_')
def build_solved_maze(maze, path):
"""Fills maze with alphabet path using list of coords touples as solved path"""
if path is None:
print("Doesn't seem like this maze has a solution.")
return []
alphabet = list(map(chr, range(97, 123)))
for i, step in enumerate(path):
x = step[0]
y = step[1]
letter = i % len(alphabet)
maze[x][y] = alphabet[letter]
return maze
def print_solved_maze(solved_maze):
"""Iterates through the solved maze list and prints it out"""
print()
for row in solved_maze:
for cell in row:
print(cell, end="")
print()
print()
def main():
text = sys.stdin.read()
maze = build_maze(text)
start = find_start(maze)
end = find_end(maze)
path = astar(maze, start, end)
solved_maze = build_solved_maze(build_maze(text), path)
print_solved_maze(solved_maze)
if __name__ == '__main__':
main()