A simple set of functions which solves the sliding pyramid problem. In summary, the task is to find the path from top to bottom of a triangular grid of numbers that has the smallest sum. The program uses the file
input.txt as input.
from time import time def build_tree(): with open("input.txt", "r") as file: lines = file.readlines() list_tree =  for line in lines[1:]: list_tree.append([int(n) for n in line.strip().split(" ")]) return list_tree def min_sum(tree): doug = list(reversed(tree)) above = doug for row in doug[1:]: below = row for index, n in enumerate(below): below[index] += min(above[index],above[index+1]) above = below return above if __name__ == "__main__": a = time() tree = build_tree() print(min_sum(tree), time() - a)
These samples are simple, but I appreciate any feedback.