"""A potential on the three boundary states proves the optimal cost for arbitrary block counts."""
from pathlib import Path
from itertools import product
from interface31 import costs
from coupures32 import joint
import json
def study():
    models=json.loads(Path(__file__).with_name('blocs29.json').read_bytes());out=[]
    for m in models:
        motif=m['motif'];words=m['blocks'];cost={}
        for w in words:
            c=costs(w,motif,0,0)[0];cost[w]=c
            for h in (0,1,2):
                for end,val in costs(w,motif,len(w),h).items():
                    assert val>=c-1-int(bool(h))+int(bool(end))
                assert min(costs(w,motif,0,h).values())>=c-int(bool(h))
            for h in (0,1):
                assert costs(w,motif,0,h)[1]==c-int(bool(h))
                assert costs(w,motif,1,h)[0]==c-1-int(bool(h))
        examples=[]
        for i in range(16):
            n=4+i%5;bs=[words[(i+j*j+j//2)%4] for j in range(n)]
            base=sum(cost[w] for w in bs)
            for cap in (0,1,2):
                fees=[base-min(n,cap),base-(n-1)-min(1,cap)]
                assert all(joint(bs,motif,cap,h)==fees[h] for h in (0,1))
            examples.append(bs)
        out.append(dict(motif=motif,cost=cost,checked=examples))
    return out
if __name__=='__main__':print(json.dumps(study()))

