Ejemplo n.º 1
0
def euler():
    # highest product of four adjacent numbers in the same direction
    highest_product = 0
    # read the file to get the grid
    grid = fileutils.list_from_file('data/011.txt')
    # horizontal products
    for row in grid:
        for x in range(len(grid [0]) - 3):
            highest_product = max(highest_product, product(row [x : x + 4]))
    # vertical products
    for x in range(len(grid [0])):
        column = [row [x] for row in grid]
        for y in range(len(grid) - 3):
            highest_product = max(highest_product, 
              product(column [y : y + 4]))
    # diagonal products from top-left to bottom-right
    for y in range(len(grid) - 3):
        for x in range(len(grid [0]) - 3):
            elements = [grid [y + i][x + i] for i in range(4)]
            highest_product = max(highest_product, product(elements))
    # highest products from bottom-left to top-right
    for y in range(len(grid) - 3):
        for x in range(3, len(grid [0])):
            elements = [grid [y + i][x - i] for i in range(4)]
            highest_product = max(highest_product, product(elements))
    # return the highest product
    return highest_product
Ejemplo n.º 2
0
def euler ():
    # read the pyramid from the file
    pyramid = fileutils.list_from_file ('data/067.txt')
    # for each row, starting at the second from the bottom and going up
    for y in range (len (pyramid) - 2, -1, -1):
        # for each element of that row
        for x in range (y + 1):
            # add to it the highest of the two directly below it
            pyramid [y][x] += max (pyramid [y + 1][x], pyramid [y + 1][x + 1])
    # return the value at the top of the pyramid
    return pyramid [0][0]