"""Certify the first restoring dwell using nested feasible sets of delay paths."""
from math import lcm
import json
from ruptures20 import solve
from decalages19 import run


def study(q):
    T=lcm(24,q);target=run(q,1)['one'];cache={}
    def trial(h):
        if h not in cache:cache[h]=solve(q,1,h)
        return cache[h]['two']
    lo,hi=1,T
    while lo<hi:
        mid=(lo+hi)//2
        if trial(mid)==target:hi=mid
        else:lo=mid+1
    assert trial(lo)==target and (lo==1 or trial(lo-1)<target)
    if q==6:
        assert all(trial(h)==target if h>=lo else trial(h)<target for h in range(1,T+1))
    return dict(q=q,T=T,threshold=lo,target=target,checks=[cache[h] for h in sorted(cache)])


if __name__=='__main__':print(json.dumps([study(q) for q in (6,12,17,31)],indent=2))
