Unable to load image

Day 12

Post code, nerds.

14
Jump in the discussion.

No email address required.

>People are importing a library to do simple path-finding.

:#marseyphonecall:: Hello, cringe department? I'd like to make a report.

Jump in the discussion.

No email address required.

def bfs_extract_path(node, visited):
    path = []
    while node is not None:
        path.append(node)
        node = visited[node]
    path.reverse()
    return path


def bfs_full_step(front, visited, goal, neighbors: Callable):
    '''Returns a list of goals reached on this step'''
    sentinel = object()
    front.append(sentinel)
    goals: List[Any] = []
    while True:
        parent_node = front.popleft()
        if parent_node is sentinel:
            return goals
        for node in neighbors(parent_node):
            if node not in visited:
                visited[node] = parent_node
                front.append(node)
                if goal(node):
                    goals.append(node)


def bfs_full(start, goal: Callable, neighbors: Callable):
    '''Returns (goals, visited) or (None, visited) if goal unreachable or unspecified
    Always does the last step in full.
    '''
    visited = {s: None for s in start}
    front = deque(start)
    step = 1
    while front:
        found = bfs_full_step(front, visited, goal, neighbors)
        if found:
            return found, visited
        if not quiet: print(f'BFS: step={step}, visited={len(visited)}')
        step += 1
    return [], visited
Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.