#!/usr/local/bin/python3 from itertools import permutations as P,combinations as Q from collections import defaultdict as D W=sorted(set(P('0011223'))) def f(w,u): s=0 for I in Q(range(7),len(u)): if all(w[I[j]]==u[j] for j in range(len(u))):s+=1 return s def S(w):return tuple(f(w,u) for u in ('01','12','23','012','123','0123')) G=D(list) for w in W:G[S(w)].append(w) L=list(G.values()) can=sorted(min(c) for c in L) def d(w):return sum(w[i]>w[i+1] for i in range(6)) def t(c):return 5*d(c)+sum((i+1)*(int(c[i])+2) for i in range(7)) big=[c for c in can if len(G[S(c)])>=3] ch=min((t(c),c) for c in big) cs=ch[1] A=B=0 for r,c in enumerate(can,1): X=G[S(c)] b=sum(d(w)==2 for w in X) A+=r*(len(X)+2)*(b+3) B+=r*(sum((i+1)*(int(c[i])+3) for i in range(7))+5) Cv=sum((i+1)*(int(cs[i])+4) for i in range(7))*(f(cs,'01')+2*f(cs,'12')+3*f(cs,'23')+5) print('N',len(L),'K',len(big),'star',''.join(cs),'theta',t(cs),'rank',can.index(cs)+1) print('A',A-101*(A//101),'B',B-103*(B//103),'C',Cv-107*(Cv//107)) print('hist',{k:sum(len(x)==k for x in L) for k in sorted(set(len(x) for x in L))})