def find_distances(grid2d, node_ch, node_pos):
    """
    Find the distances using BFS from node_ch to the next reachable node (key or door).
    """
    distances = dict()
    node_neighbours = set()

    deltas = [(-1, 0), (1, 0), (0, -1), (0, 1)]

    q = list()
    q.append((0, node_pos))
    visited = set()

    while len(q) > 0:
        d, pos = q.pop(0)
        visited.add(pos)

        # find neighbours
        y, x = pos
        for dy, dx in deltas:
            neighbour_pos = y + dy, x + dx
            neighbour_ch = grid2d[neighbour_pos]

            # only move to a next nodes if it's an empty space
            if neighbour_ch == '.' and neighbour_pos not in visited:
                q.append((d + 1, neighbour_pos))

            # we don't want to store the empty cells or doors as nodes
            if neighbour_ch not in ['.', '#'] and neighbour_pos not in visited:
                distances[(node_ch, neighbour_ch)] = d + 1
                node_neighbours.add(neighbour_ch)

    return distances, node_neighbours