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 π.
"""
Name scores
Problem 22

Using names.txt (right click and 'Save Link/Target As...'), a 46K text file
containing over five-thousand first names, begin by sorting it into
alphabetical order. Then working out the alphabetical value for each name,
multiply this value by its alphabetical position in the list to obtain a name
score.

For example, when the list is sorted into alphabetical order, COLIN, which is
worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would
obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?
"""
import os


def solution():
    """Returns the total of all the name scores in the file.

    >>> solution()
    871198282
    """
    with open(os.path.dirname(__file__) + "/p022_names.txt") as file:
        names = str(file.readlines()[0])
        names = names.replace('"', "").split(",")

    names.sort()

    name_score = 0
    total_score = 0

    for i, name in enumerate(names):
        for letter in name:
            name_score += ord(letter) - 64

        total_score += (i + 1) * name_score
        name_score = 0
    return total_score


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

LANGUAGE:

DARK MODE: