Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
From bottom up solution in Clear category for The Stones by Ylliw
def stones(pile, moves):
# Let's initialize a table of wining/losing positions
# Anything with less or equal number of stones than the smallest move is a loss
win_moves={i:False for i in range(1,min(moves)+1)}
# Let's define the smallest unknown position, will be used later
smallest_unknown=max(win_moves)+1
# Continue checking wining/losing position until we reach the pile size
while pile not in win_moves:
# For all losing position, the other player has a way to win for each move
for m in moves:
for pos,result in list((p,r) for p,r in win_moves.items() if not r):
# We don't need to keep track of anything bigger
if pos+m<=pile:
win_moves[pos+m]=True
# The smallest unknown position so far is loosing
while smallest_unknown in win_moves:
smallest_unknown+=1
win_moves[smallest_unknown]=False
return 1 if win_moves[pile] else 2
Aug. 12, 2019
Comments: