"""Final finite check: the flat case t = 9 is impossible.

Model: F = F2^5, radical Q0 = 1, quotient F/Q0 = PG(3,2).  We enumerate all
28 nondegenerate alternating forms on the quotient, all 6 spreads of W(2) for
each form, and all configurations of Lemma 6.5 of arXiv:2611.30333 for t=9.

U = Lambda \\ T (lines whose image is a TI line outside S0)
W = T \\ Lambda (traces whose image is a non-TI line outside S0)

Every line of U or W lies in X, |X| = 9, and u(P)=w(P) for every point P.

Case A: some P has u(P)=w(P)>=2.  Then X is the union of two U-lines and two
W-lines through P, and every further U/W line is a transversal.  We enumerate
all choices and all subsets of transversals: none satisfies u=w.

Case B: u(P)<=1 everywhere.  Then U and W each consist of three pairwise
disjoint lines forming a 3x3 grid.  We enumerate all triples of pairwise
disjoint U-lines; at most two W-lines meet all three, so three such W-lines
never exist.  No configuration.

Run: python t9_exclude_final.py
"""
from itertools import combinations
from collections import Counter

Q0 = 1
PTS = list(range(1, 32))

def all_lines():
    s = set()
    for a in PTS:
        for b in PTS:
            if b <= a:
                continue
            c = a ^ b
            if c:
                s.add(frozenset((a, b, c)))
    return list(s)

L = all_lines()
assert len(L) == 155
QF = [l for l in L if Q0 not in l]
assert len(QF) == 140

def qpt(x):
    assert x != Q0
    return min(x, x ^ Q0) >> 1

def qline(l):
    return frozenset(qpt(x) for x in l)

QL = sorted({qline(l) for l in QF})
assert len(QL) == 35

def make_form(bits):
    pairs = [(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)]
    M = [[0]*4 for _ in range(4)]
    for k, (i, j) in enumerate(pairs):
        if bits >> k & 1:
            M[i][j] = M[j][i] = 1
    def f(x, y):
        v = 0
        for i in range(4):
            for j in range(4):
                if M[i][j] and (x >> i & 1) and (y >> j & 1):
                    v ^= 1
        return v
    A = [row[:] for row in M]
    r = 0
    for c in range(4):
        p = next((i for i in range(r, 4) if A[i][c]), None)
        if p is None:
            continue
        A[r], A[p] = A[p], A[r]
        for i in range(4):
            if i != r and A[i][c]:
                A[i] = [a ^ b for a, b in zip(A[i], A[r])]
        r += 1
    return f, r == 4

forms = [f for bits in range(64) for f, ok in [make_form(bits)] if ok]
assert len(forms) == 28

def ti_lines(f):
    out = []
    for q in QL:
        a, b, c = tuple(q)
        if f(a,b) == 0 and f(a,c) == 0 and f(b,c) == 0:
            out.append(q)
    return out

def spreads_of(ti):
    out = []
    def rec(start, cur, used):
        if len(cur) == 5:
            out.append(frozenset(cur))
            return
        for i in range(start, len(ti)):
            if ti[i] & used:
                continue
            rec(i + 1, cur + [ti[i]], used | ti[i])
    rec(0, [], set())
    return out

caseA_valid = 0
caseA_configs = 0
caseB_triples = 0
caseB_bad = 0
caseB_max_cand = 0
caseB_cand3 = 0

for f in forms:
    TI = ti_lines(f)
    assert len(TI) == 15
    ti_set = set(TI)
    spreads = spreads_of(TI)
    assert len(spreads) == 6
    for spread in spreads:
        def cls(l):
            q = qline(l)
            if q in spread:
                return 'S0'
            return 'U' if q in ti_set else 'W'
        cm = {l: cls(l) for l in QF}
        Ulines = [l for l in QF if cm[l] == 'U']
        Wlines = [l for l in QF if cm[l] == 'W']
        assert len(Ulines) == 40 and len(Wlines) == 80

        # Case B: three pairwise disjoint U-lines, and three W-lines meeting all.
        for u3 in combinations(Ulines, 3):
            if any(u3[i] & u3[j] for i in range(3) for j in range(i+1, 3)):
                continue
            caseB_triples += 1
            cand = [w for w in Wlines if all(w & u for u in u3)]
            caseB_max_cand = max(caseB_max_cand, len(cand))
            if len(cand) < 3:
                continue
            caseB_cand3 += 1
            for w3 in combinations(cand, 3):
                if any(w3[i] & w3[j] for i in range(3) for j in range(i+1, 3)):
                    continue
                X = set().union(*u3, *w3)
                if len(X) != 9:
                    continue
                if all(sum(1 for l in u3 if q in l) == sum(1 for l in w3 if q in l)
                       for q in X):
                    caseB_bad += 1

        # Case A: two U-lines and two W-lines through a point P.
        for P in PTS:
            if P == Q0:
                continue
            through = [l for l in L if P in l and Q0 not in l]
            ub = [l for l in through if cm[l] == 'U']
            wb = [l for l in through if cm[l] == 'W']
            assert len(ub) == 4 and len(wb) == 8
            for U2 in combinations(ub, 2):
                for W2 in combinations(wb, 2):
                    if set(U2) & set(W2):
                        continue
                    X = set().union(*U2, *W2)
                    if len(X) != 9:
                        continue
                    caseA_configs += 1
                    eu = [l for l in L if P not in l and l <= X and cm.get(l) == 'U']
                    ew = [l for l in L if P not in l and l <= X and cm.get(l) == 'W']
                    for ka in range(len(eu) + 1):
                        for A in combinations(eu, ka):
                            for kb in range(len(ew) + 1):
                                for B in combinations(ew, kb):
                                    U = set(U2) | set(A)
                                    W = set(W2) | set(B)
                                    if U & W:
                                        continue
                                    if all(sum(1 for l in U if q in l) ==
                                           sum(1 for l in W if q in l) for q in X):
                                        caseA_valid += 1

print("forms", len(forms))
print("case A configurations", caseA_configs, "valid", caseA_valid)
print("case B triples", caseB_triples, "max candidates", caseB_max_cand,
      "candidate triples", caseB_cand3, "bad", caseB_bad)
assert caseA_valid == 0, caseA_valid
assert caseB_bad == 0, caseB_bad
print("t=9 excluded for all 28 symplectic forms")
