fork download
  1. #Output from this script:
  2. # Using keepThrees expected score is 1.75 (2943216 total points from 1679616 possible outcomes)
  3. # Using keepDoubles expected score is 1.81 (1225 total points from 676 possible outcomes)
  4. # Using stratThree expected score is 1.94 (47046 total points from 24266 possible outcomes)
  5.  
  6. from itertools import *
  7.  
  8. faces = [1,2,3,4,5,6]
  9.  
  10. def score(dice):
  11. """Calculate the score for a set of dice"""
  12. total = 0
  13. for i in [1,2,3]:
  14. if dice.count(i) > 2:
  15. total += (dice.count(i) + i - 3)
  16. return total
  17.  
  18. def keepThrees(dice):
  19. """Keep any 3s, reroll everything else"""
  20. keepers = []
  21. for die in diceAfterOneRoll:
  22. if die == 3:
  23. keepers.append(die)
  24. return keepers
  25.  
  26. def keepDoubles(dice):
  27. """Keep 2s and/or 3s if we have at least a pair of them"""
  28. keepers = []
  29. for value in [2,3]:
  30. if dice.count(value) >= 2:
  31. keepers += [value]*dice.count(value)
  32. return keepers
  33.  
  34. def stratThreeSecondRoll(dice):
  35. """The improved second roll strategy that is part of 'strat 3'"""
  36. twos = dice.count(2)
  37. threes = dice.count(3)
  38. if twos == 3 and threes == 3:
  39. return dice
  40. elif twos >= 3:
  41. return [2]*twos
  42. else:
  43. return [3]*threes
  44.  
  45. # The three strategies - name, first roll strategy, second roll strategy
  46. keepStrategies = [['keepThrees', [keepThrees, keepThrees]],
  47. ['keepDoubles', [keepDoubles, keepDoubles]],
  48. ['stratThree', [keepDoubles, stratThreeSecondRoll]]]
  49.  
  50. # Test all dice rolls for each strategy
  51. for name, keepFunctions in keepStrategies:
  52. total = 0
  53. count = 0
  54. diceAfterOneRoll = (2,2,3,3,6,6)
  55. keepersAfterOneRoll = keepFunctions[0](diceAfterOneRoll)
  56. numberToRoll = 6 - len(keepersAfterOneRoll)
  57. # Loop through all possible second throws
  58. for rerolledDice in product(faces, repeat=numberToRoll):
  59. diceAfterTwoRolls = tuple(sorted(rerolledDice+tuple(keepersAfterOneRoll)))
  60. keepersAfterTwoRolls = keepFunctions[1](diceAfterTwoRolls)
  61. numberToRollAfterTwoRolls = 6 - len(keepersAfterTwoRolls)
  62. # Loop through all possible third throws
  63. for rererolledDice in product(faces, repeat=numberToRollAfterTwoRolls):
  64. diceAfterThreeRolls = tuple(sorted(rererolledDice+tuple(keepersAfterTwoRolls)))
  65. total += score(diceAfterThreeRolls)
  66. count += 1
  67.  
  68. print 'Using %s expected score is %0.2f (%d total points from %d possible outcomes)'%(name, 1.0*total/count, total, count)
  69.  
Time limit exceeded #stdin #stdout 5s 7904KB
stdin
Standard input is empty
stdout
Standard output is empty