8 adjacent squares

15

1

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.

Karolis Juodelė

Posted 2014-04-08T16:33:01.873

Reputation: 261

Question was closed 2018-05-15T19:13:09.233

Formatted exactly like the string in your question, or listed in the native/natural format for your language? – Digital Trauma – 2014-04-08T16:52:08.180

2@DigitalTrauma, listed in natural format. – Karolis Juodelė – 2014-04-08T16:53:08.393

1Can you just clarify if the (0, 0) has to be omitted from the output? – Gareth – 2014-04-08T17:32:00.550

1@Gareth, yes, it has to be omitted. It would be 9 squares with (0,0), after all. – Karolis Juodelė – 2014-04-08T17:41:07.673

Does the list need to be output to stdout, or is simply storing it in a list/array sufficient? – Digital Trauma – 2014-04-08T17:51:07.637

1@DigitalTrauma, store only. – Karolis Juodelė – 2014-04-08T17:58:14.680

Answers

15

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.

algorithmshark

Posted 2014-04-08T16:33:01.873

Reputation: 8 144

15

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

user80551

Posted 2014-04-08T16:33:01.873

Reputation: 2 520

10

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

daniero

Posted 2014-04-08T16:33:01.873

Reputation: 17 193

Doesn't this include the 0,0? – Gareth – 2014-04-08T17:27:54.863

You don't actually need the square brackets in your first answer. – histocrat – 2014-04-08T17:28:06.480

@DigitalTrauma + Gareth Yup. So do other answers here I suspect. Fixed it anyways. – daniero – 2014-04-08T17:41:00.127

I FIXED IT!!!! give a poor guy a little breathing room before downvoting, eh? – daniero – 2014-04-08T17:42:23.523

1@DigitalTrauma :P – daniero – 2014-04-08T20:04:14.153

1You can go further: (a=-1,1,0).product(a)[0,8] is 26 chars. – algorithmshark – 2014-04-08T20:33:26.147

@algorithmshark you're right -- thanks! – daniero – 2014-04-08T20:36:00.517

9

Python 43 32

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

Inspired by @primo

isaacg

Posted 2014-04-08T16:33:01.873

Reputation: 39 268

I don't think this is 43 bytes anymore :) – 2rs2ts – 2014-04-09T17:03:22.647

It looks like 43 characters to me - am I missing something? Oh wow, that solution above is sweet. – isaacg – 2014-04-09T18:36:12.543

@issacg I had miscounted earlier, my mistake. Nice improvement, though. – 2rs2ts – 2014-04-09T18:50:27.053

I like it. Nice improvement on my 40. – primo – 2014-04-20T04:47:39.923

7

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.

Peter Taylor

Posted 2014-04-08T16:33:01.873

Reputation: 41 901

6

Haskell 32

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

The list is l (L).

isaacg

Posted 2014-04-08T16:33:01.873

Reputation: 39 268

You could do _:l=(,)<$>c<*>c to save 6 characters, although it requires import Control.Applicative. – Karolis Juodelė – 2014-04-09T04:45:54.277

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. – isaacg – 2014-04-09T04:59:11.500

6

Brainfuck, 1093 characters.

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

EDIT 209 characters

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

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

Rob

Posted 2014-04-08T16:33:01.873

Reputation: 168

5Someday, Brainfuck will win a code-golf contest ! – Michael M. – 2014-04-10T07:17:31.280

1

My solution is 5 times more concise :) http://codegolf.stackexchange.com/a/25666/20279

– Benjamin – 2014-04-10T14:51:29.133

4

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

Heiko Oberdiek

Posted 2014-04-08T16:33:01.873

Reputation: 3 841

4

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.

Gareth

Posted 2014-04-08T16:33:01.873

Reputation: 11 678

4

Mathematica 23

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

Dr. belisarius

Posted 2014-04-08T16:33:01.873

Reputation: 5 345

Cheeky. :) Very nice idea! I think precedence rules should allow you to replace Tuples[{...},2] with {...}~Tuples~2 to save another byte. – Martin Ender – 2014-04-08T18:26:06.957

@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 http://mathematica.stackexchange.com Any particular reason?

– Dr. belisarius – 2014-04-08T18:31:15.803

Can you explain how this works? – Digital Trauma – 2014-04-08T18:33:50.300

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. – Martin Ender – 2014-04-08T18:34:06.643

2@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} ) – Dr. belisarius – 2014-04-08T18:35:53.717

4

Brainfuck, 217 209 characters.

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

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

Benjamin

Posted 2014-04-08T16:33:01.873

Reputation: 141

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 – 2014-04-11T09:09:15.067

Hah.. I gave it a shot as well and came to the same amount of characters... Coincidence? – Rob – 2014-04-11T10:28:33.437

3

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.

Martin Ender

Posted 2014-04-08T16:33:01.873

Reputation: 184 808

Very nice use of common functions, Outer and Range, but they should not return the point {0,0}. – DavidC – 2014-04-08T17:32:46.123

@m.buettner Good. Thanks. +1 – Digital Trauma – 2014-04-08T17:38:02.567

3

Python 50:

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

commando

Posted 2014-04-08T16:33:01.873

Reputation: 1 053

2

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
$

Digital Trauma

Posted 2014-04-08T16:33:01.873

Reputation: 64 644

2

F# - 41 36 bytes

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

p.s.w.g

Posted 2014-04-08T16:33:01.873

Reputation: 573

1

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

Willem

Posted 2014-04-08T16:33:01.873

Reputation: 1 528

2r=[-1,0,1] is shorter! – Roberto Bonvallet – 2014-04-08T17:56:36.480

You're right- nice one – Willem – 2014-04-08T17:58:03.183

Um, you need to print it. – user80551 – 2014-04-08T18:04:29.680

Only storing is needed according to the instructions, so perhaps its necessary to add l= before list at a price of 2 chars – Willem – 2014-04-08T18:05:51.933

1

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;

Level River St

Posted 2014-04-08T16:33:01.873

Reputation: 22 049

Can you do char *x="11123332" to save a char? – Digital Trauma – 2014-04-08T18:22:27.083

1or just auto x="11123332" to save another – Oliver Hallam – 2014-04-08T18:36:37.757

@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. – Level River St – 2014-04-08T18:42:32.213

or char*x="11123332" if you're not so comfortable with auto. – user12205 – 2014-04-08T18:42:56.597

You can save another byte with i=8;while(i--) I believe. Combine with char*x= and you're looking at only 57 bytes. – corsiKa – 2014-04-09T01:44:15.070

(i+2)%8 -> i+2&7 – ugoren – 2014-04-09T11:01:20.673

1

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)

Mouq

Posted 2014-04-08T16:33:01.873

Reputation: 906

1

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

primo

Posted 2014-04-08T16:33:01.873

Reputation: 30 891

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[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++;
                }
            }
        }
    }
}

kukac67

Posted 2014-04-08T16:33:01.873

Reputation: 2 159

main(String[] args) -> main(String...s) Saves 3 characters. – Averroes – 2014-04-10T15:10:12.023

I bet String[]a is better than String...a – Olathe – 2014-07-06T22:20:52.550

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]

Zaq

Posted 2014-04-08T16:33:01.873

Reputation: 1 525

0

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

ɐɔıʇǝɥʇuʎs

Posted 2014-04-08T16:33:01.873

Reputation: 4 449

1This prints (0, 0). I think this is not supposed to be included – Digital Trauma – 2014-04-08T17:29:51.533

The OP has clarified (0,0) should not be included. Let me know when you fix this and I'll remove my downvote :) – Digital Trauma – 2014-04-08T17:44:58.200

What is this "PYG", you speak of? (Presumably not a Pig Latin program or yet another package manager.) – Cees Timmerman – 2014-04-11T09:25:59.913

0

C, 73

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

V-X

Posted 2014-04-08T16:33:01.873

Reputation: 747

0

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

krs013

Posted 2014-04-08T16:33:01.873

Reputation: 201

In the first case you can lose another character, by using infix notation for RandomInteger. – Martin Ender – 2014-04-11T11:17:27.267

0

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

krs013

Posted 2014-04-08T16:33:01.873

Reputation: 201

0

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

kukac67

Posted 2014-04-08T16:33:01.873

Reputation: 2 159

0

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)

lambruscoAcido

Posted 2014-04-08T16:33:01.873

Reputation: 401

0

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.

hobbs

Posted 2014-04-08T16:33:01.873

Reputation: 2 403

0

JavaScript (ES5) 52 bytes

[[n=-1,n],[n,0],[n,1],[0,n],[0,1],[1,n],[1,0],[1,1]]

Javascript (ES5) (algorithmic) 57 bytes

for(a=-2,x=[];++a<2;)for(b=-2;++b<2;)a|b&&x.push([a,b]);x

nderscore

Posted 2014-04-08T16:33:01.873

Reputation: 4 912

0

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

Kyle Kanos

Posted 2014-04-08T16:33:01.873

Reputation: 4 270

0

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;

Einacio

Posted 2014-04-08T16:33:01.873

Reputation: 436

0

C# 140 chars

Shorter version:

List<string> a=new List<string>();for(int i=0,m=-1;i<3;i++,m++){for(int j=0,n=-1;j<3;j++,n++){if(m==0 && n==0)continue;array.Add(m+","+n);}}

Easy to read version:

List<string> array = new List<string>();
for (int i = 0, m = -1; i < 3; i++, m++)
{
    for(int j = 0, n = -1; j < 3; j++, n++)
    {
         if (m == 0 && n == 0) continue;
         array.Add(m + "," + n);
    }
}

s3l1n

Posted 2014-04-08T16:33:01.873

Reputation: 11