18
\$\begingroup\$

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.

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

28 Answers 28

13
\$\begingroup\$

Wolfram Language (Mathematica), 25 bytes

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

Try it online!

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

\$\endgroup\$
1
  • \$\begingroup\$ Yet another clean generation process. Very nice! \$\endgroup\$
    – chakra
    May 23 at 23:55
11
\$\begingroup\$

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.

\$\endgroup\$
7
\$\begingroup\$

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.
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Wow! Awesome approach. \$\endgroup\$
    – chakra
    May 23 at 4:24
5
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ The 9 byte solution is very cool! Great to see it beats the 'trivial' solution too :) \$\endgroup\$
    – chakra
    May 23 at 6:58
5
\$\begingroup\$

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)
\$\endgroup\$
5
\$\begingroup\$

R, 42 35 34 33 bytes

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

Try it online!

Outputs as complex numbers.

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

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!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – chakra
    May 23 at 12:52
5
\$\begingroup\$

R, 37 29 26 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...

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

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 }
\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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.
    stosd       # Write EAX to the output address, advancing the pointer.
    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.
\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ 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.) \$\endgroup\$ May 23 at 3:40
  • \$\begingroup\$ @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 \$\endgroup\$
    – jezza_99
    May 23 at 3:49
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – chakra
    May 23 at 4:21
3
\$\begingroup\$

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
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Argh. Beat me to it. \$\endgroup\$
    – Seggan
    May 23 at 13:41
  • \$\begingroup\$ 13 bytes \$\endgroup\$
    – Steffan
    May 23 at 16:27
  • 1
    \$\begingroup\$ port of jelly is 7 bytes: Try it Online! \$\endgroup\$
    – Steffan
    May 23 at 16:32
  • \$\begingroup\$ @Steffan nice! Completely forgot about τ! \$\endgroup\$
    – Komali
    May 23 at 17:52
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 36 bytes \$\endgroup\$
    – att
    May 23 at 19:22
  • \$\begingroup\$ @att oooh, I didn't know you could do multiple infixes :D Very nice. \$\endgroup\$
    – JSorngard
    May 24 at 8:07
3
\$\begingroup\$

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.

\$\endgroup\$
0
2
\$\begingroup\$

Excel, 49 bytes

=LET(a,{1,-1},b,TRANSPOSE(a),CONCAT(a&2*b,2*a&b))

Outputs pairs without delimeters.

Screenshot


Excel, 27 bytes

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

... but that's too boring.

\$\endgroup\$
1
  • \$\begingroup\$ The gentleman's golfing language. \$\endgroup\$
    – chakra
    May 23 at 23:58
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 16 bytes

IΦEφ⁻↨ι⁵⁰χ⁼⁵ΣXι²

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

   φ                Predefined variable `1000`
  E                 Map over implicit range
      ι             Current value
     ↨              Convert to base (i.e. divmod)
       ⁵⁰           Literal integer `50`
    ⁻               Vectorised subtract
         χ          Predefined variable `10`
 Φ                  Filtered where
              ι     Current pair
             X      Vectorised raised to power
               ²    Literal integer `2`
            Σ       Take the sum
          ⁼         Is equal to
           ⁵        Literal integer `5`
I                   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.

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
3
  • \$\begingroup\$ 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)... \$\endgroup\$ May 23 at 6:25
  • \$\begingroup\$ @DominicvanEssen, good idea, please add your trivial solutions here. \$\endgroup\$
    – pajonk
    May 23 at 6:26
  • \$\begingroup\$ Oh I had misread the problem as required input to which you add all the directions, which imo makes more sense as a challenge \$\endgroup\$
    – Jonah
    May 23 at 13:35
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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...

\$\endgroup\$
1
\$\begingroup\$

(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.

\$\endgroup\$
1
\$\begingroup\$

Headascii, 71 bytes

++++[]+^^^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.............................

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.