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

# Octave, 24 bytes

dec2base([0:3,5:8],3)-49


Try it online!

I haven't seen this approach yet.

Creates a list of integers [0, 1, 2, 3, 5, 6, 7, 8], and converts it to ternary, returning a character array:

00
01
02
10
12
20
21
22


Subtracting 49 (ASCII-value for 1) from all characters gives a numeric array:

-1  -1
-1   0
-1   1
0  -1
0   1
1  -1
1   0
1   1


# Pure Bash (no external utilities), 36

a=({0,-1,1},{0,-1,1})
echo ${a[@]:1}  Try it online! # Bash with Sed, 35 printf %s\\n {-1..1},{-1..1}|sed 5d  Try it online! • Alternatively using cut for 36 bytes as well. May 14, 2018 at 15:43 • 33 bytes for bash+sed (despite how boring it is), echo {-1..1},{-1..1}|sed s/0,0.// May 14, 2018 at 18:01 • printf %s\\n {-1..1},{-1..1}|grep 1 is also 35. – Neil May 14, 2018 at 18:07 # T-SQL, 80 78 bytes SELECT-1n INTO t;INSERT t VALUES(0),(1)SELECT*FROM t,t z WHERE t.n<>0OR z.n<>0  Creates a (permanent) table t containing (-1,0,1), and performs a self-join with a WHERE clause that excludes the 0,0 row. The table t is not cleaned up by my code, you must drop it yourself. Sadly nearly twice as long as the boring solution (44 bytes), since SQL allows returns in strings: PRINT'0,1 0,-1 1,0 -1,0 1,1 1,-1 -1,1 -1,-1'  • I don't know T-SQL so well: can you use just WHERE t.n OR z.n? (You can in some but not all SQL dialects.) May 14, 2018 at 23:19 • @msh210 Good idea, I tried it but it doesn't seem to work on MS SQL Server. I get the error: An expression of non-boolean type specified in a context where a condition is expected May 15, 2018 at 13:22 • You can remove the spaces around the * Jul 11, 2018 at 4:26 # Python 2, 33 bytes i=9;exec"print-i%3-1,i/5;i-=2;"*8  Try it online! Dennis saved 3 5 bytes, wow. Thanks! # Jelly, 87 6 bytes 3p_2ẸƇ  Try it online! My first ever Jelly answer! Much thanks to Dennis for the final piece of the puzzle. Now, let's see if I can explain it ... lol. 3p_2ẸƇ Main program, takes no input. 3p Product with Range 3, yields [[1,1], [1,2], [1,3], [2,1], [2,2], ...] _2 Decrement twice, vectorizes, yields [[-1,-1], [-1,0], [-1,1], [0,-1], ...] ẸƇ Comb, removes those that contain only falsey values, the [0,0] Implicit output  -1 byte thanks to Erik; -1 byte thanks to Mr Xcoder and Dennis • Alterate answer based on this approach: 3p3_2ẸƇ May 14, 2018 at 20:19 • @Mr.Xcoder You can drop the second 3. May 15, 2018 at 1:52 • @Dennis Oh, indeed. In this case, Adm can update with the 6-byter :) May 15, 2018 at 4:08 # Haskell, 22 bytes _:l=mapM(:[1,-1])[0,0]  Try it online! Laikoni saved 1 byte. • _:l=mapM(:[1,-1])[0,0] saves a byte. (Taken from isaacg's answer to the earlier challenge). May 16, 2018 at 9:05 • @Laikoni So I had considered that and thought that would make it a snippet (as many answers to the old challenge were). But combining this meta post with the rule that functions can be defined indirectly, so this seems to be OK. Thanks for the suggestion. – xnor May 19, 2018 at 0:28 # R, 26 24 bytes Credits to @JDoe for saving two more bytes with a direct approach: paste(-1:1,-3:5%/%3)[-5]  Try it online! The original asnwer: outer(-1:1,-1:1,paste)[-5]  Try it online! Or for 27 bytes sapply(-1:1,paste,-1:1)[-5]  Try it online! Or for 34 bytes with factors: (gl(3,3,,-1:1):gl(3,1,9,-1:1))[-5]  Try it online! This last solution might be the golfiest if the output could be from 1 to 3 rather than from -1 to 1. See the other R answer for alternate solutions with expand.grid or with cbind. • huh, nice use of the flexible output format! May 14, 2018 at 14:50 • This one is better than mine because of how ultimately useless it is :) – ngm May 14, 2018 at 14:52 • @Giuseppe Originally I tried c which did not make sense inside a matrix so I switched to paste and the original output format... May 14, 2018 at 16:02 • 24 bytes with paste Sep 27, 2018 at 9:24 • @J.Doe you rock! Sep 27, 2018 at 13:52 # Japt, 1312 11 bytes Saved a byte thanks to @Shaggy 9ó8_ìJõ é)Å  Try it online! Uses -R flag to put each item on its own line. ### Explanation 9ó8_ìJõ é)Å 9ó8 Create the range [9, 9+8). [9, 10, ..., 16] _ Map each item in this range through this function: Jõ é) Generate the range [-1...1] and rotate to get [1, -1, 0]. ì Convert the item to an array of base-3 digits, mapping [0,1,2] to [1,-1,0]. [[-1, 1, 1], [-1, 1,-1], [-1, 1, 0], [-1,-1, 1], [-1,-1,-1], [-1,-1, 0], [-1, 0, 1], [-1, 0,-1]] Å Remove the first item (gets rid of the leading -1).  # Japt-Q, 15 13 bytes I'm sure there's a shorter way, but I liked this approach. ##ü80ì3 mÉ ò ##ü80 // Take 14425280 ì3 // and turn it into an array of base-3 numbers. mÉ // Subtract one from each digit ò // and then split them pairwise.  Shaved off two bytes thanks to Shaggy. Try it online! # Perl 6, 23 bytes {(1,-1,0 X 1,-1,0)[^8]}  Try it online! # 05AB1E, 8 7 bytes 2Ý<ãʒĀZ  Try it online! Explanation 2Ý< # Range of 2 decremented, yields [-1, 0, 1] ã # Cartesian product of the list with itself ʒ # Filter by ... ĀZ # Maximum of the truthified values, yields 0 only if both values are 0.  -1 byte thanks to Emigna ! • Dang, you beat me to it. Had the same start (2Ý<ã), but was figuring out how to remove the middle element of the list of pairs.. Hadn't thought about sort by absolute value and removing the first.. +1 from me. May 14, 2018 at 15:33 • Use ʒĀZ to save 1 May 15, 2018 at 5:52 • @Emigna Thanks for making me understand the difference between the regular and the 05AB1IE version of the truthified command :-) May 15, 2018 at 7:38 # Husk, 7 6 bytes There are a lot of different ways (the tricky/costly part is getting rid of [0,0]), 7 bytes is the shortest I could come up thanks to Leo for pointing out to use decimal conversion (d) as a filter: fdπ2ṡ1  Try it online! ### Explanation fdπ2ṡ1 -- constant function (expects no arguments) ṡ1 -- symmetric range [-n..n]: [-1,0,1] π2 -- cartesian power of 2: [[-1,-1],[-1,0],[0,-1],[-1,1],[0,0],[1,-1],[0,1],[1,0],[1,1]] f -- filter only elements that are truthy when d -- | decimal conversion (interpret as polynomial and evaluate at x=10) -- : [[-1,-1],[-1,0],[0,-1],[-1,1],[1,-1],[0,1],[1,0],[1,1]]  ## Alternative, 7 bytes tπ2ṙ1ṡ1  Try it online! ### Explanation tπ2ṙ1ṡ1 -- constant function (expects no arguments) ṡ1 -- symmetric range [-n..n]: [-1,0,1] ṙ1 -- rotate by 1: [0,1,-1] π2 -- cartesian power of 2: [[0,0],[0,1],[1,0],[0,-1],[1,1],[-1,0],[1,-1],[-1,1],[-1,-1]] t -- tail: [[0,1],[1,0],[0,-1],[1,1],[-1,0],[1,-1],[-1,1],[-1,-1]]  • Another 7 byte alternative tπ2↑3İZ. May 14, 2018 at 22:13 • You can save one byte by filtering the lists based on their decimal conversion Try it online! – Leo May 15, 2018 at 4:13 # MATL, 12 bytes 9:q4X-3YA49-  Try it online! Because it's MATL month, here's a MATL port of @Stewie's Octave answer. The sequence [0 1 2 3 5 6 7 8] is generated as the set difference between [0 ... 8] and 4. # Java 8, 83 42 bytes v->"1,1 1,0 1,-1 0,1 0,-1 -1,1 -1,0 -1,-1"  -41 bytes thanks to @AdmBorkBork by hard-coding.. Try it online. Non hard-coded version as reference (83 72 70 68 bytes): v->{for(int i=9;i-->1;)System.out.println(~i%3+1+","+(~(i/3)%3+1));}  -11 bytes thanks to @OlivierGrégoire. -2 bytes creating a port of @ETHproductions's JavaScript (ES6) answer. Try it online. • Non-hardcoded answer in 72 bytes: v->{for(int i=9;i-->0;)if(i!=4)System.out.println((i/3-1)+","+(i%3-1));}. May 17, 2018 at 12:40 • @OlivierGrégoire Thanks, added (and golfed by 2 more bytes). May 17, 2018 at 12:47 # R, 27 bytes expand.grid(-1:1,-1:1)[-5,]  Try it online! 30 and 35 bytes: cbind(-1:1,rep(-1:1,e=3))[-5,] expand.grid(rep(list(-1:1),2))[-5,]  • Some funky looking output, I like it :D Good job May 14, 2018 at 14:37 • expand.grid(-1:1,-1:1)[-5,] is 27 bytes. May 14, 2018 at 14:46 # JavaScript (ES6) Two alternate methods, both longer than hardcoding. ## 49 bytes _=>[...'11202200'].map((n,i,a)=>[~-n,~-a[i+3&7]])  Try it online! ## 51 bytes f=(n=1679887e3)=>n?[n%4-1,~-(n/4%4)]+' '+f(n>>4):''  Try it online! # Haskell, 28 27 bytes tail.mapM id$[0,1,-1]<$"ao"  Try it online! # J, 18 16 bytes echo}.,{;~0 1 _1  Try it online! • Why minus is displayed as underscore in TIO? May 14, 2018 at 18:11 • 18 bytes alternative: echo }.>,{;~0 1 _1 TIO May 14, 2018 at 18:15 • @Dead Possum Negative numbers are displayed with an underscore in J May 14, 2018 at 18:20 • Yes, 17 :) echo}.>,{;~0 1 _1 May 14, 2018 at 18:21 • Is echo needed? – cole May 14, 2018 at 20:20 # PowerShell, 41 bytes (1..-1|%{$i=$_;1..-1|%{"$i,$_"}})-ne'0,0'  Try it online! Double-for loop over the range 1..-1, with a -notequals at the end to pull out the extraneous 0,0 entry. They're each individually left on the pipeline and implicit Write-output at program completion gives us newlines for free. Sadly, just the barebones string output is two bytes shorter: '1,1 1,0 1,-1 0,1 0,-1 -1,1 -1,0 -1,-1'  But that's boring. # Python 2, 39 bytes n=6;exec'n+=~(n==2);print n/3,n%3-1;'*8  Try it online! # Haskell, 27 bytes tail$(,)<\$>t<*>t
t=[0,1,-1]


