Code:

'''
Problem: find a path from (0,0) to (0,7).
ones are tiles. zeros are illegal squares.
tiles are square (boring!) and connect with
adjacent tiles sharing an edge.
GRAPH NB. with coordinates.
1 1 1 1 1 1 | 7
1 0 1 0 1 1 | 6
1 1 1 1 1 1 | 5 ^
0 1 1 0 1 1 | 4 | y
1 1 1 1 1 1 | 3
1 1 0 1 1 1 | 2
1 1 1 1 1 1 | 1
1 1 1 1 0 0 | 0
-----------
0 1 2 3 4 5 x->
+/,GRAPH NB. there are 41 tiles
41
'''
import pprint
class Graph:
def __init__(self,g):
self.g = g
def __getitem__(self,item):
(x, y) = (item.x, item.y)
g = self.g
Y = y
for dx in (-1, 1):
X = x+dx
if -1 < X:
try:
if g[Y][X]:
yield Tile(X,Y)
except IndexError:
pass
X = x
for dy in (-1, 1):
Y = y+dy
if -1 < Y:
try:
if g[Y][X]:
yield Tile(X,Y)
except IndexError:
pass
class Tile:
def __init__(self,x,y):
self.x = x
self.y = y
self.H = 0
self.parent = None
self.hash = hash((x,y))
def __str__(self):
return 'Tile({},{})'.format(self.x,self.y)
def __hash__(self):
return self.hash
def __eq__(self,other):
try:
return (self.x == other.x) and (self.y == other.y)
except:
pass
raise TypeError('comparing {} and {} failed'.format(self,other))
#import pdb
def aStar(graph, current, end):
openList = set()
closedList = set()
def retracePath(c):
def parentgen(c):
while c:
yield c
c = c.parent
result = [element for element in parentgen(c)]
result.reverse()
return result
openList.add(current)
#pdb.set_trace()
while openList:
current = sorted(openList, key=lambda inst:inst.H)[0]
if current == end:
return retracePath(current)
openList.remove(current)
closedList.add(current)
for tile in graph[current]:
if tile not in closedList:
tile.H = (abs(end.x-tile.x)+abs(end.y-tile.y))
openList.add(tile)
tile.parent = current
return []
g = Graph((
(1,1,1,1,0,0,),
(1,1,1,1,1,1,),
(1,1,0,1,1,1,),
(1,1,1,1,1,1,),
(0,1,1,0,1,1,),
(1,1,1,1,1,1,),
(1,0,1,0,1,1,),
(1,1,1,1,1,1,),),)
pprint.pprint(list(map(str,aStar(g,Tile(0,0),Tile(0,7)))))
# output
#['Tile(0,0)',
# 'Tile(0,1)',
# 'Tile(0,2)',
# 'Tile(0,3)',
# 'Tile(1,3)',
# 'Tile(1,4)',
# 'Tile(1,5)',
# 'Tile(0,5)',
# 'Tile(0,6)',
# 'Tile(0,7)']

agrees : Awesome help! Thanks a ton for your advice and showing the data types for this code. I did notice it's a lot like the A* pseudocode on wikipedia which is why I wanted to use it, since it's already written in Python and it was optimized. Ty again!