Euler Problem 81

I haven’t really been updating lately, even though I have been working on a few problems.
This one is very similar to the triangle problems of 18 & 67.

#Euler81
import sys
class Problem81:
    def __init__(self):
        pass
    def Solution(self):
        M = []
        f = open("matrix.txt", "r")
        for row in f.readlines():
            newRow = []
            for column in row.split(","):
                newRow.append(int(column))
            M.append(newRow)
        f.close()
        for x in range (len(M)-1,-1,-1):
            for y in range (len(M)-1,-1,-1):
                if x == len(M)-1 and y == len(M)-1:
                    continue
                if x < len(M)-1:
                    s1 = M[x+1][y]
                else:
                    s1 = sys.maxint
                if y < len(M)-1:
                    s2 = M[x][y+1]
                else:
                    s2 = sys.maxint
                if s1 < s2:
                    M[x][y] += s1
                else:
                    M[x][y] += s2
        return "Answer = " + str(M[0][0])
if __name__ == '__main__':
    P = Problem81()
    print P.Solution()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s