"""J.VIP deterministic rule-state checks; no simulation or match probability.""" from pathlib import Path import json,itertools,sys PARTNER={'A':'B','B':'A','X':'Y','Y':'X'} NORMAL=[('A','X'),('X','B'),('B','Y'),('Y','A')] def turns(n):return n//2 if n<20 else 10+n-20 def rotate(s,r,n): ring=[(s,r),(r,PARTNER[s]),(PARTNER[s],PARTNER[r]),(PARTNER[r],s)] return ring[n%4] def direct(n,change_point=None): if change_point is None or n=10 else 2 score[0 if w=='AB' else 1]+=1 used+=1 ended=max(score)>=11 and abs(score[0]-score[1])>=2 changed=False if not ended: if used==quota:s,r=r,PARTNER[s];used=0 if deciding and not swapped and max(score)==5: r=PARTNER[r];swapped=True;changed=True;change_point=i expected=direct(i,change_point) assert (s,r)==expected,(i,score,(s,r),expected,change_point) rows.append({'pointsCompleted':i,'score':score.copy(),'nextServer':None if ended else s,'nextReceiver':None if ended else r,'ended':ended,'changedEndsNow':changed,'changePoint':change_point,'pointsAlreadyServedThisTurn':used}) return rows INPUT=json.loads(Path(sys.argv[1] if len(sys.argv)>1 else 'input.json').read_text(encoding='utf-8-sig')) assert INPUT['initialServer']=='A' and INPUT['initialReceiver']=='X' assert INPUT['players']=={'AB':['A','B'],'XY':['X','Y']} results=[] for case in INPUT['cases']: rows=trace(case['winners'],case['deciding']) results.append({'id':case['id'],'trace':rows}) find=lambda case,n:next(r for c in results if c['id']==case for r in c['trace'] if r['pointsCompleted']==n) assert (find('ordinary-deuce',20)['nextServer'],find('ordinary-deuce',20)['nextReceiver'])==('B','Y') assert (find('deciding-first5-at9',9)['nextServer'],find('deciding-first5-at9',9)['nextReceiver'])==('A','Y') assert (find('deciding-first5-at9',10)['nextServer'],find('deciding-first5-at9',10)['nextReceiver'])==('Y','B') assert (find('deciding-first5-at9',20)['nextServer'],find('deciding-first5-at9',20)['nextReceiver'])==('B','X') assert (find('deciding-first5-at6',20)['nextServer'],find('deciding-first5-at6',20)['nextReceiver'])==('A','Y') for case in results:assert case['trace'][-1]['ended'] and case['trace'][-1]['nextServer'] is None boundary={str(n):0 for n in range(5,10)} for n in range(5,10): for bits in itertools.product(['AB','XY'],repeat=n): a=bits.count('AB');b=n-a if max(a,b)!=5:continue prev=bits[:-1];prior=[prev.count('AB'),prev.count('XY')] if max(prior)>=5:continue row=trace(bits,True)[-1] assert row['changedEndsNow'] and row['changePoint']==n boundary[str(n)]+=1 assert sum(boundary.values())==252 output={'status':'AllDeterministicChecksPassed','cases':results,'firstFiveBoundaryPrefixCounts':boundary,'firstFiveBoundaryPrefixesChecked':252,'boundaryCountsAreNotProbabilities':True,'directIndexCrossCheck':'Every nonterminal state in all three traces and 252 first-to-five prefixes matched a separate turn-index expression anchored at the actual swap point.'} print(json.dumps(output,ensure_ascii=False,indent=2))