-
-
Save rvcx/a19ec7eb1ec9f06f17db0c43610b487a to your computer and use it in GitHub Desktop.
simulator for 2017-01-05 riddler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import sys | |
import random | |
from operator import add | |
def simulate(dwarves): | |
beds = set(range(1, dwarves)) | |
beds.remove(random.sample(beds, 1)[0]) | |
beds.add(0) | |
interlopers = 1 | |
for d in range(1, dwarves - 1): | |
if d in beds: | |
beds.remove(d) | |
else: | |
beds.remove(random.sample(beds, 1)[0]) | |
interlopers += 1 | |
if dwarves - 1 in beds: | |
return (1, interlopers) | |
else: | |
return (0, interlopers + 1) | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--trials', type=int, default=10000) | |
parser.add_argument('--dwarves', type=int, default=7) | |
args = parser.parse_args() | |
eldest, interlopers = map(lambda total: total / float(args.trials), | |
map(sum, zip(*(simulate(args.dwarves) | |
for i in range(args.trials))))) | |
print(eldest, interlopers) | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment