Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Shortest Knight's Path by sawako.oono
from collections import namedtuple
def checkio(cells):
Point=namedtuple("P",["x","y"])
start=Point(ord(cells[0])-96,int(cells[1]))
end=Point(ord(cells[3])-96,int(cells[4]))
count=0
path=[[start]]
while end not in path[-1]:
last=[]
for p in path[-1]:
for X,Y in ((2,1),(2,-1),(-2,1),(-2,-1),(1,2),(1,-2),(-1,2),(-1,-2)):
if 1<=p.x+X<=8 and 1<=p.y+Y<=8:
last.append(Point(p.x+X,p.y+Y))
path.append(last)
count+=1
return count
if __name__ == "__main__":
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio("b1-d5") == 2, "1st example"
assert checkio("a6-b8") == 1, "2nd example"
assert checkio("h1-g2") == 4, "3rd example"
assert checkio("h8-d7") == 3, "4th example"
assert checkio("a1-h8") == 6, "5th example"
July 17, 2021