-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathfind.py
More file actions
197 lines (162 loc) · 4.52 KB
/
pathfind.py
File metadata and controls
197 lines (162 loc) · 4.52 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import hashlib
import itertools
from termcolor import colored
small_maze = """
|||||||||
|S |
| ||| |||
| |
| || || |
| |
| || ||||
| E|
|||||||||
"""
large_maze = """
|||||||||||||||||||||||||
|S || |||||| |
| |||| ||| ||||| || |
| | |||||| ||| |
| || | |||| ||| || | |
|||| ||| |||| | |
|| |||| || ||| |||||| |
|| | || | | |
|| || |||||| | | ||| ||||
| || |||||| | | | |
|| || || | | | ||||||
|| || || ||||| | | ||
|| || || | | ||| ||
|| |||| ||| |||| | | ||
|| | ||| | ||
|| | | ||||||||||| | | ||
| | | || | | | ||
| |||| || |||| | | | ||||
| | || || | ||| ||||
| | ||||||||| | || ||||
| | || || || |
| | ||||||||| |||||| |
| |||||||||| ||||| |
| |||| E|
|||||||||||||||||||||||||
"""
class Maze(object):
ascii_map = {
' ': 'space',
'|': 'wall',
'S': 'start',
'E': 'end',
}
def __init__(self, pic=large_maze):
self.solutions = []
grid = self.grid_from_ascii(pic=pic)
# TODO: do something smarter..
self.grid = [[ Cell(self, (x, y), char)
for x, char in enumerate(row) ]
for y, row in enumerate(grid) ]
def __str__(self):
return ''.join([
str(cell) for cell in self.flattened
])
@property
def flattened(self):
return itertools.chain(*self.grid)
def grid_from_ascii(self, pic):
'''
Returns a 2D matrix (grid representation) from a multiline ascii string.
Strips the first and last lines for better ascii art representation.
'''
return [[cell for cell in row] for row in pic.split('\n')][1:-1]
def print_grid(self, path=None):
'''
Prints a pretty representation of the grid to stdout.
'''
for row in self.grid:
print()
for cell in row:
if path and cell in path:
print(colored('.', 'red'), end='')
continue
print(cell, end='')
def cell_at(self, x, y):
return self.grid[y][x]
def solve(self):
self.start.pathfind([])
@property
def sha1(self):
h = hashlib.sha1()
h.update(str(self).encode('utf-8'))
return h.hexdigest()
class Cell(object):
colors = {
'S': 'green',
'E': 'red',
}
directions = [
( 0, -1), #up
( 0, 1), #down
(-1, 0), #left
( 1, 0), #right
]
def __init__(self, maze, coord, char=''):
self.maze = maze
self.coord = coord
self.char = char
self.x, self.y = self.coord
self._type = Maze.ascii_map[self.char]
# TODO: re-use the class' mapping
self.start = self.char is 'S'
self.end = self.char is 'E'
self.wall = self.char is '|'
if self.start:
self.maze.start = self
def __repr__(self):
return "Cell (%s, %s)" % self.coord
def __str__(self):
color = Cell.colors.get(self.char)
return colored(self.char, color)
def pathfind(self, path):
for b in self.choices(path):
if b.end:
self.maze.solutions.append(path + [self])
else:
b.pathfind(path + [self])
def choices(self, path):
'''
Return Cells that aren't walls and aren't in the path.
'''
return [ cell for cell in self.neighbors
if not cell.wall
and not cell in path ]
@property
def neighbors(self):
'''
Returns a list of (x, y) tuples for each orthogonally adjacent cell.
'''
cells = []
for direction in Cell.directions:
x = self.x + direction[0]
y = self.y + direction[1]
try:
cells.append(self.maze.cell_at(x, y))
except IndexError:
pass
return cells
def run():
'''
Consumes an ASCII grid representation and creates a 2D matrix.
Finds the start (sp) and end (ep) points and finds shortest path.
'''
maze = Maze()
maze.print_grid()
maze.solve()
solutions = maze.solutions
for s in solutions:
#print(s)
maze.print_grid(path=s)
print()
shortest = min(solutions, key=len)
print('Found %s solutions.\n' % len(solutions))
print('Shortest path (%s steps):' % len(shortest))
maze.print_grid(shortest)
if __name__ == "__main__":
run()