# Python program for OEIS A362093
# Michael S. Branicky, Apr 09 2023

# A362093 a(1) = 1, a(2) = 2; for n > 2, a(n) is the least positive integer not occurring earlier such that a(n) shares no digit with a(n-2) - a(n-1).
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 22, 10, 30, 11, 23, 33, 24, 12, 34, 13, 35, 14, 36, 15, 37, 16, 38, 17, 39, 18, 40, 19, 43, 31, 44, 25, 26, 27, 28, 29, 32, 21, 42, 45, 41, 50, 46, 51, 47, 52, 48, 53, 49, 55, 54, 56, 57, 58, 59, 60, 62, 61, 63, 64, 65, 66, 67, 68, 69, 70, 72, 71, 73, 74, 75, 76, 77]

from time import time
time0 = time()

from itertools import count, product, combinations

# generator of numbers using only digits in set s
def hasonly(s):
    yield from (int("".join(p)) for d in count(1) for p in product(s, repeat=d) if p[0] != "0")

aset = {1, 2}
a1, a2 = 1, 2
alst = [1, 2]

# setup each generator
g = dict()
for i in range(1, 11):
    for c in combinations("0123456789", i):
        s = "".join(c)
        g[s] = hasonly(s)

for n in count(3):
    if n-1 == len(data):
        print("CHECK")
        print("   ", alst)
        print("   ", data)
        print(alst == data)
        print()
    s = "".join(sorted(set("0123456789") - set(str(abs(a1 - a2)))))
    if len(s) == 10 or s == "0":
        N = len(alst)
        print(f"\nSequence has {N} terms")
        print(f"a({N-1}) = {alst[-2]} and a({N}) = {alst[-1]} have difference {a2-a1}.")
        print()
        break
    an = next(g[s])
    while an in aset:
         an = next(g[s])
    a1, a2 = a2, an
    aset.add(an)
    alst.append(an)
    if n%1000000 == 0:
        print("...", n, time()-time0)