sol1 Algorithm

The sol1 Algorithm, also known as "Squaring the Circle" algorithm, is a mathematical technique that aims to solve the ancient geometrical problem of constructing a square with the same area as a given circle using only compass and straightedge. This algorithm is based on the approximation of the value of Pi (π), which is the ratio of the circumference of a circle to its diameter. The main idea behind the sol1 Algorithm is to find the side length of a square that, when multiplied by itself, gives the same area as that of a circle with a given radius. The sol1 Algorithm begins by drawing a circle with the desired radius, followed by constructing an inscribed square within the circle. The next step involves dividing the circle's circumference into a number of equal segments, which are then used to create a polygon that approximates the circle. The area of this polygon can be easily calculated using basic trigonometry, and as the number of segments increases, the approximation of the circle's area becomes more accurate. Finally, the side length of the square is determined by finding the square root of the approximated circle's area, and a square with this side length is constructed using a compass and straightedge. Although the sol1 Algorithm provides an approximation to the problem of squaring the circle, it has been proven mathematically impossible to achieve an exact solution using only compass and straightedge due to the transcendental nature of the number π.
"""
Problem Statement:
By starting at the top of the triangle below and moving to adjacent numbers on
the row below, the maximum total from top to bottom is 23.
3
7 4
2 4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom in triangle.txt (right click and
'Save Link/Target As...'), a 15K text file containing a triangle with
one-hundred rows.
"""
import os


def solution():
    """
    Finds the maximum total in a triangle as described by the problem statement
    above.

    >>> solution()
    7273
    """
    script_dir = os.path.dirname(os.path.realpath(__file__))
    triangle = os.path.join(script_dir, "triangle.txt")

    with open(triangle, "r") as f:
        triangle = f.readlines()

    a = map(lambda x: x.rstrip("\r\n").split(" "), triangle)
    a = list(map(lambda x: list(map(lambda y: int(y), x)), a))

    for i in range(1, len(a)):
        for j in range(len(a[i])):
            if j != len(a[i - 1]):
                number1 = a[i - 1][j]
            else:
                number1 = 0
            if j > 0:
                number2 = a[i - 1][j - 1]
            else:
                number2 = 0
            a[i][j] += max(number1, number2)
    return max(a[-1])


if __name__ == "__main__":
    print(solution())

LANGUAGE:

DARK MODE: