from itertools import combinations
from lib_prime import primes, is_prime


def concatenate_all(numbers):
    result = []
    for a, b in combinations(numbers, 2):
        result.append([a, b])
        result.append([b, a])
    return result


def concatentation_is_prime(a, b):
    ab = int(str(a) + str(b))
    ba = int(str(b) + str(a))
    if is_prime(ab) and is_prime(ba):
        return True
    else:
        return False


def euler_060():
    """ Hahaha. I have no idea what I was thinking
    when writing this code. Beautiful! """
    potentials = []
    new_potentials = []
    done = False
    for p in primes(100000):
        if done:
            break
        potentials += new_potentials
        new_potentials = []
        for potential in potentials:
            all_concatenations_prime = True
            for prime in potential:
                if not concatentation_is_prime(p, prime):
                    all_concatenations_prime = False
                    break
            if all_concatenations_prime:
                new_potential = list(potential)
                new_potential.append(p)
                if len(new_potential) > 4:
                    # print(new_potential)
                    s = sum(new_potential)
                    done = True
                    break
                new_potentials.append(new_potential)
        if p < 15:
            potentials.append([p])
    return s


if __name__ == "__main__":
    print("e060.py: " + str(euler_060()))
    assert(euler_060() == 26033)
