8
\$\begingroup\$

Let a function receive two integers i and j between 0 and 3, it should return the value in position (i,j) in the following 2D array:

1, 0, -1, 0
1, 0, -1, 0
0, 1, 0, -1
0, -1, 0, 1

Shortest code in any variant of C / C++ wins.

\$\endgroup\$
7
  • 19
    \$\begingroup\$ Why limit this to C/C++? \$\endgroup\$ Commented Jan 14, 2014 at 1:52
  • 4
    \$\begingroup\$ probably because the author realizes it is 3-4 chars in golfscript, and wants to make the competition more fair \$\endgroup\$
    – mniip
    Commented Jan 14, 2014 at 13:32
  • 3
    \$\begingroup\$ @Kendall Frey - Actually the true code golf contests are possible only when they target single language (or family of languages). Because there is no point to compete when someone is running on foot, someone is swimming, and others use helicopter. \$\endgroup\$
    – SergeyS
    Commented Jan 15, 2014 at 7:59
  • 3
    \$\begingroup\$ @mniip - I do not think this is 3 or 4 characters. I think it is about best C/C++ solution minus length of return keyword ;) \$\endgroup\$
    – SergeyS
    Commented Jan 15, 2014 at 8:10
  • 3
    \$\begingroup\$ @SergeyS Even if I agreed, the rest of the site does not. Language-specific questions are discouraged here. \$\endgroup\$ Commented Jan 15, 2014 at 12:50

9 Answers 9

10
\$\begingroup\$

40

f(i,j){return~-(2434352710>>8*i+2*j&3);}

A hackish 35 solution (xxd dump):

0000000: 6628 692c 6a29 7b72 6574 7572 6e7e 2d28  f(i,j){return~-(
0000010: 695b 2246 4619 9122 5d3e 3e32 2a6a 2633  i["FF.."]>>2*j&3
0000020: 293b 7d                                  );}

If proper octal escapes are used, it's the same 40 characters:

f(i,j){return~-(i["FF\31\221"]>>2*j&3);}
\$\endgroup\$
2
  • \$\begingroup\$ You can save 8 bytes by writing f(int i,int j) as f(i,j) (the arguments are int by default) \$\endgroup\$
    – r3mainer
    Commented Jan 14, 2014 at 2:40
  • \$\begingroup\$ I see, i actually tried removing those ints, but somehow failed \$\endgroup\$
    – mniip
    Commented Jan 14, 2014 at 2:41
10
\$\begingroup\$

C99, 34

f(i,j){return(i<3?1-j+i/2:j-2)%2;}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Also works in C89. And function text is 34 chars \$\endgroup\$
    – AMK
    Commented Jan 14, 2014 at 19:48
  • 2
    \$\begingroup\$ I marked it C99 since C99 actually specifies the behavior of '%', whereas prior versions left it implementation-defined. And yeah, I was counting the newline. I'll fix that. \$\endgroup\$
    – aghast
    Commented Jan 14, 2014 at 19:54
7
\$\begingroup\$

32

f(i,j){return~-(j+2-i*i/3&3)%2;}
\$\endgroup\$
2
  • \$\begingroup\$ A winner by three chars :) \$\endgroup\$ Commented Jan 15, 2014 at 6:55
  • 1
    \$\begingroup\$ f(i,j){return-~-(i*i/3^j)%2;} is 29. I found it by running an exhaustive(ish) search. I can't post it because the question is closed, so I'll put it here. \$\endgroup\$
    – lynn
    Commented Sep 13, 2021 at 10:18
5
\$\begingroup\$

C, 29

I know this one seems kinda unfair but does it actually work for anyone? ^_^

f(i,j){j=("&&4+"[i]>>j)%3-1;}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to the site! You should add your byte count to the post as this is a code golf question. Also, you may want to add a link to an interpreter so that others can test your solution, such as Try It Online! \$\endgroup\$ Commented Jan 7, 2018 at 20:56
4
\$\begingroup\$

38

f(i,j){return(i/2+j+1)*(1-(i-j&2))%2;}

37

f(i,j){return(i/2+j+1)*-~-(i-j&2)%2;}
\$\endgroup\$
4
  • \$\begingroup\$ You can save 2 chars if you change (1-(i-j&2)) to ~-(j&2-i) \$\endgroup\$
    – mniip
    Commented Jan 14, 2014 at 13:29
  • \$\begingroup\$ Tried your suggestion and it gives wrong results.(i-j&2) is actually ((i-j)&2) so your trick changes the logic here. or am i missing something? \$\endgroup\$
    – fsw
    Commented Jan 14, 2014 at 13:40
  • \$\begingroup\$ oh, I kind of overlooked that, still can save 1 by replacing with -~-(i-j&2) \$\endgroup\$
    – mniip
    Commented Jan 14, 2014 at 13:45
  • \$\begingroup\$ @mniip oh yes now it works, thx \$\endgroup\$
    – fsw
    Commented Jan 14, 2014 at 13:49
3
\$\begingroup\$

C++, 70, 45, 44

f(i,j){return(j/2&i^~i&~j>>1)*~-(j-i&2)%2;}

The Karnaugh map approach, mixed with a bit of fsw's solution.

If A is (j>>1) and D is (i&1), the function should return "-~-(j-i&2)%2" (fsw's solution) multiplied by the Karnaugh map ~A~D + AD.

I feel there is a better solution for this using the Karnaugh map, but it's too late to think of it. Maybe tomorrow :)

Thanks for the comments, shaved off many chars.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ you don't need most of the parentheses here: ((~(j>>1)&~(i&1)^j>>1&(i&1))&1) also you can move the &1 into inside the left side of ^ : (~(j>>1)&~(i&1)&1^j>>1&(i&1)) \$\endgroup\$
    – mniip
    Commented Jan 15, 2014 at 0:13
2
\$\begingroup\$

C#

int f(int i,int j){return "2101210112101012"[i*4+j]-49;}
\$\endgroup\$
2
  • 3
    \$\begingroup\$ 4 spaces will make that code highlighted... \$\endgroup\$
    – Kyle Kanos
    Commented Jan 14, 2014 at 2:11
  • 2
    \$\begingroup\$ You can save one character by removing the space after return. But it's arguable whether C# can be considered a variant of C/C++, despite the name. It's derived from C, but so is Java. \$\endgroup\$
    – Bob
    Commented Jan 14, 2014 at 5:08
2
\$\begingroup\$

48

f(a,b){return 10308>>4*a+b&1?-1:43605>>4*a+b&1;}
\$\endgroup\$
1
\$\begingroup\$

C, 45

f(i,j){i=i+(j>1)*(7-2*j)&3;return i&1?0:1-i;}
\$\endgroup\$

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