#!/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] best=min(t(c) for c in big) print('best',best,'ties',[''.join(c) for c in big if t(c)==best]) print('first15',[(t(c),''.join(c),len(G[S(c)]),sum(d(w)==2 for w in G[S(c)])) for c in sorted(big,key=lambda c:(t(c),c))[:15]]) cs=min((t(c),c) for c in big)[1] print('rank',can.index(cs)+1,'before',[''.join(c) for c in can[:can.index(cs)]]) print('sigstar',S(cs),'bstar',sum(d(w)==2 for w in G[S(cs)]),'sstar',len(G[S(cs)])) print('allbig',[(t(c),''.join(c),len(G[S(c)]),sum(d(w)==2 for w in G[S(c)])) for c in big])