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