#For debugging purposes import time import time #Starting point for the search tosearch=[[0,0]] #The points that can be reached by the monkey are added into this list confirmed=[] #Main loop that goes through the list of points to be searched for point in tosearch: if point not in confirmed: #Count the sum of the digits digits=str(abs(point[0]))+str(abs(point[1])) sum=0 for i in digits: sum+=int(i) #Here it gets weird. See thread for details. if abs(sum)<=19: confirmed.append(point) tosearch.append([point[0],point[1]-1]) tosearch.append([point[0]+1,point[1]]) tosearch.append([point[0]-1,point[1]-1]) tosearch.append([point[0],point[1]+1]) tosearch.remove(point) else: tosearch.remove(point) #For debugging #print (tosearch[-5:-1]) #print (confirmed[-1]) #time.sleep(0.2) else: tosearch.remove(point) print (len(confirmed))