30 lines
894 B
Python
30 lines
894 B
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
def from_file(name):
|
||
|
mx = []
|
||
|
with open(name, 'r') as f:
|
||
|
for row in f.readlines():
|
||
|
mx.append([int(num) if num[-1] != '\n' else int(num[:-1]) for num in row.split(',')])
|
||
|
return mx
|
||
|
|
||
|
def main():
|
||
|
mx = from_file('data/082_matrix.txt')
|
||
|
rl = len(mx)
|
||
|
cl = len(mx[0])
|
||
|
for col in range(cl - 2, -1, -1):
|
||
|
cache = [None for i in range(rl)]
|
||
|
cache[rl - 1] = sum(mx[rl - 1][col : col + 2])
|
||
|
|
||
|
for row in range(rl - 2, -1, -1):
|
||
|
cache[row] = mx[row][col] + min(cache[row + 1], mx[row][col + 1])
|
||
|
s = sum(mx[0][col : col + 2])
|
||
|
mx[0][col] = min(s, cache[0])
|
||
|
|
||
|
for row in range(1, rl):
|
||
|
s = mx[row][col] + min(s, mx[row][col + 1])
|
||
|
mx[row][col] = min(cache[row], s)
|
||
|
return min(list(zip(*mx))[0])
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
print(main())
|