"""Verify the weighted seven-colouring certificate with integer arithmetic only."""
import argparse
import json
from pathlib import Path

def verify(path):
    certificate=json.loads(Path(path).read_text())
    rows=[(a,(2*a+b)//3,(a+2*b)//3,b) for a in range(78) for b in range(a+3,78,3)]
    assert len(set(rows))==len(rows)==975
    loads=[0]*len(rows)
    mass=0
    for word,weight in certificate['words']:
        assert len(word)==78 and set(word)<=set('0123456') and isinstance(weight,int) and weight>0
        mass+=weight
        for i,(a,b,c,d) in enumerate(rows):
            if len({word[a],word[b],word[c],word[d]})<=2:
                loads[i]+=weight
    cap=max(loads)
    assert mass==certificate['numerator'] and cap==certificate['denominator']
    threshold=(mass-1)//cap
    assert threshold+1==certificate['lower_bound']
    print(f'{len(certificate["words"])} seven-colour words, {len(rows)} progressions, total weight {mass}, maximum row load {cap}')
    print(f'Every subfamily of at most {threshold} rows admits a seven-colouring.')
    return threshold

if __name__=='__main__':
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('certificate',nargs='?',default='fractional-certificate.json')
    verify(parser.parse_args().certificate)
