# Orthodiagonal steps

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
• @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. – Dead Possum May 14 '18 at 14:28
• 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? – ngm May 14 '18 at 15:08
• @Adám Yes, use anything while pairs of numbers are distinguishable – Dead Possum May 14 '18 at 15:59
• @Adám But what about (1 + 0i) ? – Dead Possum May 14 '18 at 17:48
• This is an exact duplicate of 8 adjacent squares, one of the first code golfs I ever did. – isaacg May 15 '18 at 18:17

# Jelly, 6 bytes

;Ø+pḊ


Try it online!

-1 thanks to Dennis.

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

# 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


# Japt, 14 13 bytes

[J1T]ê à2 s1J


Try it online!

# 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*ỵ⁈.

# Ruby, 40 bytes

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


Try it online!

• 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. – Adyrem May 15 '18 at 11:40
• Oh yeah I completely missed the [0, 0]. I adjusted and added a TiO link. Thanks for catching that! – Jérémie Bonal May 15 '18 at 15:55

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

• or this can be done with 37 bytes. – Francisco Hahn May 14 '18 at 20:54
• Yeah... I did think hard-coding would be smaller, but programatically is more interesting, right? :) – Dom Hastings May 15 '18 at 7:27
• of couse, far more interesting :-D – Francisco Hahn May 15 '18 at 13:37

# 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


# 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)


# Wolfram Language (Mathematica), 29 bytes

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


Try it online!

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

# Retina, 24 bytes

K_ _
_
_$"0 +0_ 1$%"-1


Try it online!

# GolfScript, 2619 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.

# brainfuck, 86 bytes

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


Try it online!

Prints pairs of numbers separated by commas.

# 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])}


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

# 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!

# 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



# 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


# 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


# Ruby, 31 bytes

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


Try it online!

# 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]

# 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)


# 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)
`