# Introduction

In chess, Knights can move two squares in one direction, and then one square in an orthogonal direction. On an x/y plane, these can be thought of as being a relative change of coordinates, such as (+1, -2).

# Challenge

Your task is to output all 8 possible moves as pairs of relative x/y coordinates, in any order.

You may output in any reasonable format. To decrease reliance on printing random separators, using the same separators for everything is fine, and no separators are also fine. Standard loopholes apply.

Example Outputs:

1,2 1,-2 -1,-2 -1,2 2,1 2,-1 -2,-1 -2,1

2-112-2-1-211-2-1-2-1221

1;2
-1;2
2;1
2;-1
-2;-1
1;-2
-1;-2
-2;1


This is code golf, so fewest bytes wins.

• Can we display the sign for positive values? May 23, 2022 at 5:39
• This seems reasonable, especially since in most languages generating/printing the extra + would cost bytes. I will allow it. May 23, 2022 at 5:42
• I would definitely recommend not allowing the "no separator" output. For non-golf languages, printing that string verbatim ought to win automatically. May 23, 2022 at 5:51
• Related May 23, 2022 at 6:24
• @dingledooper - Happy to report that in at least two non-golf language (R & Python), non-trivial approaches can still compete... May 23, 2022 at 14:56

# Wolfram Language (Mathematica), 25 bytes

Solve[x^2y^2==4,Integers]


Try it online!

Outputs a list of {x -> x, y -> y}s.

• Yet another clean generation process. Very nice! May 23, 2022 at 23:55

# Python, 30 bytes

print((2*"2212-1-21-2-1")[2:])

Attempt This Online!

A mashup of my previous and the trivial solutions.

# Python, 41 bytes

a=1
for b in b"41030143":print(a,a:=b-50)

Attempt This Online!

Abuses the dual stringy/inty nature of bytes objects. Arranges items such that a sliding 2-window finds all required pairs.

# J, 18 bytes

(,+)@(1j2*0j1^i.4)


Try it online!

A function taking any input and returning the list we want as complex numbers.

• 0j1^i.4 $$\i\$$ raised to 0, 1, 2, 3 -- gives the 4 compass directions in the complex plane: 1 0j1 _1 0j_1.
• 1j2* Multiply $$\(1 + 2i)\$$ times each of those, and we get $$\(1 + 2i)\$$ rotated 0, 90, 180, and 270 degrees: 1j2 _2j1 _1j_2 2j_1.
• (,+) Take the complex conjugates of each of those, and append them: 1j2 _2j1 _1j_2 2j_1 1j_2 _2j_1 _1j2 2j1.
• Wow! Awesome approach. May 23, 2022 at 4:24

# Jelly, 6 bytes

Ø+pḤUƬ


A niladic Link that yields this pair of lists of pairs of integers:

[[[1, 2], [1, -2], [-1, 2], [-1, -2]], [[2, 1], [-2, 1], [2, -1], [-2, -1]]]


Try it online!

### How?

Ø+pḤUƬ - Link: no arguments
Ø+     - literal [1,-1] - set as the left argument
Ḥ   - double -> [2,-2]
p    - ([1,-1]) Cartesian product ([2,-2]) -> [[1,2],[1,-2],[-1,2],[-1,-2]]
Ƭ - collect up inputs while distinct, applying:
U  -   reverse each


Original seven byter (not abusing the very lose IO):

Ø+pḤ;U$- Link: no arguments Ø+ - literal [1,-1] - set as the left argument Ḥ - double -> [2,-2] p - ([1,-1]) Cartesian product ([2,-2]) -> [[1,2],[1,-2],[-1,2],[-1,-2]]$ - last two links as a monad - f(X=that):
U  -   reverse each of (X)
;   -   (X) concatenate (that)


Try it online!

• Lots of cool answers using the Cartesian product! In keeping with the very loose output rules I think your two odd formats are probably okay. May 23, 2022 at 12:52

# Jelly, 9 8 bytes

Ø+p×Ø½UƬ


Try it online!

-1 by stretching the output format a bit

Ø+p         Cartesian product of [1, -1] with itself.
×Ø½      Multiply each pair with [1, 2],
UƬ    and pair with the reversed pairs.


# Jelly, 9 bytes

ı*Ɱ4×2ıṠƬ


Try it online!

Port of Jonah's J solution.

# Jelly, 11 bytes

“ç©Ḅ’b5_2ṡ2


Try it online!

Port of loopy walt's Python solution.

# Jelly, 12 bytes

“œƇØj_’ṃ“21-


Try it online!

A fully hardcoded, no separator solution for comparison.

• The 9 byte solution is very cool! Great to see it beats the 'trivial' solution too :) May 23, 2022 at 6:58

