"""Adversarial shifts on a 24-tick schedule, up to three switches."""
from itertools import combinations,accumulate
import json
from decalages19 import LEFT

DELAY=(-1,0,1)


def solve(phase,h):
    def hit(t,e):return int(t in LEFT and (t-phase-e)%6 in {x%6 for x in (0,1,4,8,9)})
    sums={e:list(accumulate((hit(t,e) for t in range(24)),initial=0)) for e in DELAY}
    seg={(a,b):min((v[b]-v[a],e) for e,v in sums.items()) for a in range(24) for b in range(a+1,25)}
    ans=[]
    for k in range(4):
        best=(25,[],[])
        for cc in combinations(range(1,24),k):
            if any(b-a<h for a,b in zip(cc,cc[1:])):continue
            cuts=(0,*cc,24);pieces=[seg[a,b] for a,b in zip(cuts,cuts[1:])]
            best=min(best,(sum(x for x,_ in pieces),list(cc),[e for _,e in pieces]))
        ans.append(min(ans[-1],best) if ans else best)
    # Independent time-step recursion tracks age since last switch.
    states={(e,0,1):hit(0,e) for e in DELAY}
    for t in range(1,24):
        nxt={}
        for (e,k,age),cost in states.items():
            for f in DELAY:
                sw=f!=e
                if sw and (k==3 or (k and age<h)):continue
                key=f,k+sw,1 if sw else min(h,age+1)
                nxt[key]=min(nxt.get(key,25),cost+hit(t,f))
        states=nxt
    assert [r[0] for r in ans]==[min(v for (_,k,_),v in states.items() if k<=cap) for cap in range(4)]
    return ans


def study():
    rows=[]
    for h in (1,2,4,6,8,12,16):
        paths=[solve(d,h) for d in range(6)]
        rows.append(dict(h=h,floors=[max(r[k][0] for r in paths) for k in range(4)],paths=paths))
    return rows
if __name__=='__main__':print(json.dumps(study()))
