27
\$\begingroup\$

It's a common problem to navigate in a 2D matrix. We've seen it many times and will see again. So let's help future us and develop the shortest solutions to generate all eight possible steps in a 2D matrix.

Challenge

Your code must output the following 8 pairs of -1,0,1 in any order:

(0,1)
(0,-1)
(1,0)
(-1,0)
(1,1)
(1,-1)
(-1,1)
(-1,-1)

Rules

  1. There is no input.
  2. Output order is not relevant
  3. Output is flexible. Pairs of numbers just need to be distinguishable
  4. This is , so shortest answer in bytes wins
\$\endgroup\$
18
  • 2
    \$\begingroup\$ @MartinEnder I was 99% sure about that too, but didn't find any either. So I've put it in sandbox for a few days, but noone commented about duplicate. \$\endgroup\$ Commented May 14, 2018 at 14:28
  • 4
    \$\begingroup\$ Because of the flexible output, there turns out to be an interesting Kolmogorov complexity flavour to this one. Some languages will find it harder than to do better than just hard coding the output. Should this tag be added? \$\endgroup\$
    – ngm
    Commented May 14, 2018 at 15:08
  • 1
    \$\begingroup\$ @Adám Yes, use anything while pairs of numbers are distinguishable \$\endgroup\$ Commented May 14, 2018 at 15:59
  • 1
    \$\begingroup\$ @Adám But what about (1 + 0i) ? \$\endgroup\$ Commented May 14, 2018 at 17:48
  • 8
    \$\begingroup\$ This is an exact duplicate of 8 adjacent squares, one of the first code golfs I ever did. \$\endgroup\$
    – isaacg
    Commented May 15, 2018 at 18:17

58 Answers 58

1
2
2
\$\begingroup\$

Jelly, 6 bytes

