import random
verbose = False

class DicePool:
   def __init__(self,count,sides):
      self.count = count
      self.sides = sides
   
   def rollPool(self):
      results = []
      for i in range(0, self.count):
         die = self.getRoll()
         results.append(die)
      results.sort()
      return results
          
   def getRoll(self):
      return random.randint(1, self.sides)

class Situation:
   def __init__(self,standardDice,penaltyDice):
      self.sides = 6
      self.maxPool = 6;
      diceToRoll = penaltyDice + standardDice
      if diceToRoll > self.maxPool:
         self.rerollCount = diceToRoll - self.maxPool
         standardDice = self.maxPool - penaltyDice
      else:
         self.rerollCount = 0
      self.standardPool = DicePool(standardDice,self.sides)
      self.penaltyPool = DicePool(penaltyDice,self.sides)
      self.results = {}
      self.repeat = 0
      self.resultRange = range (0,13)
      for result in self.resultRange:
         self.results[result] = 0
   
   def report(self):
      print ''
      print 'Count of how many times player scores X with %dD+%dR+%dP (%d total tests)' % (self.standardPool.count,self.rerollCount,self.penaltyPool.count,self.repeat)
      total = 0.0
      for score in self.resultRange:
         count = self.results[score]
         if (count != 0):
            percentage = count * 100.0 / self.repeat
            line = ' %2d\t%10d\t%6.1f%%' % (score,count,percentage) 
            print line
            total += score * count
      print 'Average score: %.2f' % (total / self.repeat)
      
   def run(self,repeat):
      self.repeat += repeat
      for count in range(0,repeat):
         result = self.test()
         self.results[result] += 1
   
   def test(self):
      standardRoll = self.standardPool.rollPool()
      self.reroll(standardRoll)
      penaltyRoll = self.penaltyPool.rollPool()
      combinedPool = self.combinePools(standardRoll,penaltyRoll)
      self.applyPenalty(combinedPool,penaltyRoll)
      score = self.calculateScore(combinedPool)
      return score
   
   def calculateScore(self,combinedPool):
      die1 = die2 = 0
      try:
         die1 = combinedPool.pop()
         die2 = combinedPool.pop()
         if die2 != die1:
            die2 = 0
      except:
         pass # ran out of dice, probably
      return die1 + die2
   
   def combinePools(self,standardRoll,penaltyRoll):
      combinedPool = []
      combinedPool.extend(standardRoll)
      combinedPool.extend(penaltyRoll)
      combinedPool.sort()
      return combinedPool
   
   def applyPenalty(self,combinedPool,penaltyRoll):
      diceToRemove = len(penaltyRoll)
      while (diceToRemove > 0):
         combinedPool.pop()
         diceToRemove -= 1
         
   def reroll(self,standardRoll):
      diceToReroll = self.rerollCount
      while diceToReroll > 0:
         keep = len(standardRoll) - diceToReroll
         if keep > 0:
            del standardRoll[0:diceToReroll]
            rerollPool = DicePool(diceToReroll,self.sides)
            reroll = rerollPool.rollPool()
            standardRoll.extend(reroll)
            standardRoll.sort()
            diceToReroll = 0
         else:
            del standardRoll[:]
            standardRoll.extend(self.standardPool.rollPool())
            diceToReroll -= len(standardRoll)
      
Counts = {}
random.seed()
repeat = 100000
maxTotalDice = 10
maxReroll = 4
maxPenaltyDice = 4
for penaltyDice in range(0,maxPenaltyDice):
      for standardDice in range(1,maxTotalDice):
         situation = Situation(standardDice,penaltyDice)
         situation.run(repeat)
         situation.report()