15
\$\begingroup\$

List the coordinates of 8 squares adjacent to (0, 0) in a 2d grid. Namely,

[(1,-1),(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1)]

Order does not matter. The goal is to store the points into a list. Shortest answer wins.

\$\endgroup\$
  • \$\begingroup\$ Formatted exactly like the string in your question, or listed in the native/natural format for your language? \$\endgroup\$ – Digital Trauma Apr 8 '14 at 16:52
  • 2
    \$\begingroup\$ @DigitalTrauma, listed in natural format. \$\endgroup\$ – Karolis Juodelė Apr 8 '14 at 16:53
  • 1
    \$\begingroup\$ Can you just clarify if the (0, 0) has to be omitted from the output? \$\endgroup\$ – Gareth Apr 8 '14 at 17:32
  • 1
    \$\begingroup\$ @Gareth, yes, it has to be omitted. It would be 9 squares with (0,0), after all. \$\endgroup\$ – Karolis Juodelė Apr 8 '14 at 17:41
  • 1
    \$\begingroup\$ @DigitalTrauma, store only. \$\endgroup\$ – Karolis Juodelė Apr 8 '14 at 17:58

32 Answers 32

15
\$\begingroup\$

J - 12 char

The most natural representation of a list of pairs is a list of boxes containing each pair.

   }.4|.,{;~i:1
+---+----+---+---+-----+----+----+----+
|0 1|1 _1|1 0|1 1|_1 _1|_1 0|_1 1|0 _1|
+---+----+---+---+-----+----+----+----+

The magic happens in {, called Catalogue. It is essentially a generalized Cartesian product. The }.4|. part is for removing the 0 0 pair.

\$\endgroup\$
15
\$\begingroup\$

Bash + grep 35

eval echo\ {-1..1},{-1..1}\;|grep 1

I know this doesn't beat the other bash answer but someone had to think of grep

\$\endgroup\$
10
\$\begingroup\$

Ruby 29 28 26

(a=-1,1,0).product(a)[0,8]

Bonus solution:

Ruby has many nice builtin array operators, but some of them have ridiculously long names.

[*[-1,1,0].repeated_permutation(2)].take 8
\$\endgroup\$
  • \$\begingroup\$ Doesn't this include the 0,0? \$\endgroup\$ – Gareth Apr 8 '14 at 17:27
  • \$\begingroup\$ You don't actually need the square brackets in your first answer. \$\endgroup\$ – histocrat Apr 8 '14 at 17:28
  • \$\begingroup\$ @DigitalTrauma + Gareth Yup. So do other answers here I suspect. Fixed it anyways. \$\endgroup\$ – daniero Apr 8 '14 at 17:41
  • 1
    \$\begingroup\$ @DigitalTrauma :P \$\endgroup\$ – daniero Apr 8 '14 at 20:04
  • 1
    \$\begingroup\$ You can go further: (a=-1,1,0).product(a)[0,8] is 26 chars. \$\endgroup\$ – algorithmshark Apr 8 '14 at 20:33
9
\$\begingroup\$

Python 43 32

l=zip([1,-1,0]*3,[-1,1]*3+[0]*2)

Inspired by @primo

\$\endgroup\$
  • \$\begingroup\$ I don't think this is 43 bytes anymore :) \$\endgroup\$ – 2rs2ts Apr 9 '14 at 17:03
  • \$\begingroup\$ It looks like 43 characters to me - am I missing something? Oh wow, that solution above is sweet. \$\endgroup\$ – isaacg Apr 9 '14 at 18:36
  • \$\begingroup\$ @issacg I had miscounted earlier, my mistake. Nice improvement, though. \$\endgroup\$ – 2rs2ts Apr 9 '14 at 18:50
  • \$\begingroup\$ I like it. Nice improvement on my 40. \$\endgroup\$ – primo Apr 20 '14 at 4:47
7
\$\begingroup\$

GolfScript (17 chars)

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

Since the question calls for the points to be "listed in natural format", this leaves an array on the stack. To pretty-print it, append p, as in the online demo.

\$\endgroup\$
6
\$\begingroup\$

Haskell 32

c=[0,1,-1]
_:l=[(a,b)|a<-c,b<-c]

The list is l (L).

\$\endgroup\$
  • \$\begingroup\$ You could do _:l=(,)<$>c<*>c to save 6 characters, although it requires import Control.Applicative. \$\endgroup\$ – Karolis Juodelė Apr 9 '14 at 4:45
  • \$\begingroup\$ Yeah, having free import statements would be pretty cool. It'd be interesting to specify free imports on a challenge, and see what sort of different responses you'ld get. \$\endgroup\$ – isaacg Apr 9 '14 at 4:59
6
\$\begingroup\$

Brainfuck, 1093 characters.

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

EDIT 209 characters

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

Original by Benjamin - i gave it another shot as well and got to 209 characters as well.

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

Perl, 43 bytes

for$x(-1..1){$x|$_&&push@r,[$x,$_]for-1..1}

The result is stored in array @r. Explicitly it can be defined with 58 bytes:

@r=([-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1])
\$\endgroup\$
4
\$\begingroup\$

J, 16 characters

   }.4|.,<@,"0/~i:1

