import itertools
import cmath
import sys

def contains(short_w, long_w):
    # check if short_w is contained in long_w
    for i in range(1 + len(long_w) - len(short_w)):
        if short_w == long_w[i:i+len(short_w)]:
            return i, i + len(short_w) - 1
    return False

        
def pse_period(alphabet, N):
    '''
    Greedy algorithm to generate a word that contains all subwords (or their reflections) of length N over the given alphabet.
    Reflections are not needed because they yield the same spectrum.    
    ''' 
    word = []  
    combs = itertools.product(alphabet, repeat=N)   
    for comb in combs:
        if not (contains(list(comb), word) or contains(list(reversed(comb)), word)):
            c = comb
            word.extend(c)
    return word


def write_pse_word(alphabet, N):
    with open('word.txt', 'w') as fp:
        fp.write(str(pse_period(alphabet, N)))
    

def main(args):
    alphabet = [0, 1, 2]
    
    if (len(args) >= 2):
        N = int(args[1])
    else:
        N = 6
    
    word = pse_period(alphabet, N)

    with open(f'./output/diagonal_{N}.txt', 'w') as fp:
        fp.write(', '.join(map(str, word)))
        
        
if __name__ == "__main__":
    main(sys.argv)
