Skip to content

Commit 1eecc0b

Browse files
committed
Pathfinding: added ability to add walls + various fixes
1 parent 3d6a72f commit 1eecc0b

File tree

1 file changed

+34
-48
lines changed

1 file changed

+34
-48
lines changed

2018/pathfinding.py

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,6 @@
11
import heapq
22

3-
# Cardinal directions
4-
north = 1j
5-
south = -1j
6-
west = -1
7-
east = 1
8-
northeast = 1 + 1j
9-
northwest = -1 + 1j
10-
southeast = 1 - 1j
11-
southwest = -1 - 1j
12-
13-
directions_straight = [north, south, west, east]
14-
directions_diagonals = directions_straight + [
15-
northeast,
16-
northwest,
17-
southeast,
18-
southwest,
19-
]
20-
21-
22-
def min_real(complexes):
23-
real_values = [x.real for x in complexes]
24-
return min(real_values)
25-
26-
27-
def min_imag(complexes):
28-
real_values = [x.imag for x in complexes]
29-
return min(real_values)
30-
31-
32-
def max_real(complexes):
33-
real_values = [x.real for x in complexes]
34-
return max(real_values)
35-
36-
37-
def max_imag(complexes):
38-
real_values = [x.imag for x in complexes]
39-
return max(real_values)
3+
from complex_utils import *
404

415

426
class TargetFound(Exception):
@@ -121,21 +85,20 @@ def grid_search(self, grid, items):
12185
"""
12286
Searches the grid for some items
12387
124-
:param string grid: The grid to convert
125-
:param Boolean items: Whether diagonal movement is allowed
88+
:param string grid: The grid in which to search
89+
:param Boolean items: The items to search
12690
:return: True if the grid was converted
12791
"""
12892
items_found = {}
12993
y = 0
13094

131-
for line in grid.splitlines():
95+
for y, line in enumerate(grid.splitlines()):
13296
for x in range(len(line)):
13397
if line[x] in items:
13498
if line[x] in items_found:
13599
items_found[line[x]].append(x - y * 1j)
136100
else:
137101
items_found[line[x]] = [x - y * 1j]
138-
y += 1
139102

140103
return items_found
141104

@@ -171,18 +134,41 @@ def vertices_to_grid(self, mark_coords=[], wall="#"):
171134

172135
return grid
173136

174-
def add_traps(self, vertex):
137+
def add_traps(self, vertices):
175138
"""
176139
Creates traps: places that can be reached, but not exited
177140
178-
:param Any vertex: The vertex to consider
141+
:param Any vertex: The vertices to consider
179142
:return: True if successful, False if no vertex found
180143
"""
181-
if vertex in self.edges:
182-
del self.edges[vertex]
183-
return True
184-
else:
185-
return False
144+
changed = False
145+
for vertex in vertices:
146+
if vertex in self.edges:
147+
del self.edges[vertex]
148+
changed = True
149+
150+
return changed
151+
152+
def add_walls(self, vertices):
153+
"""
154+
Adds walls - useful for modification of map
155+
156+
:param Any vertex: The vertices to consider
157+
:return: True if successful, False if no vertex found
158+
"""
159+
changed = False
160+
for vertex in vertices:
161+
if vertex in self.edges:
162+
del self.edges[vertex]
163+
del self.vertices[vertex]
164+
changed = True
165+
166+
self.edges = {
167+
source: [target for target in self.edges[source] if target not in vertices]
168+
for source in self.edges
169+
}
170+
171+
return changed
186172

187173
def depth_first_search(self, start, end=None):
188174
"""

0 commit comments

Comments
 (0)