Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Sort of Dynamic Programming solution in Clear category for The Stones by JamesArruda
"""
This game doesn't follow the standard solutions of Nim (if I'm wrong, comment!),
so instead I just build up the pile sizes and whether or not the pile size forces
a loss from the player facing it.
"""
def stones(pile, moves):
lowest = min(moves)
choice_map = {}
for i in range(1, lowest + 1):
choice_map[i] = 'lose'
for i in range(lowest+1, pile+1):
res = 'lose'
for k in moves:
nxt = i - k
if nxt > 0 and choice_map[nxt] == 'lose':
res = 'win'
break
choice_map[i] = res
return [2, 1][choice_map[pile] == 'win']
if __name__ == '__main__':
print("Example:")
print(stones(17, [1, 3, 4]))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert stones(17, [1, 3, 4]) == 2
assert stones(17, [1, 3, 4, 6, 9]) == 1
assert stones(99, [1]) == 2
print("Coding complete? Click 'Check' to earn cool rewards!")
Sept. 19, 2018