26
\$\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\$
  • 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\$ – Dead Possum May 14 '18 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 May 14 '18 at 15:08
  • 1
    \$\begingroup\$ @Adám Yes, use anything while pairs of numbers are distinguishable \$\endgroup\$ – Dead Possum May 14 '18 at 15:59
  • 1
    \$\begingroup\$ @Adám But what about (1 + 0i) ? \$\endgroup\$ – Dead Possum May 14 '18 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 May 15 '18 at 18:17

51 Answers 51

2
\$\begingroup\$

Jelly, 6 bytes

;Ø+p`Ḋ

Try it online!

-1 thanks to Dennis.

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

Ruby, 40 bytes

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

Try it online!

\$\endgroup\$
  • 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 May 15 '18 at 11:40
  • \$\begingroup\$ Oh yeah I completely missed the [0, 0]. I adjusted and added a TiO link. Thanks for catching that! \$\endgroup\$ – Jérémie Bonal May 15 '18 at 15:55
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\$
  • 2
    \$\begingroup\$ or this can be done with 37 bytes. \$\endgroup\$ – Francisco Hahn May 14 '18 at 20:54
  • 2
    \$\begingroup\$ Yeah... I did think hard-coding would be smaller, but programatically is more interesting, right? :) \$\endgroup\$ – Dom Hastings May 15 '18 at 7:27
  • \$\begingroup\$ of couse, far more interesting :-D \$\endgroup\$ – Francisco Hahn May 15 '18 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\$

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\$
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\$

brainfuck, 86 bytes

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

Try it online!

Prints pairs of numbers separated by commas.

\$\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\$
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\$

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

Ruby, 31 bytes

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

Try it online!

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

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.