Skip to content

Instantly share code, notes, and snippets.

@SimonLammer
Created July 10, 2023 07:13
Show Gist options
  • Save SimonLammer/5f7c5fd4f9e60bba9fd13db0930ff83b to your computer and use it in GitHub Desktop.
Save SimonLammer/5f7c5fd4f9e60bba9fd13db0930ff83b to your computer and use it in GitHub Desktop.
Probability to win Snakes&Ladders after n rounds
snakes = {
17: 7,
54: 34,
62: 19,
64: 60,
87: 24,
93: 73,
95: 75,
99: 78,
}
ladders = {
4: 14,
9: 31,
20: 38,
25: 84,
40: 59,
51: 67,
63: 81,
71: 91,
}
if False: # ladders are snakes too
snakes |= {v:k for k,v in ladders.items()}
ladders = {}
print(f"{snakes=}")
print(f"{ladders=}")
board_size = 100
die_min = 1
die_max = 6
m = matrix(QQ, board_size, board_size)
die_probability = 1/(die_max-die_min+1)
for i in range(board_size-1):
for j in range(die_min, die_max+1):
destination = i+j
if destination >= board_size: # overshoot back into the board
destination = board_size-(destination-board_size)-1
m[i,destination] += die_probability
m[board_size-1,board_size-1] = 1 # Stay at finish once reached
for transportations in [snakes, ladders]:
for start, destination in transportations.items():
for i in range(board_size):
m[i,destination-1] += m[i,start-1]
m[i,start-1] = 0
start = vector(QQ, board_size)
start[0] = 1
for i in (1, 2, 3, 5, 8, 13, 21, 30, 34, 55, 89, 100, 144, 233, 377, 610, 987, 1597, 2584):
state = (start * m ** i)
#print(f"{i:4d}: {state.numerical_approx(digits=2)}")
print(f"{i:4d}: {state[board_size-1].numerical_approx(digits=2)}")
@SimonLammer
Copy link
Author

I was inspired to calculate this by https://www.youtube.com/watch?v=k2ixp5VozIs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment