[Question]
  [
Golf a program or function which gives the \$n^{\text{th}}\$ location of the [wildebeest](https://en.wikipedia.org/wiki/Wildebeest_Chess "Wikipedia") who starts at square \$1\$ on an infinite [chessboard](https://en.wikipedia.org/wiki/Chessboard "Wikipedia") 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](https://youtu.be/RGQe8waGJ4w "Numberphile, YouTube") and [OEIS A316667](https://oeis.org/A316667 "The On-Line Encyclopedia of Integer Sequences").
Edit: This sequence is now on the OEIS as [A323763](https://oeis.org/A323763 "The On-Line Encyclopedia of Integer Sequences").
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](/questions/tagged/code-golf "show questions tagged '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](https://github.com/Davidebyzero/path-of-the-wildebeest "GitHub") 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](https://en.wikipedia.org/wiki/Wildebeest_Chess "Wikipedia") is a "gnu" [fairy chess piece](https://en.wikipedia.org/wiki/Fairy_chess_piece "Wikipedia") - a non-standard chess piece which may move both as a [knight](https://en.wikipedia.org/wiki/Knight_(chess) "Wikipedia") (a \$(1,2)\$-leaper) and as a [camel](https://en.wikipedia.org/wiki/Camel_(chess) "Wikipedia") (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](https://oeis.org/A316667 "The On-Line Encyclopedia of Integer Sequences").
  
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~191 ... 166~~ 164 bytes
*Saved 2 bytes thanks to [@grimy](https://codegolf.stackexchange.com/users/6484/grimy).*
Returns the \$N\$th term.
```
n=>(g=(x,y)=>n--?g(Buffer('QPNP1O?O@242Q3C3').map(m=c=>g[i=4*((x+=c%6-2)*x>(y+=c%7-2)*y?x:y)**2,i-=(x>y||-1)*(i**.5+x+y)]|i>m||(H=x,V=y,m=i))&&H,V,g[m]=1):m+1)(1,2)
```
[Try it online!](https://tio.run/##FYzNasJAFEb3PsUsrN47P8GJtgXtnZR246rqxo0IhjQJU5yJxLZkSHz2GFfnO/BxftL/9JrV9vKrfPWd9wX1ngyUBI0MSMYrlZTw8VcUeQ3T3fZrqzfJ5j1exLv553yKkUsv4CgjUx4sLThAIyh7elEx8sZAeMjrQ0LSLANyHkurhrgJXac0crCcR8@iEQGPnTWu62BNjdxTkI4s4mSylntZHtyRNC6d0AhaxtgXVQ2eEdMr5tnbwNlsWEIga0eMZZW/Vuc8OlclnFIYt/6Gw3ncFuDxdsLV6NbfAQ "JavaScript (Node.js) – Try It Online") or [See a formatted version](https://tio.run/##XVFNb4JAFLzzK@Zg9a18RNC2iRZp2ounqhcvxkRCgdDIYrBtJOBvx0cLBXvaefPmzc7b/XC/3ZOXRsdPXSbvfhnYpYQ9BylACBt01pAJZrgGpK7D@UHcpRoAL19B4Kc0WK/eVubSWT5bE2s9fh0PxJ/EiN0jxWzoNV61y7ZTABErJhiCblhwDKjV7B0eoMMSLDmDU2YN/djQGRxuTZHdOHBnCEv7d5de7cc2GYoCulmNU1QpjXuo7KLy6p2RXQcXPD5H3GUK0ILj84Nt@Mg0VOtGrYFAv49Fm2HTwnAb71hs1sTvzLSuYs5hKoJMjTcsgyQlWWlnkHjiczRipKoCOeu9RJ6Sg28ckpD2LvVyeeHPQy8PSIrLXsyUS3kF)
## 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}
&-3&-2&-1&0&+1&+2&+3\\
\hline
-3&3&3&3&3&3&3&3\\
-2&3&2&2&2&2&2&3\\
-1&3&2&1&1&1&2&3\\
0&3&2&1&0&1&2&3\\
+1&3&2&1&1&1&2&3\\
+2&3&2&2&2&2&2&3\\
+3&3&3&3&3&3&3&3
\end{array}$$
We then compute the position \$P\$ in the layer with:
$$P=\begin{cases}
2L+x+y&\text{if }x>y\\
-(2L+x+y)&\text{if }x\le y
\end{cases}$$
Which gives:
$$\begin{array}{c|ccccccc}
&-3&-2&-1&0&+1&+2&+3\\
\hline
-3&0&1&2&3&4&5&6\\
-2&-1&0&1&2&3&4&7\\
-1&-2&-1&0&1&2&5&8\\
0&-3&-2&-1&0&3&6&9\\
+1&-4&-3&-2&-3&-4&7&10\\
+2&-5&-4&-5&-6&-7&-8&11\\
+3&-6&-7&-8&-9&-10&-11&-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 > y * y ? x : y) ** 2
```
And then subtract \$P\$ with:
```
i -= (x > 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}
&-3&-2&-1&x&+1&+2&+3\\
\hline
-3&\cdot&\cdot&9&\cdot&11&\cdot&\cdot\\
-2&\cdot&\cdot&8&\cdot&10&\cdot&\cdot\\
-1&7&6&\cdot&\cdot&\cdot&12&13\\
y&\cdot&\cdot&\cdot&\bullet&\cdot&\cdot&\cdot\\
+1&5&4&\cdot&\cdot&\cdot&14&15\\
+2&\cdot&\cdot&2&\cdot&0&\cdot&\cdot\\
+3&\cdot&\cdot&3&\cdot&1&\cdot&\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.
[Answer]
# [Coconut](http://coconut-lang.org/), ~~337~~ 276 bytes
```
import math
def g((x,y))=
 A=abs(abs(x)-abs(y))+abs(x)+abs(y)
 int(A**2+math.copysign(A+x-y,.5-x-y)+1)
def f():
 p=x,y=0,0;s={p};z=[2,3,1,1]*2
 while 1:yield g(p);p=x,y=min(((a+x,b+y)for a,b in zip((1,1,2,-2,-1,-1,3,-3)*2,z+[-v for v in z])if(a+x,b+y)not in s),key=g);s.add(p)
```
Returns a generator of values. Could probably be golfed more. (Especially the sequence of difference tuples.) Spiral algorithm taken from [this math.se answer](https://math.stackexchange.com/a/2639611/293996).
[Try it online!](https://tio.run/##PVDbboMwDH3PV/gxJg4Cqr0UpVK/o@oD5dZoQCKSddBt386SMk3yRceyj49dm9pMH37b9GjN7GGs/J01bQc95wutiIrBWVU3x6MvKGMKZbFDsUMGevL8nCSFiARpbezqdD/xs1jkSjIEkaVvKHJ8kXccjwysChtURlnp1Jf9KZ/qUtCBcsqvScHg866HFvLjqtuhCXoslvvEqCfOeSUWuokVOzNDRbegAJ7ach7GqSAZLI92IHnApKCnuMgHxObHq/WKuvvnmIyPRYf03q6qx9KlVdOEjVs3mxG0b2dvzODg70sJY9oNum55uIQgzzKE7xMM2vmY7Rzesf0C "Coconut – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~77~~ ~~65~~ ~~58~~ ~~57~~ 52 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
Xˆ0UF3D(Ÿ0KãʒÄ1¢}εX+}Dε·nàDtyÆ+yO·<.±*->}D¯KßDˆkèU}¯
```
-6 bytes thanks to *@Arnauld* by using a port of his formula.
Outputs the first \$n+1\$ values as a list (of decimals).
[Try it online](https://tio.run/##AVQAq/9vc2FiaWX//1jLhjBVRjNEKMW4MEvDo8qSw4QxwqJ9zrVYK31EzrXCt27DoER0ecOGK3lPwrc8LsKxKi0@fUTCr0vDn0TLhmvDqFV9wq//w6//MTU) (the `ï` in the footer removes the `.0` to make the output more compact, but feel free to remove it to see the actual result).
**Code explanation:**
```
Xˆ             # Put integer 1 in the global_array (global_array is empty by default)
0U             # Set variable `X` to 0 (`X` is 1 by default)
F              # Loop the (implicit) input amount of times:
 3D(Ÿ          #  Push the list in the range [-3,3]: [-3,-2,-1,0,1,2,3]
     0K        #  Remove the 0: [-3,-2,-1,1,2,3]
       ã       #  Cartesian product with itself, creating each possible pair: [[3,3],[3,2],[3,1],[3,-1],[3,-2],[3,-3],[2,3],[2,2],[2,1],[2,-1],[2,-2],[2,-3],[1,3],[1,2],[1,1],[1,-1],[1,-2],[1,-3],[-1,3],[-1,2],[-1,1],[-1,-1],[-1,-2],[-1,-3],[-2,3],[-2,2],[-2,1],[-2,-1],[-2,-2],[-2,-3],[-3,3],[-3,2],[-3,1],[-3,-1],[-3,-2],[-3,-3]]
        ʒ   }  #  Filter this list of pairs by:
         Ä     #   Where the absolute values of the pair
          1¢   #   Contains exactly one 1
               #  (We now have the following pairs left: [[3,1],[3,-1],[2,1],[2,-1],[1,3],[1,2],[1,-2],[1,-3],[-1,3],[-1,2],[-1,-2],[-1,-3],[-2,1],[-2,-1],[-3,1],[-3,-1]])
 εX+}          #  Add the variable `X` (previous coordinate) to each item in the list
 D             #  Duplicate this list of coordinates
  ε            #  Map each `x,y`-coordinate to:
   ·           #   Double both the `x` and `y` in the coordinate
    n          #   Then take the square of each
     à         #   And then pop and push the maximum of the two
   Dt          #   Duplicate this maximum, and take its square-root
     yÆ        #   Calculate `x-y`
       +       #   And add it to the square-root
   yO          #   Calculate `x+y`
     ·         #   Double it
      <        #   Decrease it by 1
       .±      #   And pop and push its signum (-1 if < 0; 0 if 0; 1 if > 0)
   *           #   Multiply these two together
    -          #   And subtract it from the duplicated maximum
   >           #   And finally increase it by 1 to make it 1-based instead of 0-based
  }D           #  After the map: Duplicate that list with values
    ¯K         #  Remove all values that are already present in the global_array
      ß        #  Pop the list of (remaining) values and push the minimum
       Dˆ      #  Duplicate this minimum, and pop and add the copy to the global_array
         k     #  Then get its index in the complete list of values
          è    #  And use that index to get the corresponding coordinate
           U   #  Pop and store this coordinate in variable `X` for the next iteration
}¯             # After the outer loop: push the global_array (which is output implicitly)
```
**General explanation:**
We hold all results (and therefore values we've already encountered) in the `global_array`, which is initially started as `[1]`.  
We hold the current \$x,y\$-coordinate in variable `X`, which is initially `[0,0]`.
The list of coordinates we can reach based on the current \$x,y\$-coordinate are:
```
[[x+3,y+1], [x+3,y-1], [x+2,y+1], [x+2,y-1], [x+1,y+3], [x+1,y+2], [x+1,y-2], [x+1,y-3], [x-1,y+3], [x-1,y+2], [x-1,y-2], [x-1,y-3], [x-2,y+1], [x-2,y-1], [x-3,y+1], [x-3,y-1]]
```
The list I mention in the code explanation above holds these values we can jump to, after which the current \$x,y\$ (stored in variable `X`) is added.
Then it will calculate the spiral values based on these \$x,y\$-coordinates. It does this by using the following formula for a given \$x,y\$-coordinate:
$${T = max((2 \* x) ^ 2, (2 \* y) ^ 2)}$$
$${R = T - (x - y + √T) \* signum((x + y) \* 2 - 1) + 1}$$
Which is the same formula [*@Arnauld* is using in his answer](https://codegolf.stackexchange.com/a/179158/52210), but written differently to make use of 05AB1E's builtins for double, square, -1, +1, etc.
(If you want to see just this spiral part of the code in action: [Try it online](https://tio.run/##PY2xDcJQDERXiVJCjPy/KRFp6BkgigRIFDRQBCGlQKLKAJRUrECBQp30DJFFPuDzp7ln687nQ7Xe7LYhvJ9du@/vi2PdN@N62bWzSfcY0fw8XG7961SvqjxNhuaapHkWQlFwxmVWOFP3VVYlF8EAwVPY5iNYFbNX32vY41ThIxzAANlKliVcEmoUZsofTsGqmGGIhkUvRWsEpYIfConwgAMYIFvJXLJTsiZCL@EJ4aPCstMfyg8).)
After we've got all the values we can reach for the given \$x,y\$-coordinate, we remove all values that are already present in the `global_array`, and we then get the minimum of the (remaining) values.  
This minimum is then added to the `global_array`, and variable `X` is replaced with the \$x,y\$-coordinate of this minimum.
After we've looped the `input` amount of times, the program will output this `global_array` as result.
]