The title says it all. Your goal is to write a program that forms a w×h rectangle of characters that can be rotated and re-run to output the number of 90° Counter-Clockwise (CCW) rotations that have been done.

For example, if the 3×2 program

abc
def

solved the problem, it would initially output 0, and successive rotations of 90° CCW

cf fed da
be cba eb
ad fc

would output 1, 2, and 3 respectively.

Using comments makes this a trivial task is most languages. In Ruby for example, it can be done in a 7×7 rectangle:

###p###
### ###
###1###
p 0#2 p
###3###
### ###
###p###

The challenge is to do this without any sort of comments.

Scoring

Your score is w*h, the area of your rectangle. Newlines are excluded. In other words, code-golf, newlines not counted.

The score for the Ruby example is 49 (though of course it is invalid since it has comments).

Notes

Your code must really be rectangular with no missing characters at the end of lines.

If you wish you may output other legal "mod 90°" values instead of 0 1 2 3. So 8 is fine instead of 0, and -1 is fine instead of 3, etc.

To be more specific about what a comment is, is a comment any unevaluated code? Unparsed code?
–
isaacgJul 8 '14 at 21:59

I mean that none of your language's traditional "comment characters" should appear in any of the 4 rotated versions. So for C++, two slashes next to each other should never appear, though one may be used alone. Likewise with /*. I hope that clarifies it.
–
Calvin's HobbiesJul 8 '14 at 22:09

What about languages that do not have comment characters?
–
isaacgJul 8 '14 at 22:19

So code like echo 0;exit;e in bash is allowed?
–
user23013Jul 8 '14 at 22:22

If there is no way to comment then you needn't worry. @user23013 That bash is fine.
–
Calvin's HobbiesJul 8 '14 at 22:40

20 Answers
20

APL (1x3 = 3)

This solution uses the extra rule that any output that is correct mod 4 works.

In APL, x!y is the number of way to choose x elements from y, commonly known as binom(y,x) or choose(y,x). Let's check that each rotation gives the right answer.

0 rotations

5!3

There's no way to choose 5 elements from 3, so we get 0, which is automatically printed.

1 CCW rotation

3
!
5

APL happily evaluates each line, getting the number 3, the operator !, and then the number 5, printing only the last of these (5), which is 1 mod 4.

2 CCW rotations

3!5

This is binom(5,3), which is (5*4*3*2*1)/(3*2*1)/(2*1) = 10, which is 2 mod 4.

3 CCW rotations

5
!
3

As before, only the last-evaluated value of 3 is printer.

I don't actually know APL, so please tell me if I got any of the explanation wrong. I found it by trial and error as the first language on this site that:

Automatically prints the result of an expression

Given multiple lines of expressions, only outputs the last one

Has no issue with an operator standing alone on a line

Takes operators infix

Has a single-character arithmetic binary operator that is asymmetric (aRb != bRa), and flexible enough to return a variety of numbers.

For (5), I went down the list of APL dyadic functions. My first candidate operation was the integer division / of C and Python 2, but APL division ÷ gives floats. Exponentiation is tempting, but fails because a and a^b have the same parity but are gotten by consecutive rotations (unless b=0, but then b^a=0). Boolean operators like < give 0 and 1 180 degrees apart, which doesn't work. Finally, I found the binomial operator ! and tried numbers until I got some that work.

Thanks to Quincunx for his confidence that there exists a smaller solution than 2x2.

Ruby, 7×9 (63)

30;p
0||p=p
0|0;p;p
;p;p|p;
p=p ||0
;p p;
2||p =p
00;1
p

A bit longer than the other solution, but at least this solution doesn't depend on any implicit printing or rule abuse. For all four rotations, the full code is parsed and other than some short-circuiting, all of it is executed. Surprisingly, there's absolutely no symmetry in the code

This solution relies on the fact that it's still possible to call the p function (which is used to print the numbers) even if a variable with the same name has already been defined. For example, something like p p calls the function p with the variable p as argument (thus, printing the value of p).

Explanation for some of the common expressions used in the code:

p: As mentioned above, this is either a function call or a variable. When the variable is not defined, this calls the function p without arguments, which does nothing and returns nil.

