From a177c81b27993e9e36621c1949565f15107f4721 Mon Sep 17 00:00:00 2001 From: mindaugl Date: Thu, 10 Apr 2025 15:17:30 +0800 Subject: [PATCH 01/10] Add solution for the Euler project problem 345. --- project_euler/problem_345/__init__.py | 0 project_euler/problem_345/sol1.py | 93 +++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 project_euler/problem_345/__init__.py create mode 100644 project_euler/problem_345/sol1.py diff --git a/project_euler/problem_345/__init__.py b/project_euler/problem_345/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_345/sol1.py b/project_euler/problem_345/sol1.py new file mode 100644 index 000000000000..642d283ea77a --- /dev/null +++ b/project_euler/problem_345/sol1.py @@ -0,0 +1,93 @@ +""" +Project Euler Problem 345: https://projecteuler.net/problem=345 + +Matrix Sum + +We define the Matrix Sum of a matrix as the maximum possible sum of +matrix elements such that none of the selected elements share the same row or column. +Find the Matrix Sum of the following matrix. + +Brute force solution, with caching intermediate steps to speed up the calculation. + +>>> solution(MATRIX_1) +3315 +""" + +import numpy as np +from numpy.typing import NDArray + +MATRIX_1 = [ + "7 53 183 439 863", + "497 383 563 79 973", + "287 63 343 169 583", + "627 343 773 959 943", + "767 473 103 699 303", +] + +MATRIX_2 = [ + "7 53 183 439 863 497 383 563 79 973 287 63 343 169 583", + "627 343 773 959 943 767 473 103 699 303 957 703 583 639 913", + "447 283 463 29 23 487 463 993 119 883 327 493 423 159 743", + "217 623 3 399 853 407 103 983 89 463 290 516 212 462 350", + "960 376 682 962 300 780 486 502 912 800 250 346 172 812 350", + "870 456 192 162 593 473 915 45 989 873 823 965 425 329 803", + "973 965 905 919 133 673 665 235 509 613 673 815 165 992 326", + "322 148 972 962 286 255 941 541 265 323 925 281 601 95 973", + "445 721 11 525 473 65 511 164 138 672 18 428 154 448 848", + "414 456 310 312 798 104 566 520 302 248 694 976 430 392 198", + "184 829 373 181 631 101 969 613 840 740 778 458 284 760 390", + "821 461 843 513 17 901 711 993 293 157 274 94 192 156 574", + "34 124 4 878 450 476 712 914 838 669 875 299 823 329 699", + "815 559 813 459 522 788 168 586 966 232 308 833 251 631 107", + "813 883 451 509 615 77 281 613 459 205 380 274 302 35 805", +] + + +def solve( + arr: NDArray, row_ind: int, include_set: set[int], cache: dict[str, int] +) -> int: + """ + finds the max sum for array arr starting with row number row_ind, and with columns + included in include_set. cache is used for caching intermediate results. + + >>> solve(np.array([[1, 2], [3,4]]), 0, {0, 1}, {}) + np.int64(5) + """ + + cache_id = f"{row_ind}, {sorted(include_set)}" + if cache_id in cache: + return cache[cache_id] + if row_ind == len(arr): + return 0 + sub_max = 0 + for i in include_set: + new_set = include_set - {i} + sub_max = max( + sub_max, arr[row_ind, i] + solve(arr, row_ind + 1, new_set, cache) + ) + cache[cache_id] = sub_max + return sub_max + + +def solution(matrix_str: list[str] = MATRIX_2) -> int: + """ + Takes list of strings matrix_str to parse the matrix and calculates the max sum. + + >>> solution(["1 2", "3 4"]) + 5 + """ + + n = len(matrix_str) + arr = np.empty((n, n), dtype=np.int64) + for i in range(n): + els = matrix_str[i].strip().split(" ") + for j in range(len(els)): + arr[i, j] = int(els[j]) + + cache: dict[str, int] = {} + ans = solve(arr, 0, set(range(n)), cache) + return int(ans) + + +if __name__ == "__main__": + print(f"{solution() = }") From 4a9e68c7c79636e65d7bb2d57f5f0be5c48ad0fb Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 6 May 2025 22:22:59 +0300 Subject: [PATCH 02/10] Update sol1.py --- project_euler/problem_345/sol1.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/project_euler/problem_345/sol1.py b/project_euler/problem_345/sol1.py index 642d283ea77a..263d28364dfb 100644 --- a/project_euler/problem_345/sol1.py +++ b/project_euler/problem_345/sol1.py @@ -5,12 +5,32 @@ We define the Matrix Sum of a matrix as the maximum possible sum of matrix elements such that none of the selected elements share the same row or column. -Find the Matrix Sum of the following matrix. -Brute force solution, with caching intermediate steps to speed up the calculation. +For example, the Matrix Sum of the matrix below equals 3315 ( = 863 + 383 + 343 + 959 + 767): + 7 53 183 439 863 + 497 383 563 79 973 + 287 63 343 169 583 + 627 343 773 959 943 + 767 473 103 699 303 + +Find the Matrix Sum of: + 7 53 183 439 863 497 383 563 79 973 287 63 343 169 583 + 627 343 773 959 943 767 473 103 699 303 957 703 583 639 913 + 447 283 463 29 23 487 463 993 119 883 327 493 423 159 743 + 217 623 3 399 853 407 103 983 89 463 290 516 212 462 350 + 960 376 682 962 300 780 486 502 912 800 250 346 172 812 350 + 870 456 192 162 593 473 915 45 989 873 823 965 425 329 803 + 973 965 905 919 133 673 665 235 509 613 673 815 165 992 326 + 322 148 972 962 286 255 941 541 265 323 925 281 601 95 973 + 445 721 11 525 473 65 511 164 138 672 18 428 154 448 848 + 414 456 310 312 798 104 566 520 302 248 694 976 430 392 198 + 184 829 373 181 631 101 969 613 840 740 778 458 284 760 390 + 821 461 843 513 17 901 711 993 293 157 274 94 192 156 574 + 34 124 4 878 450 476 712 914 838 669 875 299 823 329 699 + 815 559 813 459 522 788 168 586 966 232 308 833 251 631 107 + 813 883 451 509 615 77 281 613 459 205 380 274 302 35 805 ->>> solution(MATRIX_1) -3315 +Brute force solution, with caching intermediate steps to speed up the calculation. """ import numpy as np @@ -75,6 +95,8 @@ def solution(matrix_str: list[str] = MATRIX_2) -> int: >>> solution(["1 2", "3 4"]) 5 + >>> solution(MATRIX_1) + 3315 """ n = len(matrix_str) From dc88339ecee48cfc7234ca665e0e97eb86f4efb2 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 6 May 2025 22:24:07 +0300 Subject: [PATCH 03/10] Update sol1.py --- project_euler/problem_345/sol1.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_345/sol1.py b/project_euler/problem_345/sol1.py index 263d28364dfb..a9a429859c41 100644 --- a/project_euler/problem_345/sol1.py +++ b/project_euler/problem_345/sol1.py @@ -6,7 +6,8 @@ We define the Matrix Sum of a matrix as the maximum possible sum of matrix elements such that none of the selected elements share the same row or column. -For example, the Matrix Sum of the matrix below equals 3315 ( = 863 + 383 + 343 + 959 + 767): +For example, the Matrix Sum of the matrix below equals +3315 ( = 863 + 383 + 343 + 959 + 767): 7 53 183 439 863 497 383 563 79 973 287 63 343 169 583 From 639e922faa548de4e87df21272594b35758cb968 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 6 May 2025 22:25:51 +0300 Subject: [PATCH 04/10] Update sol1.py --- project_euler/problem_345/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_345/sol1.py b/project_euler/problem_345/sol1.py index a9a429859c41..4bc5384b213c 100644 --- a/project_euler/problem_345/sol1.py +++ b/project_euler/problem_345/sol1.py @@ -71,7 +71,7 @@ def solve( finds the max sum for array arr starting with row number row_ind, and with columns included in include_set. cache is used for caching intermediate results. - >>> solve(np.array([[1, 2], [3,4]]), 0, {0, 1}, {}) + >>> solve(np.array([[1, 2], [3, 4]]), 0, {0, 1}, {}) np.int64(5) """ From 605867b0e379ba0ee1154e64a4555196ecc76cc5 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 6 May 2025 22:32:14 +0300 Subject: [PATCH 05/10] Update sol1.py --- project_euler/problem_345/sol1.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/project_euler/problem_345/sol1.py b/project_euler/problem_345/sol1.py index 4bc5384b213c..4d70473f5d4e 100644 --- a/project_euler/problem_345/sol1.py +++ b/project_euler/problem_345/sol1.py @@ -65,29 +65,31 @@ def solve( - arr: NDArray, row_ind: int, include_set: set[int], cache: dict[str, int] + arr: NDArray, row: int, cols: set[int], cache: dict[str, int] ) -> int: """ - finds the max sum for array arr starting with row number row_ind, and with columns - included in include_set. cache is used for caching intermediate results. + Finds the max sum for array `arr` starting with row index `row`, and with columns + included in `cols`. `cache` is used for caching intermediate results. - >>> solve(np.array([[1, 2], [3, 4]]), 0, {0, 1}, {}) + >>> solve(arr=np.array([[1, 2], [3, 4]]), row=0, cols={0, 1}, cache={}) np.int64(5) """ cache_id = f"{row_ind}, {sorted(include_set)}" if cache_id in cache: return cache[cache_id] - if row_ind == len(arr): + + if row == len(arr): return 0 - sub_max = 0 - for i in include_set: - new_set = include_set - {i} - sub_max = max( - sub_max, arr[row_ind, i] + solve(arr, row_ind + 1, new_set, cache) + + max_sum = 0 + for col in cols: + new_cols = cols - {col} + max_sum = max( + max_sum, arr[row, col] + solve(arr=arr, row=row + 1, cols=new_cols, cache=cache) ) - cache[cache_id] = sub_max - return sub_max + cache[cache_id] = max_sum + return max_sum def solution(matrix_str: list[str] = MATRIX_2) -> int: From 0974ec08cb170f3124bd4e0066e08623cc8c33d9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 May 2025 19:32:39 +0000 Subject: [PATCH 06/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_345/sol1.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/project_euler/problem_345/sol1.py b/project_euler/problem_345/sol1.py index 4d70473f5d4e..54051e7cf7ae 100644 --- a/project_euler/problem_345/sol1.py +++ b/project_euler/problem_345/sol1.py @@ -64,9 +64,7 @@ ] -def solve( - arr: NDArray, row: int, cols: set[int], cache: dict[str, int] -) -> int: +def solve(arr: NDArray, row: int, cols: set[int], cache: dict[str, int]) -> int: """ Finds the max sum for array `arr` starting with row index `row`, and with columns included in `cols`. `cache` is used for caching intermediate results. @@ -86,7 +84,8 @@ def solve( for col in cols: new_cols = cols - {col} max_sum = max( - max_sum, arr[row, col] + solve(arr=arr, row=row + 1, cols=new_cols, cache=cache) + max_sum, + arr[row, col] + solve(arr=arr, row=row + 1, cols=new_cols, cache=cache), ) cache[cache_id] = max_sum return max_sum From a63ccd0a72219344dcc526c5d31651a67c76f72b Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 6 May 2025 22:33:42 +0300 Subject: [PATCH 07/10] Update sol1.py --- project_euler/problem_345/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_345/sol1.py b/project_euler/problem_345/sol1.py index 54051e7cf7ae..2551a077e9bb 100644 --- a/project_euler/problem_345/sol1.py +++ b/project_euler/problem_345/sol1.py @@ -73,7 +73,7 @@ def solve(arr: NDArray, row: int, cols: set[int], cache: dict[str, int]) -> int: np.int64(5) """ - cache_id = f"{row_ind}, {sorted(include_set)}" + cache_id = f"{row}, {sorted(cols)}" if cache_id in cache: return cache[cache_id] From d1c3812bfb28e10b637506c82e56ae7bd16d369f Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 6 May 2025 22:40:08 +0300 Subject: [PATCH 08/10] Update sol1.py --- project_euler/problem_345/sol1.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/project_euler/problem_345/sol1.py b/project_euler/problem_345/sol1.py index 2551a077e9bb..deebece19102 100644 --- a/project_euler/problem_345/sol1.py +++ b/project_euler/problem_345/sol1.py @@ -93,7 +93,7 @@ def solve(arr: NDArray, row: int, cols: set[int], cache: dict[str, int]) -> int: def solution(matrix_str: list[str] = MATRIX_2) -> int: """ - Takes list of strings matrix_str to parse the matrix and calculates the max sum. + Takes list of strings `matrix_str` to parse the matrix and calculates the max sum. >>> solution(["1 2", "3 4"]) 5 @@ -102,15 +102,14 @@ def solution(matrix_str: list[str] = MATRIX_2) -> int: """ n = len(matrix_str) - arr = np.empty((n, n), dtype=np.int64) - for i in range(n): - els = matrix_str[i].strip().split(" ") - for j in range(len(els)): - arr[i, j] = int(els[j]) + arr = np.empty(shape=(n, n), dtype=np.int64) + for row, matrix_row_str in enumerate(matrix_str): + matrix_row_list_str = matrix_row_str.split() + for col, elem_str in enumerate(matrix_row_list_str): + arr[row, col] = int(elem_str) cache: dict[str, int] = {} - ans = solve(arr, 0, set(range(n)), cache) - return int(ans) + return solve(arr=arr, row=0, cols=set(range(n)), cache=cache) if __name__ == "__main__": From 569ba08703838874f5b3bec18d1032e70967e1ec Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 6 May 2025 22:45:00 +0300 Subject: [PATCH 09/10] Update sol1.py --- project_euler/problem_345/sol1.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_345/sol1.py b/project_euler/problem_345/sol1.py index deebece19102..4cef6134b23f 100644 --- a/project_euler/problem_345/sol1.py +++ b/project_euler/problem_345/sol1.py @@ -70,7 +70,7 @@ def solve(arr: NDArray, row: int, cols: set[int], cache: dict[str, int]) -> int: included in `cols`. `cache` is used for caching intermediate results. >>> solve(arr=np.array([[1, 2], [3, 4]]), row=0, cols={0, 1}, cache={}) - np.int64(5) + 5 """ cache_id = f"{row}, {sorted(cols)}" @@ -85,7 +85,7 @@ def solve(arr: NDArray, row: int, cols: set[int], cache: dict[str, int]) -> int: new_cols = cols - {col} max_sum = max( max_sum, - arr[row, col] + solve(arr=arr, row=row + 1, cols=new_cols, cache=cache), + int(arr[row, col]) + solve(arr=arr, row=row + 1, cols=new_cols, cache=cache), ) cache[cache_id] = max_sum return max_sum @@ -102,7 +102,7 @@ def solution(matrix_str: list[str] = MATRIX_2) -> int: """ n = len(matrix_str) - arr = np.empty(shape=(n, n), dtype=np.int64) + arr = np.empty(shape=(n, n), dtype=int) for row, matrix_row_str in enumerate(matrix_str): matrix_row_list_str = matrix_row_str.split() for col, elem_str in enumerate(matrix_row_list_str): From bf070fffb3fc1d5ca7283aa9f7aa55218cb282c7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 6 May 2025 19:46:39 +0000 Subject: [PATCH 10/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_345/sol1.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_345/sol1.py b/project_euler/problem_345/sol1.py index 4cef6134b23f..4234458c5ad5 100644 --- a/project_euler/problem_345/sol1.py +++ b/project_euler/problem_345/sol1.py @@ -85,7 +85,8 @@ def solve(arr: NDArray, row: int, cols: set[int], cache: dict[str, int]) -> int: new_cols = cols - {col} max_sum = max( max_sum, - int(arr[row, col]) + solve(arr=arr, row=row + 1, cols=new_cols, cache=cache), + int(arr[row, col]) + + solve(arr=arr, row=row + 1, cols=new_cols, cache=cache), ) cache[cache_id] = max_sum return max_sum