[Question]
  [
The non-negative integers are bored of always having the same two\* neighbours, so they decide to mix things up a little. However, they are also lazy and want to stay as close as possible to their original position.
They come up with the following algorithm:
* The first element is 0.
* The \$n^{th}\$ element is the smallest number which is not yet present in the sequence and which is not a neighbour of the \$(n-1)^{th}\$ element.
This generates the following infinite sequence:
```
0,2,4,1,3,5,7,9,6,8,10,12,14,11,13,15,17,19,16,18,20,22,24,21,23,25,27,29,26,28 ...
```
`0` is the first element. `1` is the smallest number not yet in the sequence, but it is a neighbour of `0`. The next smallest number is `2`, so it is the second element of the sequence. Now the remaining numbers are `1,3,4,5,6,...`, but as both `1` and `3` are neighbours of `2`, `4` is the third member of the sequence. As `1` is not a neighbour of `4`, it can finally take its place as fourth element.
### The Task
Write a function or program in as few bytes as possible which generates the above sequence. 
You may
* output the sequence infinitely,
* take an input \$n\$ and return the \$n^{th}\$ element of the sequence, or
* take an input \$n\$ and return the first \$n\$ elements of the sequence.
Both zero- or one-indexing is fine in case you choose one of the two latter options.
You don't need to follow the algorithm given above, any method which produces the same sequence is fine.
---
Inspired by [Code golf the best permutation](https://codegolf.stackexchange.com/questions/164838/code-golf-the-best-permutation). Turns out this is [A277618](https://oeis.org/A277618).  
\* Zero has literally only one neighbour and doesn't really care.
  
[Answer]
# JavaScript (ES6), 13 bytes
Returns the \$n\$th term of the sequence.
```
n=>n-2-~++n%5
```
[Try it online!](https://tio.run/##BcHRCkAwFADQX9mL3Bu7oTxO@Q5Ja0bEvWukvPj1OWe3j71c3MKtWWafFpPYdKwb/RUFZ21ywpccng5ZYSCiPkb7Ql1VONJpA8BUKkZlOrUAI9IuG0Ne5ojpBw "JavaScript (Node.js) – Try It Online")
### How?
This computes:
$$n-2+((n+2) \bmod 5)$$
```
           n |  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 ...
-------------+--------------------------------------------------
       n - 2 | -2 -1  0  1  2  3  4  5  6  7  8  9 10 11 12 ...
 (n+2) mod 5 |  2  3  4  0  1  2  3  4  0  1  2  3  4  0  1 ...
-------------+--------------------------------------------------
         sum |  0  2  4  1  3  5  7  9  6  8 10 12 14 11 13 ...
```
[Answer]
# [Python 2](https://docs.python.org/2/), 20 bytes
```
lambda n:2*n%5+n/5*5
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZhvzPycxNyklUSHPykgrT9VUO0/fVMv0f0FRZl6JQnSaRqamQlp@kUKmQmaeQlFiXnqqhomBZux/AA "Python 2 – Try It Online")
[Answer]
# [MathGolf](https://github.com/maxbergmark/mathgolf), 5 bytes
```
⌠5%+⌡
```
[Try it online!](https://tio.run/##y00syUjPz0n7//9RzwJTVe1HPQv//zfgMuQy4jLmMuEy5TLjMueyAAA "MathGolf – Try It Online")
Some nice symmetry here. Returns the `nth` element of the sequence.
### Explanation:
```
⌠      Increment input by 2
 5%    Modulo by 5
   +   Add to copy of input
    ⌡  Decrement by 2
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
```
æ%2.+
```
[Try it online!](https://tio.run/##y0rNyan8///wMlUjPe3/RgYPd2w73P6oac1/AA "Jelly – Try It Online")
Go go gadget obscure built-in!
```
æ%2.      Symmetric modulo 5: map [0,1,2,3,4,5,6,7,8,9] to [0,1,2,-2,-1,0,1,2,-2,-1]
    +     Add to input
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 14 bytes
```
#+Mod[#,5,-2]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78H1CUmVcS/V9Z2zc/JVpZx1RH1yhW7b@@g0JQYl56arSBjrFBbOx/AA "Wolfram Language (Mathematica) – Try It Online")
Prints the n-th, zero-indexed, integer in the sequence.
[Answer]
# [R](https://www.r-project.org/), ~~25~~ ~~23~~ 21 bytes
-2 bytes thanks to Jo King
```
n=scan();n-2+(n+2)%%5
```
[Try it online!](https://tio.run/##K/r/P8@2ODkxT0PTOk/XSFsjT9tIU1XV9L/hfwA "R – Try It Online")
Outputs `nth` element in sequence.
[Answer]
# [dzaima/APL](https://github.com/dzaima/APL), 9 bytes
```
2-⍨⊢+5|2+
```
Port of Arnauld's answer.
[Try it online!](https://tio.run/##SyzI0U2pSszMTfz/30j3Ue@KR12LtE1rjLT/P@qb6un/qG2CAVcakESVS3vUu9nQ4F9@QUlmfl7xf91iAA)
[Answer]
# [Pip](https://github.com/dloscutoff/pip), 14 bytes
```
02413@a+a//5*5
```
Takes \$n\$ (0-based) as a command-line argument and outputs \$a\_n\$. [Try it online!](https://tio.run/##K8gs@P/fwMjE0NghUTtRX99Uy/T///8mAA "Pip – Try It Online")
Observe that \$a\_{n+5} = a\_n+5\$. We hard-code the first five values and offset from there.
---
Or, the formula everyone's using, for **12 bytes**:
```
a-2+(a+2)%5
```
[Answer]
# [Common Lisp](http://www.clisp.org/), 67 bytes
```
(defun x(n)(loop for a from 0 to n collect(+(mod(+ a 2)5)(- a 2))))
```
[Try it online!](https://tio.run/##HYlbCoAgFAW3cj6PSGBBCwofEFy9Ygbu3qT5mmG83E@dkyGmt2CwGIpqRdKGC6lphkNXFHgVib7TMmugXfcwp@H2y2Kytrt0Duxu1Qc "Common Lisp – Try It Online")
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 8 bytes
```
U-2Ò°U%5
```
[Japt Interpreter](https://ethproductions.github.io/japt/?v=1.4.6&code=VS0y0rBVJTU=&input=MTAwCi1t)
A straight port of Arnauld's Javascript answer. The linked version runs through the first n elements, but if the `-m` flag is removed it is still valid and prints the nth element instead.
For comparison's sake, [here](https://ethproductions.github.io/japt/?v=1.4.6&code=z3tZYVggyata+Fl9YX1nVSxbMF0=&input=MTAwCi1t) is the naive version which implements the algorithm provided in the question:
```
@_aX É«NøZ}a}gNhT
```
I'll give an explanation for this one:
```
              NhT    Set N to [0]
@           }g       Get the nth element of N by filling each index with:
 _        }a          The first integer that satisfies:
  aX É                 It is not a neighbor to the previous element
      «NøZ             And it is not already in N
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 5 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
Ì5%+Í
```
Port of [@JoKing's MathGolf answer](https://codegolf.stackexchange.com/a/174729/52210).
[Try it online](https://tio.run/##yy9OTMpM/f//cI@pqvbh3v//jUwA) or [verify the first 100 numbers](https://tio.run/##yy9OTMpM/X@x6fBcl/@He0xVtQ/3/v8PAA).
**Explanation:**
```
Ì        # Increase the (implicit) input by 2
 5%      # Take modulo-5
   +     # Add the (implicit) input to it
    Í    # Decrease by 2 (and output implicitly)
```
[Answer]
# [Clean](https://github.com/Ourous/curated-clean-linux), 31 bytes
The formula everyone's using.
```
import StdEnv
?n=n-2+(n+2)rem 5
```
[Try it online!](https://tio.run/##S85JTcz7n5ufUpqTqpCbmJn3PzO3IL@oRCG4JMU1r4zLPs82T9dIWyNP20izKDVXwfR/cEkiUNpWwd7I4P@/5LScxPTi/7qePv9dKvMSczOTiwE "Clean – Try It Online")
# [Clean](https://github.com/Ourous/curated-clean-linux), 80 bytes
My initial approach, returning the first `n` items.
```
import StdEnv
$n=iter n(\l=l++[hd[i\\i<-[0..]|all((<>)i)l&&abs(i-last l)>1]])[0]
```
[Try it online!](https://tio.run/##DcwxDsIgFADQ3VMwNA2koUHntlMdTNw6AsMXUH/yoaagiYlnF30HeI4CpBpX/6TAImCqGB/rVthS/DG9dk0asYSNJW5opK7Td6/RGBykVn1vP0DE@TAJFNS2cMkcJUEujMS0t1ZoZetS4P@NrDmo@nVXgluu8nSu8ztBRJd/ "Clean – Try It Online")
[Answer]
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 14 bytes
```
n->2*n%5+n\5*5
```
[Try it online!](https://tio.run/##K0gsytRNL/ifpmCr8D9P185IK0/VVDsvxlTL9H9afpFGHlDcQEfBGIgLijLzSoACSgq6dkAiTSNPU1PzPwA "Pari/GP – Try It Online")
---
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 14 bytes
```
n->n-2+(n+2)%5
```
[Try it online!](https://tio.run/##K0gsytRNL/ifpmCr8D9P1y5P10hbI0/bSFPV9H9afpFGHlDcQEfBGIgLijLzSoACSgq6dkAiTSNPU1PzPwA "Pari/GP – Try It Online")
[Answer]
# [J](http://jsoftware.com/), 30 bytes
```
{.2}.[:,_5,./\2(i.-4 0$~])@,~]
```
[Try it online!](https://tio.run/##y/qfVmyrp2CgYKVg8L9az6hWL9pKJ95UR08/xkgjU0/XRMFApS5W00GnLva/JpeSnoJ6mq2euoKOQq2VQloxF1dqcka@QpqCoYHBfwA "J – Try It Online")
Returns a list of first `n` numbers
This solution is obviously non-competitve, but I wanted to try an array-based method.
## Explanation:
The argument is `n`
`2 ,]` - append 2 to the input
```
   (2,~]) 10
10 2
```
`()@` - and use this list to:
`i.` - create a matrix `n` x 2 with the numbers in the range 0..2n-1:
```
   i.10 2
 0  1
 2  3
 4  5
 6  7
 8  9
10 11
12 13
14 15
16 17
18 19
```
`4 0$~]` - `~` reverses the arguments, so it is ]$4 0 - creates matrix `n` x 2 repeating 4 0
```
   4 0$~10 2
4 0
4 0
4 0
4 0
4 0
4 0
4 0
4 0
4 0
4 0
```
`-` subtract the second matrix from the first one, so that the first column is "delayed" with 2 positions
```
   2(i.-4 0$~])@,~] 10
_4  1
_2  3
 0  5
 2  7
 4  9
 6 11
 8 13
10 15
12 17
14 19
```
`_5,./\` traverse the matrix in non-overlapping groups of 5 rows and stitch the columns
```
   _5,./\2(i.-4 0$~])@,~] 10
_4 _2  0  2  4
 1  3  5  7  9
 6  8 10 12 14
11 13 15 17 19
```
`[:,` ravel the entire array
```
   ,_5,./\2(i.-4 0$~])@,~] 10
_4 _2 0 2 4 1 3 5 7 9 6 8 10 12 14 11 13 15 17 19
```
`2}.` - drop the first 2 numbers
```
   2}.,_5,./\2(i.-4 0$~])@,~] 10
0 2 4 1 3 5 7 9 6 8 10 12 14 11 13 15 17 19
```
`{.` take the first `n` numbers
```
   ({.2}.[:,_5,./\2(i.-4 0$~])@,~]) 10
0 2 4 1 3 5 7 9 6 8
```
# [J](http://jsoftware.com/), 9 bytes
```
+_2+5|2+]
```
[Try it online!](https://tio.run/##y/qfVmyrp2CgYKVg8F873kjbtMZIO/a/JpeSnoJ6mq2euoKOQq2VQloxV2pyRr5CmoIFjKFmp5CpZ2hg8B8A "J – Try It Online")
Returns the `n`th element.
Port of Arnauld's answer
[Answer]
# [K (ngn/k)](https://gitlab.com/n9n/k), 12 bytes
```
{-2+x+5!x+2}
```
[Try it online!](https://tio.run/##y9bNS8/7/z/NqlrXSLtC21SxQtuo9n@auqKhwX8A "K (ngn/k) – Try It Online")
[Answer]
# [Pepe](//github.com/Soaku/Pepe), 65 bytes
```
REeeeeeeEerEeERRrEEEEEERREeeeeeEeErEEeEeErRrEEEEEEEErRrEEEEEereEE
```
[Try it online!](https://soaku.github.io/Pepe/#!6tQ--&2-!6z_i@-Se@-&0M)
Port of [Jo King's answer.](/a/174729/)
[Answer]
# x86 machine code, 16 bytes
```
00000000: 31d2 89c8 4949 4040 b305 f7f3 9201 c8c3 1...II@@........
```
Assembly:
```
section .text
	global func
func:	;function uses fastcall conventions, 1st arg in ecx, returns in eax
	;reset edx to 0 so division works
	xor edx, edx
	mov eax, ecx
	;calculate ecx (1st func arg) - 2
	dec ecx
	dec ecx
	;calculate (ecx+2) mod 5
	inc eax
	inc eax
	mov bl, 5
	div ebx
	xchg eax, edx
	
	;add (ecx-2) and ((ecx+2) mod 5), returning in eax
	add eax, ecx
	ret
```
[Try it online!](https://tio.run/##bVPbbtswDH22voIIWsROotzaYVid9WFYh73tA@YhkCUm8WpLgSQnHrJ8e0Y5TRsXe5FE8hzyUKJy4TYnKTwsFk8/vsEjTHy1nYixcBWkoMPGV1iu7ubXEW7OljQKx4aAKDcGegwbj1ZDoT0sl8J7W@S1x@UyjlfCeSnKMklgVWsZEyRJgQVkJQodJwe2Mja4ofg8TYvFbErrcEj@rSXvKs56tyrTWW905idJyo7s2LtIdr7OxzLNWMbWUnYVkv5rUEc714ZvCwRe3c1btsl/q7raAv/awVEZhTvKoNDaLtB1gH/BI16DybO2SLgn6P@c8k@/DvdH6JNX1h54Dh/5/TywLHAF/Uz334wW1qYLBTbYjH3jybWnBiXtYv8M/UN7QXAzm8yPAX@@L@hlejAYfPnjEaSptX8gK9O3LtM9uInDk/PkP121L8nxhf4dm9Bj4NJNX2OhaRRwkrl9Ly4EHt@n7TzGyaH0hdEw9jQxLFqXJhdlOxgsLA9RGrYWUjt0cJke6kTvUIeAG8HMeRB2TeNG89eMwKKvrXatLRoWpRYdekDVgDcwBWdAFbvChbR7Y58dixpjQ3wUFsaiyuwCdRTyEZ9KyroUdINkQxzqBV2haAIc5ixSKM/Yy6HDiskznCdQGQUfWFQQtRX2egj18nIUgqQMMCdfIzfrFxGkKaKEQqk2FadUQtO5kze5NF7o9WvrgfLWCMVP9LtP/wA "Try It Online")
[Answer]
# [Red](http://www.red-lang.org), 26 bytes
```
func[n][n + 2 % 5 + n - 2]
```
[Try it online!](https://tio.run/##FYqxCoAwDAX3fsWjICgiVMHFz3ANGaRNoUuQ0H5/rdPB3ZmkfksidvnquWkkZVKsODDhHFRsOLi/VrQiYw/O5JWnouAM9GuYpBYFNPJcxr4v8B4M7h8 "Red – Try It Online")
Port of Arnauld's answer
[Answer]
# Excel, 17 bytes
```
=A1-2+MOD(A1+2,5)
```
Nothing smart. Implements the common formula.
[Answer]
# [C (gcc)](https://gcc.gnu.org/) POSIX, 20 bytes
```
f(n){n=n-2+(n+2)%5;}
```
[Try it online!](https://tio.run/##HYpBDoMgEADP8oqNjQmE0jSmnrD9Rs8GxG6Ci0E8Gb9eij1NMjNGTcbkC5Lxmx2hX5PFcPu8suMk9jimLRKQaiUn2Yqm00dGSjAPSFzAzioXIvBTITzhrgt6eJyU8t@rJZbqeN1YqK/gOAqhWXWwI3@N88O0ZvWmoHBePBpMqtw/ "C (gcc) – Try It Online")
[Answer]
# QBasic, 30 bytes
```
INPUT x 
x=x+2 
?-4+x*2-(x\5)*5
```
Gives the 0-indexed entry of the list at pos `x`.
[Try it online!](https://repl.it/@pimsteenbergh/NeighboringHeavyVerification) (Note that `?` was expanded to `PRINT` because the interpreter fails otherwise...)
[Answer]
# [C# (Visual C# Interactive Compiler)](http://www.mono-project.com/docs/about-mono/releases/5.0.0/#csc), 14 bytes
```
n=>2*n%5+n/5*5
```
[Try it online!](https://tio.run/##BcGxCoAgEADQfwmC08wkcqytJZpqaDY55MAuUPt@e8/n3meqV6KCOzFCLok46O0lhkY1auXvweTuiPpwHBCMmozQJ0b0BSrPyyi5tR0PVtoqhKg/ "C# (Visual C# Interactive Compiler) – Try It Online")
Same logic than others answers: [1](https://codegolf.stackexchange.com/questions/174706/new-neighbour-sequence/174751#174751) [2](https://codegolf.stackexchange.com/questions/174706/new-neighbour-sequence/174708#174708)
[Answer]
# TI-BASIC, 11 bytes
```
Ans-2+remainder(Ans+2,5
```
Input is in `Ans`.  
Outputs \$a(n)\$.
A simple port of the other answers.
---
**Note:** TI-BASIC is a tokenized language. Character count does ***not*** equal byte count.
[Answer]
# [R](https://www.r-project.org/), 25 bytes
```
n=1:scan()-1;n-2+(n+2)%%5
```
[Try it online!](https://tio.run/##K/r/P8/W0Ko4OTFPQ1PX0DpP10hbI0/bSFNV1fS/oYHBfwA "R – Try It Online")
Port of Robert S.'s answer (and only by adding just 4 bytes) thanks to R being excellent at handling vectors.
Outputs the first n values.
[Answer]
# [dc](https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html), 9 bytes
```
d2+5%+2-p
```
[Try it online!](https://tio.run/##S0n@b2j4P8VI21RV20i34P9/AA "dc – Try It Online")
Same method as most. Duplicate top-of-stack, add 2, mod 5, add to original (duplicated earlier), subtract 2, print.
]