# Project Euler Problem 67
# Created on: 2012-06-18
# Created by: William McDonald

def importTri():
    t = []
    f = open("problem67.txt")
    for line in f:
        t.append(map(int, line.split(" ")))
    return t

def getMax(lm, cur):
    l = len(cur) - 1
    maxL = [lm[0] + cur[0]]
    i = 1
    while True:
        if i == l:
            maxL.append(lm[i - 1] + cur[i])
            break
        maxL.append(max((lm[i - 1]), lm[i]) + cur[i])
        i += 1
    return maxL

def getAns():
    t = importTri()
    lmax = t[0]
    for i in range(1, len(t)):
        lmax = getMax(lmax, t[i])
    print(max(x for x in lmax))

getAns()

@@ -25,112 +25,49 @@
 67%0D%0A
-# Created on: 2012-06-18%0D%0A# Created by: William McDonald%0D%0A%0D%0Adef importTri():%0D%0A    t = %5B%5D%0D%0A    f =
+%0D%0Adef import_triangle():%0D%0A    with
  open(
-%22
+'
 prob
@@ -79,91 +79,369 @@
 .txt
-%22)%0D%0A    for line in f:%0D%0A        t.append(map(int, line.split(%22 %22)))%0D%0A    return t%0D%0A
+') as f:%0D%0A        # Split each line by spaces and convert to integers%0D%0A        return %5Blist(map(int, line.split(' '))) for line in f%5D%0D%0A%0D%0A# The max of this row is the maximum sum up to its parent items plus the value%0D%0A# in this row. But note that the first and last items in this row only have one%0D%0A# parent each, so it can make the code a little funky to write.
 %0D%0Ade
@@ -445,22 +445,31 @@
 %0Adef get
-M
+_m
 ax(l
-m
+ast_maxes
 , cur):%0D
@@ -477,101 +477,212 @@
     
-l = len(cur) - 1%0D%0A    maxL
+current_maxes
  = %5B
-lm
+cur
 %5B0%5D + 
-cur%5B0%5D%5D%0D%0A    i = 1%0D%0A    while True:%0D%0A        if i == l:
+last_maxes%5B0%5D%5D%0D%0A    for idx, lm in enumerate(last_maxes):%0D%0A        # Our left child was the right child of a previous element; get max%0D%0A        max_for_left_child = cur%5Bidx%5D + lm
 %0D%0A  
@@ -691,62 +691,132 @@
     
-    maxL.append(lm%5Bi - 1%5D + cur%5Bi%5D)%0D%0A            break
+current_maxes%5Bidx%5D = max(current_maxes%5Bidx%5D, max_for_left_child)%0D%0A        # Right child hasn't been seen yet, just append it
 %0D%0A  
@@ -825,20 +825,29 @@
     
+current_
 max
-L
+es
 .append(
 max(
@@ -846,57 +846,26 @@
 end(
-max((lm%5Bi - 1%5D), lm%5Bi%5D) + cur%5Bi%5D)%0D%0A        i
+lm + cur%5Bidx
  +
-=
  1
+%5D)
 %0D%0A  
@@ -877,20 +877,29 @@
 urn 
+current_
 max
-L
+es
 %0D%0A%0D%0Adef 
 getA
@@ -894,22 +894,21 @@
 %0D%0A%0D%0Adef 
-getAns
+solve
 ():%0D%0A   
@@ -909,16 +909,23 @@
 :%0D%0A    t
+riangle
  = impor
@@ -929,19 +929,25 @@
 port
-Tri
+_triangle
 ()%0D%0A    
 lmax
@@ -946,50 +946,75 @@
     
-l
 max
- = t%5B0%5D%0D%0A    for i in range(1, len(t))
+_for_last_row = triangle%5B0%5D%0D%0A    for current_row in triangle%5B1:%5D
 :%0D%0A 
@@ -1024,76 +1024,161 @@
     
-l
 max
+_for_last_row
  = get
-M
+_m
 ax(
-l
 max
-, t%5Bi%5D)%0D%0A    print(max(x for x in lmax))%0D%0A%0D%0AgetAns
+_for_last_row, current_row)%0D%0A    print('Answer: %7B%7D'.format(max(max_for_last_row)))%0D%0A%0D%0Aif __name__ == '__main__':%0D%0A    solve
 ()%0D%0A

Update problem 67 to be legible