Introduction
Inspired by the very recent video The Trapped Knight - Numberphile, I came up with a challenge.
The trapped knight sequence is a finite integer sequence of length 2016, starting from 1, and has the following construction rules:
- Write a number spiral in the following manner:
17 16 15 14 13 ...
18 5 4 3 12 ...
19 6 1 2 11 ...
20 7 8 9 10 ...
21 22 23 24 25 ...
- Place a knight on 1.
- Move the knight to the grid with the smallest number it can go that has not been visited before, according to the rules of chess (i.e. 2 units vertically and 1 unit horizontally, or vice versa).
- Repeat until the knight gets stuck.
Here is the first three steps:
Step 1
17 [16] 15 [14] 13
[18] 5 4 3 [12]
19 6 < 1> 2 11
[20] 7 8 9 [10]
21 [22] 23 [24] 25
Possible moves are 10, 12, 14, 16, 18, 20, 22, 24, among which the smallest is 10, so the second term is 10.
Step 2
4 [ 3] 12 [29] 54
( 1) 2 11 28 [53]
8 9 <10> 27 52
[23] 24 25 26 [51]
46 [47] 48 [49] 50
Possible moves are 1, 3, 23, 29, 47, 49, 51, 53, among which the smallest is 3, so the third term is 3.
Step 3
35 [34] 33 [32] 31
[16] 15 14 13 [30]
5 4 < 3> 12 29
[ 6] ( 1) 2 11 [28]
7 [ 8] 9 (10) 27
Possible moves are 6, 8, 10, 16, 28, 30, 32, 34, among which the smallest is 6, so the fourth term is 6.
The sequence stars with:
1 10 3 6 9 4 7 2 5 8 11 14 ...
and ends with
... 2099 2284 2477 2096 2281 2474 2675 2884 3101 2880 2467 2084
Challenge
Write a shortest program or function, receiving an integer in the range [1, 2016]
(or [0, 2015]
if 0-indexed is used) as input, output the number at that index in the trapped knight sequence. You can choose to index the sequence with 0-indexed or 1-indexed, but you must specify which indexing scheme you use.
Test cases (1-indexed)
n | s(n)
-----+-----
1 | 1
2 | 10
3 | 3
6 | 4
11 | 11
21 | 23
51 | 95
101 | 65
201 | 235
501 | 761
1001 | 1069
2001 | 1925
2016 | 2084
For all possible outputs, please refer to this page.
Winning Criteria
The shortest code of each language wins. Restrictions on standard loopholes apply.
12851850258
\$\endgroup\$