Try it online!

The output is [(0,1),(0,-1),(1,0),(1,1),(1,-1),(-1,0),(-1,1),(-1,-1)].

# CJam, 13 bytes

3,:(2m*{2b},


Try it online!

## Explanation

3,    e# Range [0,3):       [0 1 2]
:(    e# Decrement each:    [-1 0 1]
2m*   e# Cartesian square:  [[-1 -1] [-1 0] [-1 1] [0 -1] [0 0] [0 1] [1 -1] [1 0] [1 1]]
{     e# Filter by
2b   e#   conversion to binary:
},    e#                    [[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]]
e# Stringify:         "[[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]]"


# Befunge-93, 24 bytes

11#v91090~9~19~<
9.._@#,


Try it online!

I feel like this challenge is missing answers from 2D languages, even if most don't move diagonally. This outputs space separated numbers, each pair separated by tabs.

# F# (Mono), 54 bytes

let f=Seq.where((<>)(0,0))(Seq.allPairs[-1..1][-1..1])


Try it online!

44 bytes - thanks to Laikoni:

let f=Seq.tail(Seq.allPairs[0;-1;1][0;-1;1])

• 44 bytes by having (0,0) be the first element and calling Seq.tail: Try it online! May 14, 2018 at 22:07

# Brachylog, 8 bytes

Ċ{ṡᵐ≜}ᶠb


Try it online!

### Explanation

Ċ           Couple: take a list of two elements [A,B]
{   }ᶠ     Find all…
≜         …possible values of…
ṡᵐ          …signs of A and B
b    Behead: remove the first one which is [0,0]


## Perl 5, 31 bytes

map/1/&&say,<{-1,0,1},{-1,0,1}>


Try it online!

• Would -1..1 work in the glob? May 14, 2018 at 23:11
• @msh210 Unfortunately not... I did experiment with it, and it works in bash, but not in Perl :( May 15, 2018 at 7:26

## Bash, 30 bytes

echo "
"{-1..1},{-1..1}|grep 1


Try it online!

Prints a trailing space on each line but the last. (Thanks to @Neil - this originally printed a leading space, but a trailing space is better as per their comment)

• I guess you could print a trailing space on all but the last line as an alternative.
– Neil
May 20, 2018 at 10:35

# MATL, 12 bytes

3:qq2Z^[]5Y(


Try it at MATL Online!

My first ever serious MATL answer! Thanks a lot to Luis Mendo, Sanchises and DJMcMayhem for the help.

### How it works

3:qq2Z^[]5Y( – Full program. Outputs to STDOUT.
3:           – Range 3. Push [1 2 3] to the stack.
qq         – Decrement by 2. Yields [-1 0 1].
2Z^      – Cartesian power of 2.
5Y( – Replace the row at index 5 with...
[]    – An empty vector.


## Batch, 77 bytes

@for %%x in (-1 0 1)do @for %%y in (-1 0 1)do @if not %%x%%y==00 echo %%x %%y


63 bytes if a nonstandard separator is allowed:

@for %%x in (-1/-1 -1/0 -1/1 0/-1 0/1 1/-1 1/0 1/1)do @echo %%x


# Pyth, 11 9 bytes

t^+U2_1 2


Try it here

### Explanation

t^+U2_1 2
+U2_1     [0, 1, -1]
^      2   Product with itself.
t           Exclude the first.


Equivalently, we could use t*J+U2_1J, but that's not any shorter.