Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Uncategorized category for The Shortest Knight's Path by madmanbob
def checkio(move_string):
board = [
[0, 3, 2, 3, 2, 3, 4, 5],
[3, 2, 1, 2, 3, 4, 3, 4],
[2, 1, 4, 3, 2, 3, 4, 5],
[3, 2, 3, 2, 3, 4, 3, 4],
[2, 3, 2, 3, 4, 3, 4, 5],
[3, 4, 3, 4, 3, 4, 5, 4],
[4, 3, 4, 3, 4, 5, 4, 5],
[5, 4, 5, 4, 5, 4, 5, 6]]
x1 = ord(move_string[0].lower()) - 97
y1 = ord(move_string[1]) - 49
x2 = ord(move_string[3].lower()) - 97
y2 = ord(move_string[4]) - 49
dx = abs(x1 - x2)
dy = abs(y1 - y2)
if (dx == dy == 1) and ((x1 % 7 == 0 and y1 % 7 == 0) or (x2 % 7 == 0 and y2 % 7 == 0)):
return 4
return board[dx][dy]
if __name__ == "__main__":
assert checkio("b1-d5") == 2, "First"
assert checkio("a6-b8") == 1, "Second"
assert checkio("h1-g2") == 4, "Third"
assert checkio("h8-d7") == 3, "Fourth"
assert checkio("a1-h8") == 6, "Fifth"
assert checkio("b2-a1") == 4, "to lower-left corner"
assert checkio("g7-h8") == 4, "to upper-right corner"
Feb. 15, 2014
Comments: