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.

• Formatted exactly like the string in your question, or listed in the native/natural format for your language? Apr 8, 2014 at 16:52
• @DigitalTrauma, listed in natural format. Apr 8, 2014 at 16:53
• Can you just clarify if the (0, 0) has to be omitted from the output? Apr 8, 2014 at 17:32
• @Gareth, yes, it has to be omitted. It would be 9 squares with (0,0), after all. Apr 8, 2014 at 17:41
• @DigitalTrauma, store only. Apr 8, 2014 at 17:58

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

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

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

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

# Python 43 32

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


Inspired by @primo

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

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

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


The list is l (L).

• You could do _:l=(,)<$>c<*>c to save 6 characters, although it requires import Control.Applicative. Apr 9, 2014 at 4:45 • 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. Apr 9, 2014 at 4:59 # Brainfuck, 1093 characters. ++++++++[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++>+++++++++++++>++++++++++++++>+++++++++++++++>++++++++++++++++<<<<<<<<<<<<<<<<-]>>>>>>>>>>>+++.---<<<<<<<<<<<>>>>>.<<<<<>>>>>>+.-<<<<<<>>>>>>----.++++<<<<<<>>>>>>---.+++<<<<<<>>>>>>+.-<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>+.-<<<<<<>>>>>>----.++++<<<<<<>>>>>>.<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>+.-<<<<<<>>>>>>----.++++<<<<<<>>>>>>+.-<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>.<<<<<<>>>>>>----.++++<<<<<<>>>>>>+.-<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>---.+++<<<<<<>>>>>>+.-<<<<<<>>>>>>----.++++<<<<<<>>>>>>+.-<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>---.+++<<<<<<>>>>>>+.-<<<<<<>>>>>>----.++++<<<<<<>>>>>>.<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>---.+++<<<<<<>>>>>>+.-<<<<<<>>>>>>----.++++<<<<<<>>>>>>---.+++<<<<<<>>>>>>+.-<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>.<<<<<<>>>>>>----.++++<<<<<<>>>>>>---.+++<<<<<<>>>>>>+.-<<<<<<>>>>>+.-<<<<<>>>>>>>>>>>>---.+++<<<<<<<<<<<<.  # EDIT 209 characters ++++++++++[>+++++++++>++++>+++++>++++<<<<-]>+.>.>-.>++++.<.<+.>>.<<-.>.>.<-.<+.>>.<<-.>+.>.+.-<.<+.>>.<<-.>-.>.<+.<+.>>.<<-.>-.>.+.<+.<+.>>-.<<-.>>+.<.>-.<.<+.>>.<<-.>>+.<.>-.<-.<+.>>.<<-.>>+.<+.>-.+.<.<+.<++.  Original by Benjamin - i gave it another shot as well and got to 209 characters as well. # 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])


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

# Mathematica 23

Most@Tuples[{-1,1,0},2]
(*
{{-1, -1}, {-1, 1}, {-1, 0}, {1, -1}, {1, 1}, {1, 0}, {0, -1}, {0, 1}}
*)

• Cheeky. :) Very nice idea! I think precedence rules should allow you to replace Tuples[{...},2] with {...}~Tuples~2 to save another byte. Apr 8, 2014 at 18:26
• @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? Apr 8, 2014 at 18:31
• Can you explain how this works? Apr 8, 2014 at 18:33
• 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. Apr 8, 2014 at 18:34
• @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} ) Apr 8, 2014 at 18:35

# Brainfuck, 217 209 characters.

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

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

• 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.
– Rob
Apr 11, 2014 at 9:09
• Hah.. I gave it a shot as well and came to the same amount of characters... Coincidence?
– Rob
Apr 11, 2014 at 10:28

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

• Very nice use of common functions, Outer and Range, but they should not return the point {0,0}. Apr 8, 2014 at 17:32
• @m.buettner Good. Thanks. +1 Apr 8, 2014 at 17:38

# Python 50:

l=[(a,b)for a in[1,0,-1]for b in[1,0,-1]];l.pop(4)


# Bash, 25 characters

Storing the list of coordinates as a bash array a:

a=({-1..1},{-1..1});a=


Output:

$a=({-1..1},{-1..1});a=$ echo ${a[@]} -1,-1 -1,0 -1,1 0,-1 0,1 1,-1 1,0 1,1$


# F# - 41 36 bytes

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


# C++, 60

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

char x[]="11123332",y,i;for(i=8;i--;)y[(i+2)%8]=x[i]-=50;

• Can you do char *x="11123332" to save a char? Apr 8, 2014 at 18:22
• or just auto x="11123332" to save another Apr 8, 2014 at 18:36
• @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. Apr 8, 2014 at 18:42
• or char*x="11123332" if you're not so comfortable with auto. Apr 8, 2014 at 18:42
• You can save another byte with i=8;while(i--) I believe. Combine with char*x= and you're looking at only 57 bytes. Apr 9, 2014 at 1:44

# Python - 5754 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)})

• r=[-1,0,1] is shorter! Apr 8, 2014 at 17:56
• You're right- nice one Apr 8, 2014 at 17:58
• Um, you need to print it. Apr 8, 2014 at 18:04
• Only storing is needed according to the instructions, so perhaps its necessary to add l= before list at a price of 2 chars Apr 8, 2014 at 18:05

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


## 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,*3+*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:]


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


# 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;for(int x=-1,y,i=0;x<2;x++){for(y=-1;y<2;y++){if(x!=0||y!=0){l[i]=x;l[i]=y;System.out.print("("+l[i]+","+l[i]+")");i++;}}}}}


Sensibly:

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

• main(String[] args) -> main(String...s) Saves 3 characters. Apr 10, 2014 at 15:10
• I bet String[]a is better than String...a Jul 6, 2014 at 22:20

# 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

• This prints (0, 0). I think this is not supposed to be included Apr 8, 2014 at 17:29
• The OP has clarified (0,0) should not be included. Let me know when you fix this and I'll remove my downvote :) Apr 8, 2014 at 17:44
• What is this "PYG", you speak of? (Presumably not a Pig Latin program or yet another package manager.) Apr 11, 2014 at 9:25

# C, 73

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


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

• In the first case you can lose another character, by using infix notation for RandomInteger. Apr 11, 2014 at 11:17

# Matlab: 53

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

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

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


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

## 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)],);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.

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

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;