Skip to content

Instantly share code, notes, and snippets.

@k12ish
Last active December 16, 2023 10:00
Show Gist options
  • Save k12ish/484222999a0cc43eb1c3cdbf2e3b2bb0 to your computer and use it in GitHub Desktop.
Save k12ish/484222999a0cc43eb1c3cdbf2e3b2bb0 to your computer and use it in GitHub Desktop.
Knights Move Puzzle (with Sympy!!)
from sympy import symbols, solve, Eq
def knight_moves(x, y, board_size=8):
moves = [
(x + 1, y + 2),
(x + 1, y - 2),
(x - 1, y + 2),
(x - 1, y - 2),
(x + 2, y + 1),
(x + 2, y - 1),
(x - 2, y + 1),
(x - 2, y - 1),
]
return [(i, j) for i, j in moves if 0 <= i < board_size and 0 <= j < board_size]
squares = [symbols(f"{col}1:9") for col in "abcdefgh"]
def equations():
for i in range(8):
for j in range(i, 8):
# symmetry condition along diagonal
yield Eq(squares[i][j], squares[j][i])
if (i, j) == (7, 7):
# the final square is the destination, and is set to zero
yield Eq(squares[-1][-1], 0)
else:
# every other square is equal to 1 + (∑ neighbour ) / len(neighbours)
possible_moves = knight_moves(i, j)
yield Eq(
squares[i][j],
1
+ sum(squares[x][y] for x, y in possible_moves)
/ len(possible_moves),
)
print(solve(list(equations())))
@k12ish
Copy link
Author

k12ish commented Dec 13, 2023

A knight starts in the bottom-left corner of an otherwise-empty chessboard. Every second, the knight moves to a square chosen uniformly at random among the legal moves for the knight.

What is the expected number of seconds until the knight first lands in the top right corner of the chessboard?

The rough idea behind our solution is this equation for the expected distance between nodes:

$$\mathrm{dist}(u, \text { dest })=1 + \sum _{v \in \text { Neighbours of } u} P(u,v) \times \mathrm{dist}(v, \text { dest })$$

Here, we generate 64 equations procedurally, and solve it with Sympy!

Script output:

{a1: 4464173644/20983073,
 a2: 586816639429744381/2758952575872421,
 a3: 7404567334431939/34923450327499,
 a4: 584504025566113701/2758952575872421,
 a5: 577647816455413633/2758952575872421,
 a6: 582099114588134283/2758952575872421,
 a7: 576334985635638600/2758952575872421,
 a8: 98351036746795/465803237527,
 b1: 586816639429744381/2758952575872421,
 b2: 583638685320088815/2758952575872421,
 b3: 4443190571/20983073,
 b4: 582250050487012173/2758952575872421,
 b5: 580523910947559005/2758952575872421,
 b6: 97840770910178/465803237527,
 b7: 568492998440220333/2758952575872421,
 b8: 576204477626819356/2758952575872421,
 c1: 7404567334431939/34923450327499,
 c2: 4443190571/20983073,
 c3: 584962190654480526/2758952575872421,
 c4: 577255439922319087/2758952575872421,
 c5: 578868529885411485/2758952575872421,
 c6: 569595540078244861/2758952575872421,
 c7: 97929696108358/465803237527,
 c8: 570608648153494671/2758952575872421,
 d1: 584504025566113701/2758952575872421,
 d2: 582250050487012173/2758952575872421,
 d3: 577255439922319087/2758952575872421,
 d4: 578017501107835416/2758952575872421,
 d5: 576968376371015333/2758952575872421,
 d6: 562276663362237347/2758952575872421,
 d7: 568641920486461661/2758952575872421,
 d8: 544143173754329183/2758952575872421,
 e1: 577647816455413633/2758952575872421,
 e2: 580523910947559005/2758952575872421,
 e3: 578868529885411485/2758952575872421,
 e4: 576968376371015333/2758952575872421,
 e5: 546818447740302400/2758952575872421,
 e6: 566703266024667547/2758952575872421,
 e7: 553276247211628759/2758952575872421,
 e8: 574843966260949125/2758952575872421,
 f1: 582099114588134283/2758952575872421,
 f2: 97840770910178/465803237527,
 f3: 569595540078244861/2758952575872421,
 f4: 562276663362237347/2758952575872421,
 f5: 566703266024667547/2758952575872421,
 f6: 572493232609584278/2758952575872421,
 f7: 167,
 f8: 6853192829119413/34923450327499,
 g1: 576334985635638600/2758952575872421,
 g2: 568492998440220333/2758952575872421,
 g3: 97929696108358/465803237527,
 g4: 568641920486461661/2758952575872421,
 g5: 553276247211628759/2758952575872421,
 g6: 167,
 g7: 573532568718680757/2758952575872421,
 g8: 558482857016421309/2758952575872421,
 h1: 98351036746795/465803237527,
 h2: 576204477626819356/2758952575872421,
 h3: 570608648153494671/2758952575872421,
 h4: 544143173754329183/2758952575872421,
 h5: 574843966260949125/2758952575872421,
 h6: 6853192829119413/34923450327499,
 h7: 558482857016421309/2758952575872421,
 h8: 0}

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