"""Check local cyclic ranks and an external distance-four pancake sphere census."""
from collections import Counter
from itertools import permutations
import json
from experience import unrank
from roues import rank


def signature(values):
    cyc=sorted(values)
    j=cyc.index(values[0])
    anchored=cyc[j:]+cyc[:j]
    return tuple(anchored.index(x) for x in values[1:])


def experiment():
    n=8
    joint=Counter()
    low={}
    for w in permutations(range(1,n+1)):
        a,b=signature(w[:4]),signature(w[4:])
        r=rank(w)%6
        assert low.setdefault(b,r)==r
        joint[a,b]+=1
    assert sorted(low.values())==list(range(6)) and set(joint.values())=={1120}
    measured=[]
    for k,known in ((6,199),(7,543),(8,1191),(9,2278)):
        root=tuple(range(k))
        seen={root};frontier={root}
        for _ in range(4):
            frontier={w[:-j]+w[-j:][::-1] for w in frontier for j in range(2,k+1)}-seen
            seen.update(frontier)
        assert len(frontier)==known
        measured.append(len(frontier))
    return dict(permutations=40320,signature_pairs=len(joint),each=1120,distance_four=measured)


if __name__=="__main__":print(json.dumps(experiment(),indent=2))
