In a far-off kingdom, a chess queen takes a daily walk across a spiral path, numbered from 1 to n
, not caring to follow the spiral itself, but simply making queen's moves as she would on a chessboard. The queen is beloved by her subjects, and they make a note of every square she visits on her path. Given that the queen can start her walk on any square and ends it on any square, what is the shortest queen's walk that she can take?
The challenge
Given a spiral of integers on a rectangular grid, write a function that returns one of the shortest possible paths (counted by number of cells travelled) between two numbers on this spiral grid using a chess queen's moves.
For example, from 16
to 25
:
25 10 11 12 13
24 9 2 3 14
23 8 1 4 15
22 7 6 5 16
21 20 19 18 17
Some possible paths include 16, 4, 2, 10, 25
and 16, 5, 1, 9, 25
.
Rules
- The input will be any two positive integers.
- The output will be a path of integers (including both endpoints) across the spiral using only orthogonal and diagonal moves.
- The length of a path is counted by the number of cells travelled.
- Your answer may be a program or a function.
- This is code golf, so the smallest number of bytes wins.
As always, if the problem is unclear, please let me know. Good luck and good golfing!
Test cases
>>> queen_spiral(4, 5)
4, 5
>>> queen_spiral(13, 20)
13, 3, 1, 7, 20
>>> queen_spiral(14, 14)
14
>>> queen_spiral(10, 3)
10, 11, 3
>>> queen_spiral(16, 25)
16, 4, 2, 10, 25
>>> queen_spiral(80, 1)
80, 48, 24, 8, 1