Example #1
0
def euler():
    xs = flattened_list_from_file("data/059.txt", separator = ",")
    letter_range = range(ord('a'), ord('z') + 1)
    common_chars_rx = re.compile("[ a-zA-Z.,']")
    the_rx = re.compile("\\bthe\\b")
    for a in letter_range:
        for b in letter_range:
            for c in letter_range:
                key = [a, b, c]
                decrypted = "".join(chr(x ^ key[i % len(key)]) for i,x in enumerate(xs))
                chars = len(decrypted)
                common_chars = len(common_chars_rx.findall(decrypted))
                thes = len(the_rx.findall(decrypted))
                if common_chars >= 0.95 * chars and thes >= 3:
                    return sum(ord(c) for c in decrypted)
    return None
Example #2
0
def euler():
    # read the names from the file
    names = fileutils.flattened_list_from_file('data/022.txt', separator = ',',
      convert_to = str)
    # remove the quotes surrounding the name
    names = [name.replace('"', '') for name in names]
    # sort the names alphabetically
    names.sort()
    # accumulator for the total of the name scores
    accumulator = 0
    for index, name in enumerate(names):
        # calculate the name score
        score = sum(ord(letter) - ord('A') + 1 for letter in name)
        score *= (index + 1)
        # add the score to the accumulator
        accumulator += score
    # return the accumulator
    return accumulator
Example #3
0
def euler():
    # set of the triangle numbers until an arbitrary maximum number
    triangles = set()
    # generate triangle numbers
    n = 1
    highest_triangle = 0
    while highest_triangle < MAX:
        highest_triangle = n * (n + 1) // 2
        triangles.add(highest_triangle)
        n += 1
    # read the words and put them into a list of strings
    words = fileutils.flattened_list_from_file('data/042.txt',
            separator = ',', convert_to = str)
    # strip the quote-sign from the strings, leaving only the word
    words = [word.replace('"', '') for word in words]
    # accumulator for the final answer, the number of triangle words
    triangle_word_count = 0
    # count the number of triangle words
    for word in words:
        if word_to_int(word) in triangles:
            triangle_word_count += 1
    # return it
    return triangle_word_count
Example #4
0
def euler():
    # read the file
    numbers = fileutils.flattened_list_from_file('data/013.txt')
    # return the first ten digits of the sum of the numbers
    return str(sum(numbers)) [:10]