;Ø+p`Ḋ

Try it online!

-1 thanks to Dennis.

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

JavaScript (ES6), 45 bytes

f=(n=8)=>n?[...f(n-1),[1+~n%3,1+~(n/3)%3]]:[]

Try it online!

Another alternative to hardcoding, still not as short though...

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

Stax, 10 bytes

τÄêdD┘│çû╢

Run and debug it

Explanation (unpacked):

1:r2|^{|af|u Full program
1:r          Push [-1, 0, 1]
   2|^       Join with self
   c|*        Alternative: copy and join
      {  f   Filter:
       |a      Any truthy (not 0)?
          |u Representation
\$\endgroup\$
2
\$\begingroup\$

Japt, 14 13 bytes

[J1T]ê à2 s1J

Try it online!

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

Gaia, 8 bytes

3…(¦:×ỵ⁈

Try it online!

This generates the list in this order:

[[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]]

Explanation

3…(¦:×ỵ⁈ – Full program. Outputs to STDOUT.
3…       – Push [0 1 2] to the stack.
  (¦     – Decrement each; yields [-1 0 1].
    :    – Duplicate.
     ×   – Cartesian product.
       ⁈ – Filter-reject those that:
      ỵ  – Have no truthy element.

An alternative to this would be 3…(¦2*ỵ⁈.

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

Ruby, 40 bytes

([*-1..1]*2).permutation(2).uniq-[[0,0]]

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Could you add a TIO link? Also I've never used Ruby yet, but i think this also outputs (0, 0), which isn't a valid step. \$\endgroup\$
    – Adyrem
    Commented May 15, 2018 at 11:40
  • \$\begingroup\$ Oh yeah I completely missed the [0, 0]. I adjusted and added a TiO link. Thanks for catching that! \$\endgroup\$ Commented May 15, 2018 at 15:55
2
\$\begingroup\$

brainfuck, 86 bytes

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

Try it online!

Prints pairs of numbers separated by commas.

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

PHP, 79, 76, 73 Bytes

76 Bytes thanks to Dom Hastings

Try it online (79 Bytes) using base 16 (Original answer)

Try it online (77 Bytes) using base 36

Try it online (73 Bytes) using base 36

Tried to avoid any kind of loop

<?=strtr(chunk_split(base_convert("18qfremyq6",36,4),3,"
"),[2=>-1,',']);
//as the starting index is 2, next will be 3, no need for 3=>','

Output

1,0
-1,0
1,1
1,-1
-1,1
-1,-1
0,1
0,-1

Explanation

Basically "72c75eb6e34e" 18qfremyq6 is the base 16 36 representation of a base 4 number 130230131132231232031032, every 3 represents a "," and 2 a -1

Chunk split, adds a line break every 3 characters.

Shorter with an output without the comma, 79 Bytes

<?=strtr(chunk_split(base_convert("10fcb5c",16,3),2,"
"),[2=>-1]);

Output

10
-10
11
1-1
-11
-1-1
01
0-1
\$\endgroup\$
3
  • 2
    \$\begingroup\$ or this can be done with 37 bytes. \$\endgroup\$ Commented May 14, 2018 at 20:54
  • 2
    \$\begingroup\$ Yeah... I did think hard-coding would be smaller, but programatically is more interesting, right? :) \$\endgroup\$ Commented May 15, 2018 at 7:27
  • \$\begingroup\$ of couse, far more interesting :-D \$\endgroup\$ Commented May 15, 2018 at 13:37
2
\$\begingroup\$

Japt, 9 7 bytes

4 Japt solutions to one challenge; that has to be a first :)

Jõ ï fd

Test it


Original, 9 bytes

4#ô²ìJõ)ò

Test it


Explanation

Using the number Nit found for his solution (14425280) as a starting point, I converted it to base-3, split it to an array of 2 character strings and then tested each permutation to see if any of them produced a perfect square or cube when rejoined to a string and converted back to base-10. That gave me the perfect squares 15366400, 18011536 & 18421264 and, of those, only 18011536 had a square root that would allow me to save a byte thanks to Japt's trick of using # to build numbers, that being 4244.

4#ô           :4244
   ²          :Square
     Jõ       :Array [-1,0,1]
    ì  )      :Convert to a digit array in that base
        ò     :Split on every second element
\$\endgroup\$
1
\$\begingroup\$

K (ngn/k), 15 bytes

{(+1-!x#3)^,&x}

Try it online!

 x:2 / for testing
 x#3 / that many 3s
3 3
 !x#3 / all vectors of 0 1 2 as the columns of a matrix
(0 0 0 1 1 1 2 2 2
 0 1 2 0 1 2 0 1 2)
 +!x#3 / transpose
(0 0
 0 1
 0 2
 1 0
 1 1
 1 2
 2 0
 2 1
 2 2)
 &x / that many 0s
0 0
 (+!x#3)^,&x / remove the all-zero vector
(0 1
 0 2
 1 0
 1 1
 1 2
 2 0
 2 1
 2 2)
\$\endgroup\$
1
\$\begingroup\$

Wolfram Language (Mathematica), 29 bytes

Print@Rest@Tuples[{0,1,-1},2]

Try it online!

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

JavaScript (Node.js), 42 bytes

_=>`1,1
1,0
1,-1
0,1
0,-1
-1,1
-1,0
-1,-1`

Try it online!

Explanation :

Does nothing basically no input and prints the string separated by new lines using string literal.

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

Retina, 24 bytes

K`_ _
_
_$"0
+0`_
1$%"-1

Try it online!

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

GolfScript, 26 19 18 bytes

9,[4]-{.3/(\3%(n}/

Try it online!

My first GolfScript submission, any help is appreciated! Outputs one element per line, and removes the 0, 0 element.

For nicer printing, I found this 20 byte solution:

9,[4]-{[.3/(\3%(]p}%

which prints one array per line.

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

TI-Basic, 32 bytes

For(A,-1,1
For(B,-1,1
If A≠2B
Disp A,B,"
End
End

Prints pairs as two lines, separated by empty lines.

TI-Basic is a tokenized language, all tokens used here are one-byte.

Explanation:

For(A,-1,1 # First number from -1 to 1 (inclusive)
For(B,-1,1 # Second number from -1 to 1 (inclusive)
If A≠2B    # If the pair is not 0,0
Disp A,B," # Print the first number, then the second, then
           # an empty string (each on its own line)
End        # End second number loop
End        # End first number loop

Output

-1
-1

-1
0

-1
1

0
-1

0
1

1
-1

1
0

1
1

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

Yabasic, 49 bytes

An anonymous function that takes no input and outputs to the console.

For i=-1To 1
For j=-1To 1
If i Or j?i,j
Next
Next

Try it online!

Output

-1 -1
-1 0
-1 1
0 -1
0 1
1 -1
1 0
1 1
\$\endgroup\$
1
\$\begingroup\$

VBA, 66 bytes

An anonymous function that takes no input and outputs to the console.

For i=-1To 1:For j=-1To 1:?IIf(i Or j,i &" "&j &vbCr,"");:Next j,i

Output

-1 -1
-1 0
-1 1
0 -1
0 1
1 -1
1 0
1 1
\$\endgroup\$
1
\$\begingroup\$

Ruby, 31 bytes

p (w=*-1..1).product(w)-[[0,0]]

Try it online!

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

Julia 0.6, 24 bytes

[(A=[0:3;5:8])%3 A÷3]-1

Try it online!

Generate [0,1,2,0,2,0,1,2] in the first column and [0,0,0,1,1,2,2,2] in the second (excluding 4%3 and 4÷3 to avoid 0 0), then subtract 1 from all the values.

Other methods I tried:

[(i,j)for i=-1:1,j=-1:1 if i|j!=0]

[[s...].-'1'for s=base.(3,[0:3;5:8],2)] (based on Stewie Griffin's Octave solution)

[(i,j)for i=-1:1,j=-1:1][(1:9).!=5]

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

JavaScript (Most browsers), 42 40 bytes

for(x=9;x;)--x-4&&alert([~(-x/3),x%3-1])

Only works in DevTools in browsers. As per New users' guides to golfing rules in specific languages this should be valid (Full browser JS program).

If the one above is anyway considered a snippet, then 47 45 bytes:

_=>{for(x=9;x;)--x-4&&alert([~(-x/3),x%3-1])}
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 6 bytes

T®šã¨

Port of @ბიმო's second Husk answer, so make sure to upvote him!

Try it online.

Explanation:

T      # Push 10
 ®š    # Prepend -1 as list: [-1,1,0]
   ã   # Take the cartesian product of this list with itself, generating all pairs
    ¨  # Remove the last one, which is the [0,0]
       # (after which the result is output implicitly)
\$\endgroup\$
1
\$\begingroup\$

MathGolf, 7 5 bytes

1╤╫■╞

-2 bytes by porting @ბიმო's second Husk answer, so make sure to upvote him!

Try it online.

Explanation:

1╤     # Push list [-1,0,1] (push 1; pop and push a list in the range [-n,n])
  ╫    # Rotate it once towards the left: [0,1,-1]
   ■   # Take the cartesian product with itself, creating all possible pairs
    ╞  # Discard the first item of this list (the [0,0] pair)
       # (after which the entire stack joined together is output implicitly)
\$\endgroup\$
1
\$\begingroup\$

Whitespace, 219 bytes

[S S S T    T   N
_Push_3][S S T  T   N
_Push_-1][S S S N
_Push_0][S S S T    T   N
_Push_3][S S T  T   N
_Push_-1][S S S T   N
_Push_1][S S S T    T   N
_Push_3][S S S N
_Push_0][S S S T    T   N
_Push_3][S S T  T   N
_Push_-1][S S S T   N
_Push_1][S S S T    T   N
_Push_3][S S T  T   N
_Push_-1][S S S N
_Push_0][S S S T    T   N
_Push_3][S S S T    N
_Push_1][S S S T    T   N
_Push_3][S S S N
_Push_0][S S S T    T   N
_Push_3][S S S T    N
_Push_1][S S S T    S N
_Push_2][S S S N
_Push_0][S S S T    T   N
_Push_3][S S T  T   N
_Push_-1][S S S T   N
_Push_1][S S S T    S N
_Push_2][S S S N
_Push_0][S S S T    T   N
_Push_3][S S S T    N
_Push_1][S S S T    T   N
_Push_3][S S T  T   N
_Push_-1][S S S N
_Push_0][S S S T    S N
_Push_2][S S S T    N
_Push_1][S S S T    T   N
_Push_3][S S S N
_Push_0][S S S T    S N
_Push_2][N
S S N
_Create_Label_LOOP][S S S T S T T   T   S N
_Push_46][T S S S _Add_top_two][T   N
S S _Print_as_character][N
S N
N
_Jump_to_Label_LOOP]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online (with raw spaces, tabs and new-lines only).

Prints with a . delimiter between each number, and / between each pair of numbers. Since the codepoints of both . and / are between - and 01, these are the shortest delimiters to use with this approach.

Uses this Whitespace tip of mine to print the output. The optimal constant 46 is generated by this Java program.

NOTE: Hard-coded with tab and newline delimiters (since their codepoints are pretty short, being 9 and 10 respectively) would be 332 bytes instead.

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

Deadfish~, 238 bytes

{iiiii}ddc{d}ddddddc{ii}dddcdddddciiiic{d}ddddddc{i}iiiciiiicdddddciiiiic{dd}iiic{i}iiiiiicddddciciiiic{dd}iiic{i}iiiiiicddddciiiiic{dd}iiic{ii}dddcdddddciiiiic{dd}iiic{i}iiiciiiicdddddciciiiic{dd}iiic{ii}dddcdddddciciiiic{dd}iiic{ii}dddc

Try it online!

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

Pxem, Filename: 39 bytes + Content: 0 bytes = 39 bytes.

  • Filename: 0 1;0 -1;1 0;-1 0;1 1;1 -1;-1 1;-1 -1.p
  • Content is empty.

How would you do that by control flowing although we don't have negative constants nor the way to obtain negative value without .i or ._!

\$\endgroup\$
1
  • \$\begingroup\$ Other attempt: -1 -1;.e.s.p-1 0;.e.s.p-1 1;.e.s.p, 34 bytes up to six pairs. \$\endgroup\$
    – user100411
    Commented Mar 22, 2021 at 11:00
1
\$\begingroup\$

Vyxal, 10 7 bytes

1u0W2↔Ṫ

Try it Online!

Explained

1u0W2↔Ṫ
1u0W    # list of [1,-1,0]
    2↔  # combinations of length 2
      Ṫ # remove tail which is [0,0] which is not needed in the challenge
\$\endgroup\$
0
\$\begingroup\$

Tcl, 44 bytes

puts "-1 -1
0 -1
1 -1
-1 0
1 0
-1 1
0 1
1 1"

Try it online!


Tcl, 77 bytes

set j -1
time {set i -2
time {puts [expr [incr i]|$j?"$i $j":""]} 3
incr j} 3

Try it online!

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

Uiua, 10 bytes

↘1/⊂⊞⊂.∸~1

Try it!

↘1/⊂⊞⊂.∸~1
       ∸~1  # the list [0 ¯1 1]
      .     # duplicate
    ⊞⊂      # cartesian product
  /⊂        # decrement rank
↘1          # drop first row which is [0 0]
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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