Daniele Fucini a6da9539cd
Add more solutions
Added solutions of problems 81, 82 and 83 in C and python, and
solution of problem 84 in C.
2019-09-30 21:41:38 +02:00

60 lines
1.6 KiB
Python

#!/usr/bin/python
# In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by only moving to the right and down,
# is indicated in bold red and is equal to 2427.
#
# *131* 673 234 103 18
# *201* *96* *342* 965 150
# 630 803 *746* *422* 111
# 537 699 497 *121* 956
# 805 732 524 *37* *331*
#
# Find the minimal path sum, in matrix.txt, a 31K text file containing a 80 by 80 matrix, from the top left to the bottom right
# by only moving right and down.
from timeit import default_timer
def sum_paths(matrix, m, n):
for i in range(m-2, -1, -1):
matrix[m-1][i] = matrix[m-1][i] + matrix[m-1][i+1]
matrix[i][m-1] = matrix[i][m-1] + matrix[i+1][m-1]
for i in range(m-2, -1, -1):
for j in range(n-2, -1, -1):
if matrix[i][j+1] > matrix[i+1][j]:
matrix[i][j] = matrix[i][j] + matrix[i+1][j]
else:
matrix[i][j] = matrix[i][j] + matrix[i][j+1]
return matrix[0][0]
def main():
start = default_timer()
try:
fp = open('matrix.txt', 'r')
except:
print('Error while opening file {}'.format('matrix.txt'))
exit(1)
matrix = fp.readlines()
fp.close()
j = 0
for i in matrix:
matrix[j] = list(map(int, i.strip().split(',')))
j = j + 1
dist = sum_paths(matrix, 80, 80)
end = default_timer()
print('Project Euler, Problem 81')
print('Answer: {}'.format(dist))
print('Elapsed time: {:.9f} seconds'.format(end - start))
if __name__ == '__main__':
main()