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

20
\$\begingroup\$

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

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!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Alternatively using cut for 36 bytes as well. \$\endgroup\$ Commented May 14, 2018 at 15:43
  • 1
    \$\begingroup\$ 33 bytes for bash+sed (despite how boring it is), echo {-1..1},{-1..1}|sed s/0,0.// \$\endgroup\$
    – user41805
    Commented May 14, 2018 at 18:01
  • 1
    \$\begingroup\$ printf %s\\n {-1..1},{-1..1}|grep 1 is also 35. \$\endgroup\$
    – Neil
    Commented May 14, 2018 at 18:07
9
\$\begingroup\$

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'
\$\endgroup\$
3
  • \$\begingroup\$ 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.) \$\endgroup\$
    – msh210
    Commented May 14, 2018 at 23:19
  • \$\begingroup\$ @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 \$\endgroup\$
    – BradC
    Commented May 15, 2018 at 13:22
  • 1
    \$\begingroup\$ You can remove the spaces around the * \$\endgroup\$ Commented Jul 11, 2018 at 4:26
7
\$\begingroup\$

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!

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

Jelly, 8 7 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

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Alterate answer based on this approach: 3p3_2ẸƇ \$\endgroup\$
    – Mr. Xcoder
    Commented May 14, 2018 at 20:19
  • \$\begingroup\$ @Mr.Xcoder You can drop the second 3. \$\endgroup\$
    – Dennis
    Commented May 15, 2018 at 1:52
  • \$\begingroup\$ @Dennis Oh, indeed. In this case, Adm can update with the 6-byter :) \$\endgroup\$
    – Mr. Xcoder
    Commented May 15, 2018 at 4:08
7
\$\begingroup\$

Haskell, 22 bytes

_:l=mapM(:[1,-1])[0,0]

Try it online!

Laikoni saved 1 byte.

\$\endgroup\$
2
  • \$\begingroup\$ _:l=mapM(:[1,-1])[0,0] saves a byte. (Taken from isaacg's answer to the earlier challenge). \$\endgroup\$
    – Laikoni
    Commented May 16, 2018 at 9:05
  • \$\begingroup\$ @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. \$\endgroup\$
    – xnor
    Commented May 19, 2018 at 0:28
7
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ huh, nice use of the flexible output format! \$\endgroup\$
    – Giuseppe
    Commented May 14, 2018 at 14:50
  • 2
    \$\begingroup\$ This one is better than mine because of how ultimately useless it is :) \$\endgroup\$
    – ngm
    Commented May 14, 2018 at 14:52
  • \$\begingroup\$ @Giuseppe Originally I tried c which did not make sense inside a matrix so I switched to paste and the original output format... \$\endgroup\$
    – JayCe
    Commented May 14, 2018 at 16:02
  • \$\begingroup\$ 24 bytes with paste \$\endgroup\$
    – J.Doe
    Commented Sep 27, 2018 at 9:24
  • 1
    \$\begingroup\$ @J.Doe you rock! \$\endgroup\$
    – JayCe
    Commented Sep 27, 2018 at 13:52
6
\$\begingroup\$

Japt, 13 12 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).
\$\endgroup\$
0
6
\$\begingroup\$

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!

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

Perl 6, 23 bytes

{(1,-1,0 X 1,-1,0)[^8]}

Try it online!

\$\endgroup\$
5
\$\begingroup\$

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 !

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$ Commented May 14, 2018 at 15:33
  • 2
    \$\begingroup\$ Use ʒĀZ to save 1 \$\endgroup\$
    – Emigna
    Commented May 15, 2018 at 5:52
  • \$\begingroup\$ @Emigna Thanks for making me understand the difference between the regular and the 05AB1IE version of the truthified command :-) \$\endgroup\$ Commented May 15, 2018 at 7:38
5
\$\begingroup\$

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]]
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Another 7 byte alternative tπ2↑3İZ. \$\endgroup\$
    – Laikoni
    Commented May 14, 2018 at 22:13
  • 2
    \$\begingroup\$ You can save one byte by filtering the lists based on their decimal conversion Try it online! \$\endgroup\$
    – Leo
    Commented May 15, 2018 at 4:13
5
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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));}. \$\endgroup\$ Commented May 17, 2018 at 12:40
  • \$\begingroup\$ @OlivierGrégoire Thanks, added (and golfed by 2 more bytes). \$\endgroup\$ Commented May 17, 2018 at 12:47
4
\$\begingroup\$

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,]
\$\endgroup\$
2
  • \$\begingroup\$ Some funky looking output, I like it :D Good job \$\endgroup\$ Commented May 14, 2018 at 14:37
  • \$\begingroup\$ expand.grid(-1:1,-1:1)[-5,] is 27 bytes. \$\endgroup\$
    – Giuseppe
    Commented May 14, 2018 at 14:46
4
\$\begingroup\$

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!

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

Haskell, 28 27 bytes

tail.mapM id$[0,1,-1]<$"ao"

Try it online!

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

J, 18 16 bytes

echo}.,{;~0 1 _1

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ Why minus is displayed as underscore in TIO? \$\endgroup\$ Commented May 14, 2018 at 18:11
  • \$\begingroup\$ 18 bytes alternative: echo }.>,{;~0 1 _1 TIO \$\endgroup\$ Commented May 14, 2018 at 18:15
  • \$\begingroup\$ @Dead Possum Negative numbers are displayed with an underscore in J \$\endgroup\$ Commented May 14, 2018 at 18:20
  • 1
    \$\begingroup\$ Yes, 17 :) echo}.>,{;~0 1 _1 \$\endgroup\$ Commented May 14, 2018 at 18:21
  • 2
    \$\begingroup\$ Is echo needed? \$\endgroup\$
    – cole
    Commented May 14, 2018 at 20:20
3
\$\begingroup\$

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.

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

Python 2, 39 bytes

n=6;exec'n+=~(n==2);print n/3,n%3-1;'*8

Try it online!

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

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

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

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]]"
\$\endgroup\$
3
\$\begingroup\$

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.

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

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])
\$\endgroup\$
1
  • 1
    \$\begingroup\$ 44 bytes by having (0,0) be the first element and calling Seq.tail: Try it online! \$\endgroup\$
    – Laikoni
    Commented May 14, 2018 at 22:07
3
\$\begingroup\$

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

Perl 5, 31 bytes

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

Try it online!

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

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)

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

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

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

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.

\$\endgroup\$

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.