The Path Of The Wildebeest Golf a program or function which gives the \$n^{\text{th}}\$ location of the wildebeest who starts at square \$1\$ on an infinite chessboard which is numbered in an anti-clockwise square spiral, where the wildebeest always visits the lowest numbered square she can reach that she has not yet visited.

Inspiration: The Trapped Knight and OEIS A316667.

Edit: This sequence is now on the OEIS as A323763.

The code may produce the \$n^{\text{th}}\$ location, the first \$n\$ locations, or generate the sequence taking no input.

Feel free to give her location after (or up to) \$n\$ leaps instead, but if so please state this clearly in your answer and make sure that an input of \$n=0\$ yields 1 (or [1] if appropriate).

This is code-golf, so the aim is to produce working code in as few bytes as possible in your chosen language.

Note: the wildebeest becomes trapped (much like the knight does at his \$2016^{\text{th}}\$ location, square \$2084\$, and the camel does at his \$3723^{\text{rd}}\$, square \$7081\$) at her \$12899744968^{\text{th}}\$ location on square \$12851850258\$. The behaviour of your code may be undefined for \$n\$ larger than this. (Thanks to Deadcode for the C++ code that found this!)

Detail

The board looks like the below, and continues indefinitely:

101 100  99  98  97  96  95  94  93  92  91
102  65  64  63  62  61  60  59  58  57  90
103  66  37  36  35  34  33  32  31  56  89
104  67  38  17  16  15  14  13  30  55  88
105  68  39  18   5   4   3  12  29  54  87
106  69  40  19   6   1   2  11  28  53  86
107  70  41  20   7   8   9  10  27  52  85
108  71  42  21  22  23  24  25  26  51  84
109  72  43  44  45  46  47  48  49  50  83
110  73  74  75  76  77  78  79  80  81  82
111 112 113 114 115 116 117 118 119 120 121


A wildebeest is a "gnu" fairy chess piece - a non-standard chess piece which may move both as a knight (a \$(1,2)\$-leaper) and as a camel (a \$(1,3)\$-leaper).
As such she could move to any of these locations from her starting location of \$1\$:

  .   .   .   .   .   .   .   .   .   .   .
  .   .   .   .  35   .  33   .   .   .   .
  .   .   .   .  16   .  14   .   .   .   .
  .   .  39  18   .   .   .  12  29   .   .
  .   .   .   .   .  (1)  .   .   .   .   .
  .   .  41  20   .   .   .  10  27   .   .
  .   .   .   .  22   .  24   .   .   .   .
  .   .   .   .  45   .  47   .   .   .   .
  .   .   .   .   .   .   .   .   .   .   .


The lowest of these is \$10\$ and she has not yet visited that square, so \$10\$ is the second term in the sequence.

Next she could move from \$10\$ to any of these locations:

  .   .   .   .   .   .   .   .   .   .   .
  .   .   .   .   .   .  14   .  30   .   .
  .   .   .   .   .   .   3   .  29   .   .
  .   .   .   .   6   1   .   .   .  53  86
  .   .   .   .   .   .   . (10)  .   .   .
  .   .   .   .  22  23   .   .   .  51  84
  .   .   .   .   .   .  47   .  49   .   .
  .   .   .   .   .   .  78   .  80   .   .
  .   .   .   .   .   .   .   .   .   .   .


However, she has already visited square \$1\$ so her third location is square \$3\$, the lowest she has not yet visited.



The first \$100\$ terms of the path of the wildebeest are:

1, 10, 3, 6, 9, 4, 7, 2, 5, 8, 11, 14, 18, 15, 12, 16, 19, 22, 41, 17, 33, 30, 34, 13, 27, 23, 20, 24, 44, 40, 21, 39, 36, 60, 31, 53, 26, 46, 25, 28, 32, 29, 51, 47, 75, 42, 45, 71, 74, 70, 38, 35, 59, 56, 86, 50, 78, 49, 52, 80, 83, 79, 115, 73, 107, 67, 64, 68, 37, 61, 93, 55, 58, 54, 84, 48, 76, 43, 69, 103, 63, 66, 62, 94, 57, 87, 125, 82, 118, 77, 113, 72, 106, 148, 65, 97, 137, 91, 129, 85


The first \$11\$ leaps are knight moves so the first \$12\$ terms coincide with A316667.
JavaScript (Node.js), &nbsp;191 ... 166&nbsp; 164 bytes

Saved 2 bytes thanks to @grimy.

Returns the \$N\$th term.

n=&gt;(g=(x,y)=&gt;n--?g(Buffer('QPNP1O?O@242Q3C3').map(m=c=&gt;g[i=4*((x+=c%6-2)*x&gt;(y+=c%7-2)*y?x:y)**2,i-=(x&gt;y||-1)*(i**.5+x+y)]|i&gt;m||(H=x,V=y,m=i))&amp;&amp;H,V,g[m]=1):m+1)(1,2)


Try it online! or See a formatted version

How?

Spiral indices

In order to convert the coordinates \$(x,y)\$ into the spiral index \$I\$, we first compute the layer \$L\$ with:

$$L=\max(|x|,|y|)$$

Which gives:

