#!/etc/python3.5 import csv def loaddata(inputfile): biglist = list(csv.reader(open(inputfile, 'rb'), delimiter='\t')) dictoflists = {} lines = len(biglist) for line in xrange(0,lines): #needed to "prime" the dictionary for 2d lists dictoflists[str(biglist[line][0])] = [[],[],[],[],[],[],[],[],[],[],[],[],[]] for line in xrange(0,lines): dictoflists[str(biglist[line][0])][int(biglist[line][1])]\ = eval(biglist[line][2]) return(dictoflists) class Pool: def __init__(self): self.dice = [4] self.underpool = [[],[],[],[],[],[],[],[],[],[],[],[],[]] self.overpool = [[],[],[],[],[],[],[],[],[],[],[],[],[]] self.probabilities = [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]] def go(self): return not self.dice == [4]*12 def __str__(self): output = "" for TN in xrange(1,13): output += ("%s\t %d\t %s\n" % (self.dice, TN, self.probabilities[TN])) return(output) def getoverunder(pool, overlist, underlist): MAX = len(pool.dice) for TN in xrange(1,13): for index in xrange(0,MAX): if pool.dice[index] > TN: pool.overpool[TN].append(pool.dice[index]) else: pool.underpool[TN].append(pool.dice[index]) return() def process(pool, overlist, underlist, vectors): for TN in xrange(1,13): if pool.underpool[TN] == []: pool.probabilities[TN] = overlist[str(pool.dice)][TN] elif pool.overpool[TN] == []: pool.probabilities[TN] = underlist[str(pool.dice)][TN] else: for v in vectors: pool.probabilities[TN][sum(v)] \ += underlist[str(pool.underpool[TN])][TN][v[0]] \ * overlist[str(pool.overpool[TN])][TN][v[1]] def iterate(pool): pool.overpool = [[],[],[],[],[],[],[],[],[],[],[],[],[]] pool.underpool = [[],[],[],[],[],[],[],[],[],[],[],[],[]] pool.probabilities = [[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],\ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]] MAX = len(pool.dice) if pool.dice[-1] == 12: pool.dice = [4]*(MAX+1) else: for index in xrange(0,MAX): if index == MAX-1: pool.dice[index] += 2 elif pool.dice[(index+1):MAX] == [pool.dice[index]]*(MAX-index-1): pool.dice[index] += 2 for i in xrange(index+1,MAX): pool.dice[i] = 4 break #main output = open("PoR Probabilities.txt", "w") overlist = loaddata('overinput.txt') underlist = loaddata('underinput.txt') vectors = [] for successes in xrange(0,12): v = [ (i,j) for i in xrange(0,6)\ for j in xrange(0,12) if i+j == successes ] vectors.extend(v) pool = Pool() while pool.go(): print pool.dice getoverunder(pool, overlist, underlist) process(pool, overlist, underlist, vectors) output.write("%s" % pool) iterate(pool) output.close()