Skip to content

Instantly share code, notes, and snippets.

@AstraLuma
Last active September 3, 2017 19:34
Show Gist options
  • Select an option

  • Save AstraLuma/dbc4c02fe1024b7d7b35b901bc2e3245 to your computer and use it in GitHub Desktop.

Select an option

Save AstraLuma/dbc4c02fe1024b7d7b35b901bc2e3245 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import random
import itertools
rand = random.SystemRandom()
def roll(num, *, reroll=False, explosions=False):
dice = [rand.randint(1, 10) for _ in range(num)]
if explosions:
# Extra dice for 10s
dice += [rand.randint(1, 10) for _ in range(dice.count(10))]
if reroll:
# Drop the lowest
dice.remove(min(dice))
dice += [rand.randint(1, 10)]
return dice
def gengroups(roll, *, fifteens=False):
def mkgroups(groups, group, unused):
if not unused:
if group:
yield groups+[group]
else:
yield groups
return
d, unused = unused[0], unused[1:]
group = group+[d]
if fifteens:
if sum(group) >= 15:
yield from mkgroups(groups+[group], [], unused)
elif sum(group) >= 10:
yield from mkgroups(groups+[group], [], unused)
yield from mkgroups(groups, group, unused)
else:
yield from mkgroups(groups, group, unused)
else:
if sum(group) >= 10:
yield from mkgroups(groups+[group], [], unused)
else:
yield from mkgroups(groups, group, unused)
for dice in itertools.permutations(roll, len(roll)):
yield from mkgroups([], [], dice)
def count_successes(groups, *, fifteens=False):
if fifteens:
return sum(
2 if sum(group) > 15 else
1 if sum(group) > 10 else
0
for group in groups
)
else:
return sum(1 for group in groups if sum(group) > 10)
def main():
dice = roll(9, reroll=True, explosions=True)
print(dice)
high = 0
highgroups = []
for groups in gengroups(dice, fifteens=True):
successes = count_successes(groups, fifteens=True)
if successes > high:
high = successes
highgroups = groups
print(successes, groups, end='\r', flush=True)
print("\x1B[K", end='') # ANSI Terminal to clear the line
print(high, highgroups)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment