from lib_prime import primes


def euler_049():

    def find_increasing_sequence(xs):
        deltas = [(xs[j] - xs[i], i, j)
                  for i in range(0, len(xs) - 1)
                  for j in range(i + 1, len(xs))
                  ]
        d = {}
        for delta, i, j in deltas:
            if delta in d and d[delta][-1] == i:
                d[delta].append(j)
            else:
                d[delta] = [i, j]
        for delta, sequence in d.items():
            if len(sequence) == 3:
                return [xs[index] for index in sequence]
        return []

    ps = [p for p in primes(10000) if p > 1000]
    d = {}
    for p in ps:
        s = "".join(sorted(str(p)))
        try:
            d[s].append(p)
        except KeyError:
            d[s] = [p]

    pss = [value for key, value in d.items() if len(value) >= 3]
    s = [find_increasing_sequence(ps)
         for ps in pss if find_increasing_sequence(ps)]
    return int("".join(map(str, s[1])))


if __name__ == "__main__":
    print("e049.py: " + str(euler_049()))
    assert(euler_049() == 296962999629)
