Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Pawn Brotherhood by waxdesf
def safe_pawns(pawns):
# horizontal values
h = [x for x in range(97,105)]
# vertical values
v = [x for x in range(1, 9)]
# grid, tuples
grid = [list(zip([x]*8,v)) for x in h]
# grid default dictionary (x, y) = False
dgrid = {(w,z):False for row in grid for w, z in row}
#convert pawns str to tuple ("a1" -> (97, 1)
split = lambda x: (ord(x[0]), int(x[1]))
# set True in grid dict for each pawn
for p in pawns:
dgrid[split(p)] = True
c=0
for p in pawns:
s = split(p)
downleft = (s[0]-1, s[1]-1)
downrigh = (s[0]+1, s[1]-1)
# if pawn in downright or downleft, pawn is safe
if (downleft in dgrid and dgrid[downleft]) or (downrigh in dgrid and dgrid[downrigh]):
c+=1
return c
May 25, 2015
Comments: