Finds the maximum total in a triangle as described by the problem statement above. >>> solution() 7273
()
| 16 | |
| 17 | |
| 18 | def solution() -> int: |
| 19 | """ |
| 20 | Finds the maximum total in a triangle as described by the problem statement |
| 21 | above. |
| 22 | >>> solution() |
| 23 | 7273 |
| 24 | """ |
| 25 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 26 | triangle_path = os.path.join(script_dir, "triangle.txt") |
| 27 | |
| 28 | with open(triangle_path) as in_file: |
| 29 | triangle = [[int(i) for i in line.split()] for line in in_file] |
| 30 | |
| 31 | while len(triangle) != 1: |
| 32 | last_row = triangle.pop() |
| 33 | curr_row = triangle[-1] |
| 34 | for j in range(len(last_row) - 1): |
| 35 | curr_row[j] += max(last_row[j], last_row[j + 1]) |
| 36 | return triangle[0][0] |
| 37 | |
| 38 | |
| 39 | if __name__ == "__main__": |