"# [Path Sum: Three Ways](https://projecteuler.net/problem=82)\n",
"\n",
"This is the same matrix as [problem 81](https://projecteuler.net/problem=81)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "098b70b9",
"metadata": {},
"outputs": [],
"source": [
"with open(\"txt/0082_matrix.txt\") as f:\n",
" mat = matrix((int(n) for n in line.split(',')) for line in f)"
]
},
{
"cell_type": "markdown",
"id": "aaf2f803",
"metadata": {},
"source": [
"We can reuse our solution from problem 81 as well, with some slight modifications.\n",
"1. Instead of our queue initially containing only the top-left entry of the matrix, it will initially hold *all* the entries from the first column.\n",
"2. Additionally, instead of checking if we've reached the bottom-right entry, we'll stop on reaching *any entry* in the last column.\n",
"3. Finally, when adding new entries to the queue, we'll add the entry *above* our current entry, along with the entries to the right and below, as before."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3b7872ef",
"metadata": {},
"outputs": [],
"source": [
"import heapq\n",
"\n",
"def minimal_path_sum(mat):\n",
" m, n = mat.dimensions()\n",
" \n",
" destinations = {(i, n - 1) for i in range(0, m)}\n",
"This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)."