day15/part1.py
def dijkstra(grid, start, end):
    """Dijsktra algorithm to find the shortest path.

    Returns map with visited nodes as keys and the shortest path possible to them.
    """
    unvisited = {(x, y): math.inf for x in range(WIDTH) for y in range(HEIGHT)}
    visited = {}

    current = start
    current_risk = 0
    while True:
        x, y = current
        for x2, y2 in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
            if (x2, y2) not in unvisited or not (0 <= x2 < WIDTH and 0 <= y2 < HEIGHT):
                continue
            risk = grid[y2][x2]
            new_risk = current_risk + risk
            if unvisited[(x2, y2)] > new_risk:
                unvisited[(x2, y2)] = new_risk

        visited[current] = current_risk
        del unvisited[current]
        if current == end or not unvisited:
            break
        candidates = dict(filter(lambda item: item[1] < math.inf, unvisited.items()))
        current = min(candidates, key=candidates.get)
        current_risk = unvisited[current]

    return visited
Dijsktra algorithm to find the shortest path.

    Returns map with visited nodes as keys and the shortest path possible to them.
    
Dijsktra algorithm to find the shortest path.
Returns map with visited nodes as keys and the shortest path possible to them.
Dijsktra algorithm to find the shortest path . Returns map with visited nodes as keys and the shortest path possible to them .
def dijkstra(grid, start, end):
    unvisited = {(x, y): math.inf for x in range(WIDTH) for y in range(HEIGHT)}
    visited = {}
    current = start
    current_risk = 0
    while True:
        x, y = current
        for x2, y2 in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
            if (x2, y2) not in unvisited or not (0 <= x2 < WIDTH and 0 <= y2 < HEIGHT):
                continue
            risk = grid[y2][x2]
            new_risk = current_risk + risk
            if unvisited[(x2, y2)] > new_risk:
                unvisited[(x2, y2)] = new_risk
        visited[current] = current_risk
        del unvisited[current]
        if current == end or not unvisited:
            break
        candidates = dict(filter(lambda item: item[1] < math.inf, unvisited.items()))
        current = min(candidates, key=candidates.get)
        current_risk = unvisited[current]
    return visited
def dijkstra ( grid , start , end ) : unvisited = { ( x , y ) : math . inf for x in range ( WIDTH ) for y in range ( HEIGHT ) } visited = { } current = start current_risk = 0 while True : x , y = current for x2 , y2 in ( ( x + 1 , y ) , ( x - 1 , y ) , ( x , y + 1 ) , ( x , y - 1 ) ) : if ( x2 , y2 ) not in unvisited or not ( 0 <= x2 < WIDTH and 0 <= y2 < HEIGHT ) : continue risk = grid [ y2 ] [ x2 ] new_risk = current_risk + risk if unvisited [ ( x2 , y2 ) ] > new_risk : unvisited [ ( x2 , y2 ) ] = new_risk visited [ current ] = current_risk del unvisited [ current ] if current == end or not unvisited : break candidates = dict ( filter ( lambda item : item [ 1 ] < math . inf , unvisited . items ( ) ) ) current = min ( candidates , key = candidates . get ) current_risk = unvisited [ current ] return visited
Dijsktra algorithm to find the shortest path.
Dijsktra algorithm to find the shortest path .
"""Dijsktra algorithm to find the shortest path.

    Returns map with visited nodes as keys and the shortest path possible to them.
    """
grid
start
end