Usage:

   }.4|.,<@,"0/~i:1
┌───┬────┬───┬───┬─────┬────┬────┬────┐
│0 1│1 _1│1 0│1 1│_1 _1│_1 0│_1 1│0 _1│
└───┴────┴───┴───┴─────┴────┴────┴────┘

Pretty ugly - especially dropping the (0, 0). There's probably a better way.

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

Mathematica 23

Most@Tuples[{-1,1,0},2]
(*
{{-1, -1}, {-1, 1}, {-1, 0}, {1, -1}, {1, 1}, {1, 0}, {0, -1}, {0, 1}}
*)
\$\endgroup\$
  • \$\begingroup\$ Cheeky. :) Very nice idea! I think precedence rules should allow you to replace Tuples[{...},2] with {...}~Tuples~2 to save another byte. \$\endgroup\$ – Martin Ender Apr 8 '14 at 18:26
  • \$\begingroup\$ @m.buettner Thanks. That byte is lost afterwards when trying to apply Most[] (or I can't find a way around it). BTW I never saw you around mathematica.stackexchange.com Any particular reason? \$\endgroup\$ – Dr. belisarius Apr 8 '14 at 18:31
  • \$\begingroup\$ Can you explain how this works? \$\endgroup\$ – Digital Trauma Apr 8 '14 at 18:33
  • \$\begingroup\$ Ah, I thought ~..~ had higher precedence than @. I think that means I can save a byte in quite a few of my mathematica-answers :D. No particular reason why I haven't been around mathematica stackexchange, but I haven't used other SE sites other than SO much until recently. And I don't really consider myself an expert in Mathematica to be answering interesting questions. And I also don't use it for more than some casual stuff like here in code golf and the odd physics problem. \$\endgroup\$ – Martin Ender Apr 8 '14 at 18:34
  • 2
    \$\begingroup\$ @DigitalTrauma Tuples[{a,b,c ...}, n] generates all possible lists of length n containing {a,b,c ...} as elements. Then Most[] discards the last one (which is {0,0} ) \$\endgroup\$ – Dr. belisarius Apr 8 '14 at 18:35
4
\$\begingroup\$

Brainfuck, 217 209 characters.

Only a bit worse than Java! 5x less than the other BF solution.

++++++++++[>+++++++++>++++>+++++>++++<<<<-]>+.>.>-.>++++.+.<.<+.>>-.<<-.>.>.<-.<+.>>.<<-.>+.>.<.<+.>>.<<-.>-.>.<+.<+.>>.<<-.>>+.<.>-.<.<+.>>.<<-.>>+.<.>-.<-.<+.>>.<<-.>>+.<+.>-.+.<.<+.>>-.<<-.>-.>.+.<+.<+.<++.
\$\endgroup\$
  • \$\begingroup\$ You mean 1/5th of the other solution :D But i accept your challlenge. Allthough i think i can not get it any smaller than this. \$\endgroup\$ – Rob Apr 11 '14 at 9:09
  • \$\begingroup\$ Hah.. I gave it a shot as well and came to the same amount of characters... Coincidence? \$\endgroup\$ – Rob Apr 11 '14 at 10:28
3
\$\begingroup\$

Mathematica, 44 characters

Outer[List,d=Range@3-2,d]~Flatten~1~Drop~{5}

Well. Generates all 9 coordinates, flattens the list and drops the centre.

\$\endgroup\$
  • \$\begingroup\$ Very nice use of common functions, Outer and Range, but they should not return the point {0,0}. \$\endgroup\$ – DavidC Apr 8 '14 at 17:32
  • \$\begingroup\$ @m.buettner Good. Thanks. +1 \$\endgroup\$ – Digital Trauma Apr 8 '14 at 17:38
3
\$\begingroup\$

Python 50:

l=[(a,b)for a in[1,0,-1]for b in[1,0,-1]];l.pop(4)
\$\endgroup\$
2
\$\begingroup\$

Bash, 25 characters

Storing the list of coordinates as a bash array a:

a=({-1..1},{-1..1});a[4]=

Output:

$ a=({-1..1},{-1..1});a[4]=
$ echo ${a[@]}
-1,-1 -1,0 -1,1 0,-1 0,1 1,-1 1,0 1,1
$
\$\endgroup\$
2
\$\begingroup\$

F# - 41 36 bytes

[for i in 0..7->i*2/5-1,(i*5/4)%3-1]
\$\endgroup\$
1
\$\begingroup\$

C++, 60

Let's do this an interesting way. Hint: "2" is ASCII 50.

char x[]="11123332",y[8],i;for(i=8;i--;)y[(i+2)%8]=x[i]-=50;
\$\endgroup\$
  • \$\begingroup\$ Can you do char *x="11123332" to save a char? \$\endgroup\$ – Digital Trauma Apr 8 '14 at 18:22
  • 1
    \$\begingroup\$ or just auto x="11123332" to save another \$\endgroup\$ – Oliver Hallam Apr 8 '14 at 18:36
  • \$\begingroup\$ @DigitalTrauma you can, but only once the array has been declared, otherwise it throws an exception. Eliminating declarations my code becomes char*x="11123332";for(i=8;i--;)y[(i+2)%8]=x[i]-=50; saving 8 bytes. You have to include the type specifier char so it knows what to store in *x. \$\endgroup\$ – Level River St Apr 8 '14 at 18:42
  • \$\begingroup\$ or char*x="11123332" if you're not so comfortable with auto. \$\endgroup\$ – user12205 Apr 8 '14 at 18:42
  • \$\begingroup\$ You can save another byte with i=8;while(i--) I believe. Combine with char*x= and you're looking at only 57 bytes. \$\endgroup\$ – corsiKa Apr 9 '14 at 1:44
1
\$\begingroup\$

Python - 57 54 56

r=range(-1,2);list({(i,j) for i in r for j in r}-{(0,0)})

Improved to

r=[-1,0,1];list({(i,j) for i in r for j in r}-{(0,0)})

with storage of the variable

r=[-1,0,1];l=list({(i,j) for i in r for j in r}-{(0,0)})
\$\endgroup\$
  • 2
    \$\begingroup\$ r=[-1,0,1] is shorter! \$\endgroup\$ – Roberto Bonvallet Apr 8 '14 at 17:56
  • \$\begingroup\$ You're right- nice one \$\endgroup\$ – Willem Apr 8 '14 at 17:58
  • \$\begingroup\$ Um, you need to print it. \$\endgroup\$ – user80551 Apr 8 '14 at 18:04
  • \$\begingroup\$ Only storing is needed according to the instructions, so perhaps its necessary to add l= before list at a price of 2 chars \$\endgroup\$ – Willem Apr 8 '14 at 18:05
1
\$\begingroup\$

Perl 6: 27 characters

Most simply:

$/=(-1..1 X -1..1)[^4,5..*]

Get the permutations of -1, 0, and 1 with itself, and hard-code skipping (0, 0). But that's no fun! I prefer things to be more complex: (50 characters)

$/=map {[.re,.im]},grep *.abs>0,(-1..1 X+ -i,0i,i)
\$\endgroup\$
1
\$\begingroup\$

Python - 37 bytes

a=[(i*2/5-1,-i%3-1)for i in range(8)]

An alternative at 40:

a=zip([0,1,-1]*3,[0]*3+[1]*3+[-1]*3)[1:]

And a 42 that's also interesting:

a=0,1,-1;a=zip(sum(zip(a,a,a),()),a*3)[1:]
\$\endgroup\$
1
\$\begingroup\$

APL (13 characters)

1↓,∘.,⍨0 1 ¯1

Explanation:

1↓           ⍝ Drop the first element of
  ,          ⍝ an array that comes from the matrix formed by
   ∘.,⍨      ⍝ concatenating each element with every other element
       0 1 ¯1⍝ in this array [0,1,-1]
\$\endgroup\$
1
\$\begingroup\$

Java 207 204

Way too much, I know, but its the best I could do right now...
I might think of something better...

class A{public static void main(String...a){int[][] l=new int[8][2];for(int x=-1,y,i=0;x<2;x++){for(y=-1;y<2;y++){if(x!=0||y!=0){l[i][0]=x;l[i][1]=y;System.out.print("("+l[i][0]+","+l[i][1]+")");i++;}}}}}

Sensibly:

class A {
    public static void main(String...a) {
        int[][] l = new int[8][2];
        for (int x = -1, y, i = 0; x < 2; x++) {
            for (y = -1; y < 2; y++) {
                if (x != 0 || y != 0) {
                    l[i][0] = x;
                    l[i][1] = y;
                    System.out.print("("+l[i][0]+","+l[i][1]+")");
                    i++;
                }
            }
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ main(String[] args) -> main(String...s) Saves 3 characters. \$\endgroup\$ – Averroes Apr 10 '14 at 15:10
  • \$\begingroup\$ I bet String[]a is better than String...a \$\endgroup\$ – Olathe Jul 6 '14 at 22:20
0
\$\begingroup\$

PYG (27):

P(*ItPr((-1,0,1),repeat=2))

Nothing all that special, really.

In regular python:

from itertools import product
for i in product((-1,0,1),repeat=2): print i
\$\endgroup\$
  • 1
    \$\begingroup\$ This prints (0, 0). I think this is not supposed to be included \$\endgroup\$ – Digital Trauma Apr 8 '14 at 17:29
  • \$\begingroup\$ The OP has clarified (0,0) should not be included. Let me know when you fix this and I'll remove my downvote :) \$\endgroup\$ – Digital Trauma Apr 8 '14 at 17:44
  • \$\begingroup\$ What is this "PYG", you speak of? (Presumably not a Pig Latin program or yet another package manager.) \$\endgroup\$ – Cees Timmerman Apr 11 '14 at 9:25
0
\$\begingroup\$

C, 73

main(i){for(i=0;i<9;i++)if(i-4)printf("%s(%d,%d)",i?",":"",i%3-1,i/3-1);}
\$\endgroup\$
0
\$\begingroup\$

Mathematica: 41

This works most of the time ;)

Union[RandomInteger[2,{99,2}]-1]~Drop~{5}

Equivalently:

Drop[Union@RandomInteger[2,{99,2}]-1,{5}]
\$\endgroup\$
  • \$\begingroup\$ In the first case you can lose another character, by using infix notation for RandomInteger. \$\endgroup\$ – Martin Ender Apr 11 '14 at 11:17
0
\$\begingroup\$

Matlab: 53

Similar to my other answer:

unique(round(2*rand(99,2))-1,'rows');ans([1:4,6:9],:)

I couldn't index on the output of unique directly, and I couldn't find a way around that...

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

Haskell 97

Too much for Haskell and definitely not as good as the other Haskell answer, but here's a different, albeit sucky, approach:

take 8 (zip (replicate 3 (-1) ++ replicate 3 1 ++ replicate 3 0) (concat (replicate 3 [-1,1,0])))

:D

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

Scala, 56

Self-explanatory.

def x=List(-1,0,1);for{i<-x;j<-x;if i!=0|j!=0}yield(i,j)

Ungolfed:

def x=List(-1,0,1)
for{
  i <- x
  j <- x
  if i!=0 | j!=0
} yield(i,j)
\$\endgroup\$
0
\$\begingroup\$

Perl, 42 characters:

@r=map[split","],grep/1/,glob"{-1,0,1},"x2

Uses the shell-like glob functionality to generate the list of strings ("-1,-1","-1,0","-1,1","0,-1","0,0","0,1","1,-1","1,0","1,1"), discards strings that don't contain the character "1" (i.e. drop "0,0"), then splits the strings on comma to get the desired data structure.

41 characters:

@r=map[split x],grep/1/,glob"{-1,0,1}x"x2

abuses barewords: the strings are ("-1x-1","-1x0", etc.) and the split is done on the letter x instead.

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

Fortran: 101 96

Fortran supports complex variables, so we just store the 8 values as a set of complex numbers and get ourselves a list of tuples.

complex x(8);x=reshape([(1,-1),(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1)],[8]);print*,x;end

Printing this to look nice might be a pain in the butt because printing the real and aimag components adds a lot of bytes. But we can deal with floats.


Old answer:

complex x(8);k=1;do i=-1,1;do j=-1,1;if((j==0).and.(i==0))cycle;x(k)=cmplx(i,j);k=k+1;enddo;enddo;end

This abuses implicit typing (as usual).

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

SQL 109 (that far)

create table a(x int);insert into a values(-1),(0),(1);select a.x,b.x y from a,a b where not(a.x=0 and b.x=0)

if you want to retest it, you can prepend drop table if exists a;

\$\endgroup\$

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