-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_83.py
56 lines (46 loc) · 2.15 KB
/
problem_83.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Challenge 83 of project euler - Path Sum: Four Ways
@author Ori Dabush
"""
from utils.inputs import get_input
from itertools import product
from math import inf
from collections import defaultdict
def find_minimal_path_sum(matrix: tuple[tuple[int, ...], ...]) -> int:
"""
An implementation of Dijkstra's algorithm to the shortest path problem.
The nodes are the matrix tiles and the weight of an edge from m[i,j] to m[k,l] is the value of m[k,l].
"""
rows, columns = len(matrix), len(matrix[0])
unvisited_nodes = list(product(range(rows), range(columns)))
distances = defaultdict(lambda: inf)
initial_node, target_node = (0, 0), (rows - 1, columns - 1)
# The initial distance is the value of the initial tile and not 0 because it will be part of the path weight
distances[initial_node] = matrix[0][0]
while target_node in unvisited_nodes and min(unvisited_nodes, key=distances.__getitem__) != inf:
# Get the node with minimal tentative distance
current_node = min(unvisited_nodes, key=distances.__getitem__)
unvisited_nodes.remove(current_node)
row, column = current_node
# Update the neighbors if needed
if row > 0:
neighbor = (row - 1, column)
distances[neighbor] = min(distances[neighbor], distances[current_node] + matrix[row - 1][column])
if row < rows - 1:
neighbor = (row + 1, column)
distances[neighbor] = min(distances[neighbor], distances[current_node] + matrix[row + 1][column])
if column > 0:
neighbor = (row, column - 1)
distances[neighbor] = min(distances[neighbor], distances[current_node] + matrix[row][column - 1])
if column < columns - 1:
neighbor = (row, column + 1)
distances[neighbor] = min(distances[neighbor], distances[current_node] + matrix[row][column + 1])
return int(distances[target_node])
def solve():
matrix = get_input(83)
matrix = tuple(tuple(map(int, line.split(','))) for line in matrix.split())
return find_minimal_path_sum(matrix)
def main():
print(f'The answer is {solve()}')
if __name__ == '__main__':
main()