$$\begin{array}{c|ccccccc}
&amp;-3&amp;-2&amp;-1&amp;0&amp;+1&amp;+2&amp;+3\\
\hline
-3&amp;3&amp;3&amp;3&amp;3&amp;3&amp;3&amp;3\\
-2&amp;3&amp;2&amp;2&amp;2&amp;2&amp;2&amp;3\\
-1&amp;3&amp;2&amp;1&amp;1&amp;1&amp;2&amp;3\\
0&amp;3&amp;2&amp;1&amp;0&amp;1&amp;2&amp;3\\
+1&amp;3&amp;2&amp;1&amp;1&amp;1&amp;2&amp;3\\
+2&amp;3&amp;2&amp;2&amp;2&amp;2&amp;2&amp;3\\
+3&amp;3&amp;3&amp;3&amp;3&amp;3&amp;3&amp;3
\end{array}$$

We then compute the position \$P\$ in the layer with:

$$P=\begin{cases}
2L+x+y&amp;\text{if }x&gt;y\\
-(2L+x+y)&amp;\text{if }x\le y
\end{cases}$$

Which gives:

$$\begin{array}{c|ccccccc}
&amp;-3&amp;-2&amp;-1&amp;0&amp;+1&amp;+2&amp;+3\\
\hline
-3&amp;0&amp;1&amp;2&amp;3&amp;4&amp;5&amp;6\\
-2&amp;-1&amp;0&amp;1&amp;2&amp;3&amp;4&amp;7\\
-1&amp;-2&amp;-1&amp;0&amp;1&amp;2&amp;5&amp;8\\
0&amp;-3&amp;-2&amp;-1&amp;0&amp;3&amp;6&amp;9\\
+1&amp;-4&amp;-3&amp;-2&amp;-3&amp;-4&amp;7&amp;10\\
+2&amp;-5&amp;-4&amp;-5&amp;-6&amp;-7&amp;-8&amp;11\\
+3&amp;-6&amp;-7&amp;-8&amp;-9&amp;-10&amp;-11&amp;-12
\end{array}$$

The final index \$I\$ is given by:

$$I=4L^2-P$$

NB: The above formula gives a 0-indexed spiral.

In the JS code, we actually compute \$4L^2\$ right away with:

i = 4 * (x * x &gt; y * y ? x : y) ** 2


And then subtract \$P\$ with:

i -= (x &gt; y || -1) * (i ** 0.5 + x + y)


Moves of the wildebeest

Given the current position \$(x,y)\$, the 16 possible target squares of the wildebeest are tested in the following order:

$$\begin{array}{c|cccccccc}
&amp;-3&amp;-2&amp;-1&amp;x&amp;+1&amp;+2&amp;+3\\
\hline
-3&amp;\cdot&amp;\cdot&amp;9&amp;\cdot&amp;11&amp;\cdot&amp;\cdot\\
-2&amp;\cdot&amp;\cdot&amp;8&amp;\cdot&amp;10&amp;\cdot&amp;\cdot\\
-1&amp;7&amp;6&amp;\cdot&amp;\cdot&amp;\cdot&amp;12&amp;13\\
y&amp;\cdot&amp;\cdot&amp;\cdot&amp;\bullet&amp;\cdot&amp;\cdot&amp;\cdot\\
+1&amp;5&amp;4&amp;\cdot&amp;\cdot&amp;\cdot&amp;14&amp;15\\
+2&amp;\cdot&amp;\cdot&amp;2&amp;\cdot&amp;0&amp;\cdot&amp;\cdot\\
+3&amp;\cdot&amp;\cdot&amp;3&amp;\cdot&amp;1&amp;\cdot&amp;\cdot
\end{array}$$

We walk through them by applying 16 pairs of signed values \$(dx,dy)\$. Each pair is encoded as a single ASCII character.

 ID | char. | ASCII code | c%6-2 | c%7-2 | cumulated
----+-------+------------+-------+-------+-----------
  0 |  'Q'  |     81     |   +1  |   +2  |  (+1,+2)
  1 |  'P'  |     80     |    0  |   +1  |  (+1,+3)
  2 |  'N'  |     78     |   -2  |   -1  |  (-1,+2)
  3 |  'P'  |     80     |    0  |   +1  |  (-1,+3)
  4 |  '1'  |     49     |   -1  |   -2  |  (-2,+1)
  5 |  'O'  |     79     |   -1  |    0  |  (-3,+1)
  6 |  '?'  |     63     |   +1  |   -2  |  (-2,-1)
  7 |  'O'  |     79     |   -1  |    0  |  (-3,-1)
  8 |  '@'  |     64     |   +2  |   -1  |  (-1,-2)
  9 |  '2'  |     50     |    0  |   -1  |  (-1,-3)
 10 |  '4'  |     52     |   +2  |   +1  |  (+1,-2)
 11 |  '2'  |     50     |    0  |   -1  |  (+1,-3)
 12 |  'Q'  |     81     |   +1  |   +2  |  (+2,-1)
 13 |  '3'  |     51     |   +1  |    0  |  (+3,-1)
 14 |  'C'  |     67     |   -1  |   +2  |  (+2,+1)
 15 |  '3'  |     51     |   +1  |    0  |  (+3,+1)


We keep track of the minimum encountered value in \$m\$ and of the coordinates of the corresponding cell in \$(H,V)\$.

Once the best candidate has been found, we mark it as visited by setting a flag in the object \$g\$, which is also our main recursive function.

On the first iteration, we start with \$x=1\$ and \$y=2\$. This ensures that the first selected cell is \$(0,0)\$ and that it's the first cell to be marked as visited.