DEV Community

Discussion on: Daily Challenge #116 - Shortest Knight Path

Collapse
 
jbristow profile image
Jon Bristow

inelegant python brute force solution

def parse(s):
    return (ord(s[0]) - ord('a'), int(s[1]))

def move(start, end):
    return move_inner({parse(start)}, parse(end))


def next_moves(positions):
    moves = set()
    for move in positions:
        for position in get_moves(move):
            if (
                position[0] >= 0
                and position[1] >= 0
                and position[0] < 8
                and position[1] < 8
            ):
                moves.add(position)
    return moves


def move_inner(from_start, end, n=1):
    print(from_start)
    nstart = from_start | next_moves(from_start)

    if end in nstart:
        return n
    else:
        return move_inner(nstart, end, n + 1)


def get_moves(point):
    (x, y) = point
    return [
        (x + 2, y + 1),
        (x + 2, y - 1),
        (x - 2, y + 1),
        (x - 2, y - 1),
        (x + 1, y + 2),
        (x + 1, y - 2),
        (x - 1, y + 2),
        (x - 1, y - 2),
    ]