#205 - Dice Game

Peter has nine four-sided (pyramidal) dice, each with faces numbered 1, 2, 3, 4. Colin has six six-sided (cubic) dice, each with faces numbered 1, 2, 3, 4, 5, 6.

Peter and Colin roll the dice and compare totals: the highest total wins. The result is a draw if the totals are equal.

What is the probability that Pyramidal Peter beats Cubic Colin? Give your answer rounded to seven decimal places in the form 0.abcdefg


We can directly brute force this solution. The totals range from 9 to 36 for Peter, and 6 to 36 for Colin. We compute the probability of getting that total for each one. Since the totals are independent probabilities, then probability that they both happen is the product of the two. We loop through each pair, and we add the product, since the occurrences are mutually exclusive.

I use defaultdict to automatically set a value of 0 for uninitialized values. We run a double for loop going through each possible pair of totals.

# file: "problem205.py"
pyrProbs = defaultdict(float)
cubeProbs = defaultdict(float)
# There are 9 pyramids. Each configuration has
# probability 1 / (4 ^ 9).
numberOfDice = 9
prob = 1 / (4 ** numberOfDice)
for config in product(np.arange(1, 5), repeat=numberOfDice):
    total = sum(config)
    pyrProbs[total] += prob
# Do the same for the cubes
numberOfDice = 6
prob = 1 / (6 ** numberOfDice)
for config in product(np.arange(1, 7), repeat=numberOfDice):
    total = sum(config)
    cubeProbs[total] += prob

winProb = 0
# Go through each value in the pyramid and the cube and add those
# which win
for pyrSum, pyrProb in pyrProbs.items():
    for cubeSum, cubeProb in cubeProbs.items():
        if pyrSum > cubeSum:
            winProb += pyrProb * cubeProb

print(f'Peter beats Colin with a probability of {winProb:0.7f}.')

We got an output of,

Peter beats Colin with a probability of 0.5731441.
0.4863471 seconds.

Therefore, the probability that Peter beats Colin is 0.5731441.