p p: Prints the variable p.

p|x: When p is the function, this is identical to nil|x, which returns true/false depending on the value of x. If p is an integer, it's bitwise or. Either way, this statement has no side effect.

p=p||x: Effectively the same as p||=x (conditional assignment) with the advantage of being syntactically valid and a no-op when reversed.

Symmetric version (9×10 = 90)

p
0 * 0
00100
0||2* p
p *0||0
00300
0 * 0
p

This is the shortest symmetric solution (C2 when ignoring the numbers to print) I could come up with.

Test script

Here's a test script to verify the code above (the # at the line ends have been added so that the whitespace doesn't get stripped and are removed before execution):

It may not be the shortest but this is exactly the kind of obfuscated brilliance I was looking for :D
–
Calvin's HobbiesJul 8 '14 at 23:39

1

@Calvin'sHobbies Does this meet the "Your code must really be rectangular with no missing characters at the end of lines" restriction?
–
Joshua TaylorJul 10 '14 at 15:00

@JoshuaTaylor It's rectangular, some lines are just padded with spaces (which, as far as I'm concerned, is a character). If that wouldn't meet the restriction for whatever reason, I could also pad the lines with semicolons instead ...
–
VenteroJul 10 '14 at 15:06

@Ventero I certainly hope this legal; I'm just trying to understand what was meant in the question.
–
Joshua TaylorJul 10 '14 at 15:10

Assuming the language here simply prints the last number it evaluates, I don't believe a 1*n (one-number) solution exists for any n. This is because the parity of the whole number equals the parity of its rightmost digit, but rotations 90 degrees apart must yield different parities. So, 2*2 is the minimum for this approach.
–
xnorJul 10 '14 at 20:10

While recent changes to pyth have made 2 digit numbers harder to generate (A change that I am considering reverting), older versions of Pyth have easy two digit number generation, which, combined with the implicit printing and the fact that all lines but the first are ignored, gives the following solution:

Python - 23 x 23 = 529

Ok, this question has already a winner, but there is no Python solution, yet. So I thought about it - heavily! - and found a way to make the bulky print command working in any direction without producing errors when parsed from one of the other directions.

The breakthrough was the following line:

'"\'';forward_code;"';backward_code;""\'"''

While the forward_code is executed, the backward_code is part of a string and thus not printed. This is exactly the other way around when reading backwards.

So combined with two more directions and fine-tuned to get all quotes matching correctly I end up with the following solution:

I think this is the shortest answer not to require the "mod 90°" rule-relaxation:

3z1
0p2

Outputs 0, 1, 2 or 3 for each rotation.

For the 0, 2 and 3 rotations, the p simply pops and prints the last number literal to have been pushed to the stack. For the 1 rotation, the z pushes the current stack depth (1) to the stack, then the p pops and prints it.

It does give different results when rotated. @Cory, That's clever! I'm too used to working in a language without the comma operator.
–
lrnJul 10 '14 at 20:01

@lrn Yes, it gives different results, but that isn't the requirement. The program has to print 0 (mod 4) when not rotated, and 1, 2 or 3 (mod 4) when rotated the respective number of times...
–
zennehoyJul 11 '14 at 13:48

The second note of the original poster allows any values for the rotated versions, as long as they are distinct.
–
lrnJul 11 '14 at 23:58

CJam / GolfScript - 3*3

Piet, 49

I made a point only to use yellow and red colors, and to try and make it roughly symmetrical. When rotated, it prints 0, 1, 2 or 3. Exiting the program in Piet is hard, and takes up around half the space in the picture, unfortunately.

Since Aheui does not have a letter that pushes 1 onto the stack, I decided to print 0, 5, 2, and 3.

Explanation: 바 and 반 push 0 and 2, respectively, onto the stack and moves the cursor right by one character. 뷸 and 뷷 push 5 and 3, respectively, onto the stack and moves the cursor down by two characters. 몽 pops and prints the number in the stack and moves the cursor up by one character. 희 terminates the program.

@AMK: Unfortunately, this produces a syntax error in line ..3 psid. You need at least three dots at the beginning of each line to indicate a line break and to ignore the remaining characters.
–
FalkoSep 1 '14 at 15:53