# Factor + math.combinatorics math.unicode, 47 bytes

{ 1 -1 2 -2 } 2 selections [ Σ odd? ] filter .


Try it online!

## How?

Generate all 2-selections of { 1 -1 2 -2 }:

{ 1 1 }
{ 1 -1 }
{ 1 2 }
{ 1 -2 }
{ -1 1 }
{ -1 -1 }
{ -1 2 }
{ -1 -2 }
{ 2 1 }
{ 2 -1 }
{ 2 2 }
{ 2 -2 }
{ -2 1 }
{ -2 -1 }
{ -2 2 }
{ -2 -2 }


and keep only those with odd sums:

{ 1 2 }
{ 1 -2 }
{ -1 2 }
{ -1 -2 }
{ 2 1 }
{ 2 -1 }
{ -2 1 }
{ -2 -1 }


# R, 423534 33 bytes

round(2*exp((1:2+0:7%/%2*pi)/2i))


Try it online!

Outputs as complex numbers.

• Gotcha!... your turn now... May 23, 2022 at 6:47
• @DominicvanEssen challenge accepted. May 23, 2022 at 7:52
• Dang! Well done! May 23, 2022 at 9:37
• Your turn again... May 23, 2022 at 14:43
• I consider myself beaten, good job! May 23, 2022 at 15:06

# R, 372926 25 / 23* bytes

Edit: -1 byte thanks to pajonk
*Edit 2: Or -2 more bytes with output as elements of a 2x4 matrix, thanks to Giuseppe

(1+.5:-1*4i)*1i^(2:9%/%2)


Try it online!

Change of approach to try to compete with pajonk; now outputs as complex numbers.
Still frustratingly 3 bytes longer than Now the same length as Satisfyingly now shorter than a hard-coded string...

• No need for c May 23, 2022 at 15:07
• @pajonk - Super! Thanks a lot! We've finally beaten the hard-coded string! May 23, 2022 at 15:16
• Maybe (1+.5:-1*4i)%o%1i^(1:4) for a slightly more stretched output format, 23 bytes May 23, 2022 at 16:03
• @Giuseppe - Even better! This has been a bit of a battle, so would you like to post that yourself...? May 23, 2022 at 16:14
• @DominicvanEssen nah, my first attempt without peeking was much longer, so this is still essentially your solution. May 23, 2022 at 19:46

# MathGolf, 9 bytes

2╤ç■gÉ±~¡


Try it online.

Explanation:

2╤         # Push a list in the range [-2,2]: [-2,-1,0,1,2]
ç        # Remove the 0 with a falsey filter
■       # Cartesian product with itself
g      # Filter these pairs by,
É     # using three characters as inner code-block:
±    #  Take the absolute values of the pair
~   #  Pop and push the values separated to the stack
¡  #  Check that they are NOT equal
# (after which the entire stack is output implicitly as result)


# Brachylog, 9 bytes

12ẹ{ṅ|}ᵐp


Try it online!

Generates each move.

   {  }ᵐ     For each element of
ẹ          the digits of
12           12,
|       maybe
ṅ        negate it.
p    Output a permutation of the result.


Although ᵐ maps over digits if given a raw integer, it also smashes the results back into an integer, which doesn't play nice with signs.

# x86-64 machine code, 15 bytes

B8 01 02 FF FE AB 0F C8 AB C1 C8 18 73 F7 C3


Try it online!

Writes a size-8 array of size-2 arrays of 8-bit integers to an address given in RDI.

In assembly:

f:  mov eax, 0xFEFF0201 # Set EAX to this value; -2, -1, 2, 1 from top to bottom.
r:  stosd       # Write EAX to the output address, advancing the pointer.
bswap eax   # Reverse the order of the bytes of EAX, getting 1, 2, -1, -2.
ror eax, 24 # Rotate right by 24 bits, getting 2, -1, -2, 1.
jnc r       # Jump back if CF (set from the last bit rotated: the high bit) is 0.
#  This is true the first time.
# After the jump, the value is added to the output again,
#  then reversing gives 1, -2, -1, 2, which is also added to the output,
# and then rotating gives -2, -1, 2, 1, and CF is 1, ending the loop.
ret         # Return.


# Python, 67 bytes

