Last active
April 10, 2018 16:39
-
-
Save nealeyoung/87f2cbbd67a42b855d9f21ecdf497253 to your computer and use it in GitHub Desktop.
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 | |
# https://www.reddit.com/r/math/comments/8b1z6h/interview_question/ | |
from statistics import mean | |
from functools import lru_cache | |
memoize = lru_cache(None) | |
rolls = tuple((min(i, j), max(i, j)) | |
for i in range(1, 7) for j in range(1, 7) if i+j <= 9) | |
table = {} | |
@memoize | |
def value(position=frozenset(range(1, 10)), roll=None, move=None): | |
if move is not None: | |
if not move.issubset(position): | |
return 9 - len(position) | |
return value(position - move) | |
elif roll is None: | |
return mean(value(position, roll) for roll in rolls) | |
else: | |
i, j = roll | |
moves = [[i+j]] if i == j else [[i, j], [i+j]] | |
val, move = max((value(position, move=frozenset(move)), move) for move in moves) | |
table[position, roll] = move, val | |
return val | |
print("value", value()) | |
for (position, roll), (move, val) in sorted(table.items(), key=lambda x: x[1][1]): | |
if len(position) >= 9: | |
print(list(position), roll, f"{val:.6f}", move) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output: