"""Minimax slot count with a single delay change per full joint cycle."""
import json
from itertools import accumulate
from math import lcm

LEFT={0,1,2,8,9,15,16}


def run(q,j):
    if not 6<=q<=40 or not 0<=j<=5:
        raise ValueError("unsupported period or jitter")
    length=lcm(24,q)
    right={a%q for a in (0,1,4,8,9)}
    times=[t for t in range(length) if t%24 in LEFT]
    floors=[]; constant=[]; witnesses=[]
    for d in range(q):
        curves=[list(accumulate((int((t-d-e)%q in right) for t in times),initial=0)) for e in range(-j,j+1)]
        base=min(c[-1] for c in curves)
        constant.append(base)
        worst=(base,0,0,0)
        for i in range(len(times)+1):
            first,e1=min((c[i],e) for e,c in enumerate(curves,-j))
            last,e2=min((c[-1]-c[i],e) for e,c in enumerate(curves,-j))
            if first+last<worst[0] or i==0:
                worst=(first+last,times[i] if i<len(times) else length,e1,e2)
        floors.append(worst[0]); witnesses.append(worst[1:])
    best=floors.index(max(floors))
    stamp,e1,e2=witnesses[best]
    assert sum(t%24 in LEFT and (t-best-(e1 if t<stamp else e2))%q in right for t in range(length))==floors[best]
    return dict(best=best,one=max(floors),fixed=max(constant),floors=floors,witness=[stamp,e1,e2],period=length)


if __name__=='__main__':
    rows=[dict(q=q,j=j,**run(q,j)) for q in range(6,41) for j in range(3)]
    for row in rows:
        if row['q'] not in (6,8,12):
            continue
        q,j=row['q'],row['j']
        for d,v in enumerate(row['floors']):
            right={a%q for a in (0,1,4,8,9)}
            assert v==min(sum(t%24 in LEFT and (t-d-(a if t<s else b))%q in right for t in range(row['period']))
                          for s in range(row['period']+1) for a in range(-j,j+1) for b in range(-j,j+1))
    print(json.dumps(rows,indent=2))