lambda:[z for i in range(99)if{*map(abs,z:=(i%5-2,i//5-2))}=={1,2}]

Attempt This Online!

Shorter than using itertools.product

• why not just use print([z for i in range(99)if{*map(abs,z:=(i%5-2,i//5-2))}=={1,2}])? it's the same number of bytes. (or just print("2-112-2-1-211-2-1-2-1221") like the bf answer.) May 23, 2022 at 3:40
• @DeeraWijesundara because it's the same number of bytes, and I'm used to writing functions for codegolf :p. And hard coding the answer in python just feels wrong May 23, 2022 at 3:49
• Very cool answer! Both how you generate and check the pairs are real neat. I do agree that hard-coding in languages with access to normal text seems a little cheap. May 23, 2022 at 4:21

# Vyxal, 16 13 bytes

»1rKȮ8»-12τ


Try it online!
-3 thanks to Steffan

7 bytes (o, Port of this, also thanks to Steffan)

k-:dẊ…R


Try it online!

### 16 bytes (old)

»-∞ðO∩ß¹¶"⊍»0\-¢


Try it Online!

»-∞ðO∩ß¹¶"⊍»        # Compressed integer 201120201021102010201221
0\-¢    # Replace 0 with -


Spent forever trying to find a cool answer but this is all my half asleep brain could come up with at the moment
There is a 0% chance this is optimal :P
• Argh. Beat me to it. May 23, 2022 at 13:41
• 13 bytes May 23, 2022 at 16:27
• port of jelly is 7 bytes: Try it Online! May 23, 2022 at 16:32
• @Steffan nice! Completely forgot about τ! May 23, 2022 at 17:52

# brainfuck, 68 bytes

-[----->>+<+<]>-.>--..----.<.>.<.-..+..->.<.>.<.+.>.<.->.<.>.<.+>.<.


Try it online!

Start is Similar to Pyautogui's answer, with a loop running 255/5=51 times to set a starting cell to 51. But I set two cells to 51, and use one for the characters 12 and the other one (hunt and peck style) for the character -.

The output 211-2-21122-1-12-2-1-1-2 consists of alternating pairs of 2s and 1s with interspersed - where necessary. The first two 1's are read from the cell which generates the - as the value descends from 51 through 49 to 45. The rest of the 1s are from the same cell as the 2.

# brainfuck, 29 bytes

+.+..-..>--..+..-..<.+.>+..<.


Try it online!

A different interpretation of the rules outputting binary values -2, -1, 1, 2 when run on a blank Brainfuck tape. For convenient verification the header initializes the two cells used on the tape to 48 = ASCII '0' so that the outputs for -2, -1, 1, 2 are . / 1 2 respectively.

# Wolfram Language (Mathematica), 41 36 bytes

-5 bytes, thanks @att.

Selects the elements with odd element sum from the group of all length 2 tuples with elements from the set {1,-2,2,-2}:

{1,-1,2,-2}~Tuples~2~Select~OddQ@*Tr

• 36 bytes
– att
May 23, 2022 at 19:22
• @att oooh, I didn't know you could do multiple infixes :D Very nice. May 24, 2022 at 8:07

# C (gcc), 74 70 bytes

-4 bytes thanks to @ceilingcat

i;main(){for(;i<32;++i)putchar(i&1?(i/2^i/8)%2+49:i&4+6*(i&2)?45:43);}


Try it online!

Edit: it's obviosly longer than necessary, but it's the shortest way I found without printing directly the result.

# Charcoal, 16 bytes

ＩΦＥφ⁻↨ι⁵⁰χ⁼⁵ΣＸι²


Try it online! Link is to verbose version of code. Explanation:

   φ                Predefined variable 1000
Ｅ                 Map over implicit range
ι             Current value
↨              Convert to base (i.e. divmod)
⁵⁰           Literal integer 50
⁻               Vectorised subtract
χ          Predefined variable 10
Φ                  Filtered where
ι     Current pair
Ｘ      Vectorised raised to power
²    Literal integer 2
Σ       Take the sum
⁼         Is equal to
⁵        Literal integer 5
Ｉ                   Cast to string
Implicitly print


Using base conversion results in the invalid "pairs" [], [-9], [-8], ... [39] but none of them have a Euclidean distance of √5. Boring string compression version for 11 bytes:

”{“↥?UV&✳¿>


Try it online! Link is to verbose version of code.

# Ruby, 33 bytes

x,y=1,2
4.times{p x,t=y,y=-x,x=t}


Try it online!

A straightforward rotation based on a concept of y,x=-x,y with some extra code to print in reverse as well.

# Burlesque, 18 bytes

{1-1}{2-2}cpJ)<-_+


Try it online!

{1-1} # {1 -1}
{2-2} # {2 -2}
cp    # Cart product
J     # Duplicate
)<-   # Reverse each in dup
_+    # Concat


# BQN, 19 bytes

(2|+´)¨⊸/⥊⋈⌜˜-⊸∾1‿2


Try it at BQN online

### Explanation

(2|+´)¨⊸/⥊⋈⌜˜-⊸∾1‿2
1‿2  List containing 1 and 2
∾     Prepend
-⊸      its negation
⋈⌜˜         Table of each item of that list paired with each item of that list
⥊             Deshape into a list of two-element lists
(    )¨                For each of those:
+´                    Get its sum
2|                      Mod 2
⊸/              Select the elements for which that result is 1


# Excel, 49 40 bytes

Saved 9 bytes thanks to JvdV

=CONCAT({1,-1}&2*{1;-1},2*{1,-1}&{1;-1})


Outputs pairs without delimeters.

# Excel, 27 bytes

="12-121-2-1-221-212-1-2-1"


... but that's too boring.

• The gentleman's golfing language. May 23, 2022 at 23:58
• Having a absulate blast going over your posts. Can't really follow the logic, but if you'd be interested in less bytes than maybe use =CONCAT({1,-1}&2*{1;-1},2*{1,-1}&{1;-1}) for 40 bytes.
– JvdV
Sep 5, 2022 at 19:32

# C (gcc), 68 bytes

y;main(x){for(y=3;x--<-3?x=--y+6:x*x+y*y-5||printf("%d,%d;",x,y););}


Try it online!

A reference for all the trivial approaches.

# Python, 33 bytes

print('2-112-2-1-211-2-1-2-1221')

Attempt This Online!

# R, 26 bytes

'2-112-2-1-211-2-1-2-1221'


Try it online!

# Wolfram Language (Mathematica), 26 bytes

Assumes a notebook environment. If this is false Echo@ can be added in front at the cost of 5 bytes.

"2-112-2-1-211-2-1-2-1221"


Try it online!

• Well, you might as well make it a community post then, and add R with cat("2-112-2-1-211-2-1-2-1221") (beats Python by 2 bytes)... May 23, 2022 at 6:25
• Oh I had misread the problem as required input to which you add all the directions, which imo makes more sense as a challenge May 23, 2022 at 13:35

# A0A0, 39 bytes

O-1212212
O-1
O-2
O-11
O-2
O-1
O-2
O-21


Prints -1212212-1-2-11-2-1-2-21. This simply prints many negative numbers, of which the negative numbers form non-separated pairs of relative positions. Since there are eight negative numbers, we use eight different output instructions.

# JavaScript, 58 bytes

_=>[...'01234567'].map(i=>[((i&4)-2)/(m=i%2+1),(i&2)*m-m])


Try it online!

I know it's shorter to hardcode, but that feels cheaty...

# (8-bit, wrapping) Brainfuck, 80 bytes

-[>+<-----]>-.[->+>+<<]>>-----.<-..+.>.<.>.<-.>.<+.-..>.<+.>.<-.>.<+.>.<-.+..-.>

Attempt This Online!

Outputs in the example format: "2-112-2-1-211-2-1-2-1221". Originally, this was in severe need of a golf, having been made by an automated tool. I have now optimized it a little. It now uses a similar technique to Level River St's answer, though I arrived at it independently, and it differs in the details; instead of 51, the starting value is 50.

++++[]+^^^D^^^D^P]PD^P]+PD^P]+PD^P]PD^P]P+PPD^P]PPD^P]+PD^P]+P-PP+PP-P!


