Skip to content

Instantly share code, notes, and snippets.

@itarato
Created January 2, 2015 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itarato/55f6eec2427f3557b361 to your computer and use it in GitHub Desktop.
Save itarato/55f6eec2427f3557b361 to your computer and use it in GitHub Desktop.
Math dice problem
import sys
import random
def solve(A, total, current, solution):
if total == 0:
print(' + '.join([str(i) for i in solution]) + ' = ' + str(sum(solution)))
return
if current >= len(A):
return
solution.append(-A[current])
solve(A, total + A[current], current + 1, solution)
solution.pop()
solution.append(A[current])
solve(A, total - A[current], current + 1, solution)
solution.pop()
solve(A, total, current + 1, solution)
if __name__ == '__main__':
if len(sys.argv) != 3:
print('Call it with 2 arguments, eg: 1d100 50d6')
sys.exit(1)
master_dice_count, master_dice_max = map(int, sys.argv[1].split('d'))
player_dice_count, player_dice_max = map(int, sys.argv[2].split('d'))
master_number = random.randint(master_dice_count, master_dice_count * master_dice_max)
player_numbers = [random.randint(1, player_dice_max) for x in range(0, player_dice_count)]
print('Target: ' + str(master_number))
print('Dice rolls: ' + ', '.join([str(i) for i in player_numbers]))
solve(player_numbers, master_number, 0, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment