This repository is dedicated to archiving my solutions for questions in Advent of Code 2021, separating each question to each folder by its released date.
Read it at your own risk.
- Day 15
- First approach: Dijkstra + heapq
- Second approach: networkx
G = nx.Graph() h, w = len(maps), len(maps[0]) for r in range(h): for c in range(w): for e in list(filter( lambda p: 0 <= p[0] < h and 0 <= p[1] < w, [(r-1, c), (r+1, c), (r, c-1), (r, c+1)] )): G.add_edge((r, c), e) # check weight here instead of assign while adding edge weight_func = lambda u,v,d: maps[v[0]][v[1]] res = nx.dijkstra_path(G, (0, 0), (h-1, w-1), weight=weight_func) print(reduce(lambda acc, p: acc+maps[p[0]][p[1]], res[1:], 0))