Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 069a14b

Browse files
MaximSmolskiygithub-actions
and
github-actions
authoredMar 2, 2023
Add Project Euler problem 082 solution 1 (TheAlgorithms#6282)
Update DIRECTORY.md --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 64543fa commit 069a14b

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed
 

‎DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,8 @@
918918
* [Sol1](project_euler/problem_080/sol1.py)
919919
* Problem 081
920920
* [Sol1](project_euler/problem_081/sol1.py)
921+
* Problem 082
922+
* [Sol1](project_euler/problem_082/sol1.py)
921923
* Problem 085
922924
* [Sol1](project_euler/problem_085/sol1.py)
923925
* Problem 086

‎project_euler/problem_082/__init__.py

Whitespace-only changes.

‎project_euler/problem_082/input.txt

Lines changed: 80 additions & 0 deletions
Large diffs are not rendered by default.

‎project_euler/problem_082/sol1.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Project Euler Problem 82: https://projecteuler.net/problem=82
3+
4+
The minimal path sum in the 5 by 5 matrix below, by starting in any cell
5+
in the left column and finishing in any cell in the right column,
6+
and only moving up, down, and right, is indicated in red and bold;
7+
the sum is equal to 994.
8+
9+
131 673 [234] [103] [18]
10+
[201] [96] [342] 965 150
11+
630 803 746 422 111
12+
537 699 497 121 956
13+
805 732 524 37 331
14+
15+
Find the minimal path sum from the left column to the right column in matrix.txt
16+
(https://projecteuler.net/project/resources/p082_matrix.txt)
17+
(right click and "Save Link/Target As..."),
18+
a 31K text file containing an 80 by 80 matrix.
19+
"""
20+
21+
import os
22+
23+
24+
def solution(filename: str = "input.txt") -> int:
25+
"""
26+
Returns the minimal path sum in the matrix from the file, by starting in any cell
27+
in the left column and finishing in any cell in the right column,
28+
and only moving up, down, and right
29+
30+
>>> solution("test_matrix.txt")
31+
994
32+
"""
33+
34+
with open(os.path.join(os.path.dirname(__file__), filename)) as input_file:
35+
matrix = [
36+
[int(element) for element in line.split(",")]
37+
for line in input_file.readlines()
38+
]
39+
40+
rows = len(matrix)
41+
cols = len(matrix[0])
42+
43+
minimal_path_sums = [[-1 for _ in range(cols)] for _ in range(rows)]
44+
for i in range(rows):
45+
minimal_path_sums[i][0] = matrix[i][0]
46+
47+
for j in range(1, cols):
48+
for i in range(rows):
49+
minimal_path_sums[i][j] = minimal_path_sums[i][j - 1] + matrix[i][j]
50+
51+
for i in range(1, rows):
52+
minimal_path_sums[i][j] = min(
53+
minimal_path_sums[i][j], minimal_path_sums[i - 1][j] + matrix[i][j]
54+
)
55+
56+
for i in range(rows - 2, -1, -1):
57+
minimal_path_sums[i][j] = min(
58+
minimal_path_sums[i][j], minimal_path_sums[i + 1][j] + matrix[i][j]
59+
)
60+
61+
return min(minimal_path_sums_row[-1] for minimal_path_sums_row in minimal_path_sums)
62+
63+
64+
if __name__ == "__main__":
65+
print(f"{solution() = }")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
131,673,234,103,18
2+
201,96,342,965,150
3+
630,803,746,422,111
4+
537,699,497,121,956
5+
805,732,524,37,331

0 commit comments

Comments
 (0)
Please sign in to comment.