|
| 1 | +""" |
| 2 | +Project Euler Problem 345: https://projecteuler.net/problem=345 |
| 3 | +
|
| 4 | +Matrix Sum |
| 5 | +
|
| 6 | +We define the Matrix Sum of a matrix as the maximum possible sum of |
| 7 | +matrix elements such that none of the selected elements share the same row or column. |
| 8 | +Find the Matrix Sum of the following matrix. |
| 9 | +
|
| 10 | +Brute force solution, with caching intermediate steps to speed up the calculation. |
| 11 | +
|
| 12 | +>>> solution(MATRIX_1) |
| 13 | +3315 |
| 14 | +""" |
| 15 | + |
| 16 | +import numpy as np |
| 17 | +from numpy.typing import NDArray |
| 18 | + |
| 19 | +MATRIX_1 = [ |
| 20 | + "7 53 183 439 863", |
| 21 | + "497 383 563 79 973", |
| 22 | + "287 63 343 169 583", |
| 23 | + "627 343 773 959 943", |
| 24 | + "767 473 103 699 303", |
| 25 | +] |
| 26 | + |
| 27 | +MATRIX_2 = [ |
| 28 | + "7 53 183 439 863 497 383 563 79 973 287 63 343 169 583", |
| 29 | + "627 343 773 959 943 767 473 103 699 303 957 703 583 639 913", |
| 30 | + "447 283 463 29 23 487 463 993 119 883 327 493 423 159 743", |
| 31 | + "217 623 3 399 853 407 103 983 89 463 290 516 212 462 350", |
| 32 | + "960 376 682 962 300 780 486 502 912 800 250 346 172 812 350", |
| 33 | + "870 456 192 162 593 473 915 45 989 873 823 965 425 329 803", |
| 34 | + "973 965 905 919 133 673 665 235 509 613 673 815 165 992 326", |
| 35 | + "322 148 972 962 286 255 941 541 265 323 925 281 601 95 973", |
| 36 | + "445 721 11 525 473 65 511 164 138 672 18 428 154 448 848", |
| 37 | + "414 456 310 312 798 104 566 520 302 248 694 976 430 392 198", |
| 38 | + "184 829 373 181 631 101 969 613 840 740 778 458 284 760 390", |
| 39 | + "821 461 843 513 17 901 711 993 293 157 274 94 192 156 574", |
| 40 | + "34 124 4 878 450 476 712 914 838 669 875 299 823 329 699", |
| 41 | + "815 559 813 459 522 788 168 586 966 232 308 833 251 631 107", |
| 42 | + "813 883 451 509 615 77 281 613 459 205 380 274 302 35 805", |
| 43 | +] |
| 44 | + |
| 45 | + |
| 46 | +def solve( |
| 47 | + arr: NDArray, row_ind: int, include_set: set[int], cache: dict[str, int] |
| 48 | +) -> int: |
| 49 | + """ |
| 50 | + finds the max sum for array arr starting with row number row_ind, and with columns |
| 51 | + included in include_set. cache is used for caching intermediate results. |
| 52 | +
|
| 53 | + >>> solve(np.array([[1, 2], [3,4]]), 0, {0, 1}, {}) |
| 54 | + np.int64(5) |
| 55 | + """ |
| 56 | + |
| 57 | + cache_id = f"{row_ind}, {sorted(include_set)}" |
| 58 | + if cache_id in cache: |
| 59 | + return cache[cache_id] |
| 60 | + if row_ind == len(arr): |
| 61 | + return 0 |
| 62 | + sub_max = 0 |
| 63 | + for i in include_set: |
| 64 | + new_set = include_set - {i} |
| 65 | + sub_max = max( |
| 66 | + sub_max, arr[row_ind, i] + solve(arr, row_ind + 1, new_set, cache) |
| 67 | + ) |
| 68 | + cache[cache_id] = sub_max |
| 69 | + return sub_max |
| 70 | + |
| 71 | + |
| 72 | +def solution(matrix_str: list[str] = MATRIX_2) -> int: |
| 73 | + """ |
| 74 | + Takes list of strings matrix_str to parse the matrix and calculates the max sum. |
| 75 | +
|
| 76 | + >>> solution(["1 2", "3 4"]) |
| 77 | + 5 |
| 78 | + """ |
| 79 | + |
| 80 | + n = len(matrix_str) |
| 81 | + arr = np.empty((n, n), dtype=np.int64) |
| 82 | + for i in range(n): |
| 83 | + els = matrix_str[i].strip().split(" ") |
| 84 | + for j in range(len(els)): |
| 85 | + arr[i, j] = int(els[j]) |
| 86 | + |
| 87 | + cache: dict[str, int] = {} |
| 88 | + ans = solve(arr, 0, set(range(n)), cache) |
| 89 | + return int(ans) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + print(f"{solution() = }") |
0 commit comments