Outputs -1-2-2-1-122-11-2-211221

Try it Here by executing

erun("++++[]+^^^D^^^D^P]PD^P]+PD^P]+PD^P]PD^P]P+PPD^P]PPD^P]+PD^P]+P-PP+PP-P!")


I'm ashamed at how long this is. I have to make a golfier language.............................

# 05AB1E, 8 bytes

X®‚xâ˜Â«


Try it online!

X     push an integer variable, which is initialized to 1 (using 1 makes it a string, which makes the output look weird)
®     push the register, which is initialized to -1
‚     pair, to get [1, -1]
x     push tos and itself doubled, to get [1, -1], [2, -2]
â     Cartesian product, all pairs
˜     Flatten, to get a list of coordinates instead of a list of pairs
Â     Push tos and itself reversed
«     Concatenate


# Zsh, 26 bytes

echo {,-}2{,-}1 {,-}1{,-}2

Attempt This Online!

One byte shorter than the trivial solution <<<2-112-2-1-211-2-1-2-1221.

# Regeneratea, 13 bytes

-?1-?2|-?2-?1


Not sure if this is golfable unless more features are added to the language lol... but please prove me wrong!

Prints like this:

12
1-2
-12
-1-2
21
2-1
-21
-2-1



Breakdown:

-?1-?2|-?2-?1
Print every string that is
-?1             -1 or 1
-?2          followed by -2 or 2
|       or
-?2      -2 or 2
-?1   followed by -1 or 1


Attempt This Online!