66
\$\begingroup\$

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.
  • The output may go to the console or into a file.
  • Standard loopholes apply.

I hope this, my first question, really intrigues some people. Enjoy!

\$\endgroup\$
10
  • 1
    \$\begingroup\$ To be more specific about what a comment is, is a comment any unevaluated code? Unparsed code? \$\endgroup\$
    – isaacg
    Commented Jul 8, 2014 at 21:59
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 8, 2014 at 22:09
  • \$\begingroup\$ What about languages that do not have comment characters? \$\endgroup\$
    – isaacg
    Commented Jul 8, 2014 at 22:19
  • \$\begingroup\$ So code like echo 0;exit;e in bash is allowed? \$\endgroup\$
    – jimmy23013
    Commented Jul 8, 2014 at 22:22
  • \$\begingroup\$ If there is no way to comment then you needn't worry. @user23013 That bash is fine. \$\endgroup\$ Commented Jul 8, 2014 at 22:40

36 Answers 36

48
+200
\$\begingroup\$

APL (1x3 = 3)

5!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:

  1. Automatically prints the result of an expression
  2. Given multiple lines of expressions, only outputs the last one
  3. Has no issue with an operator standing alone on a line
  4. Takes operators infix
  5. 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.

\$\endgroup\$
5
  • 8
    \$\begingroup\$ Man, it's nice to see an answer that uses a non-esoteric language and wins. \$\endgroup\$ Commented Jul 12, 2014 at 18:40
  • \$\begingroup\$ I've accepted this as the answer since it is the shortest according to my rules, but I want to make Ventero's answer (codegolf.stackexchange.com/a/33162/26997) a very honorable mention. It follows the true essence of the problem. \$\endgroup\$ Commented Jul 15, 2014 at 12:43
  • \$\begingroup\$ It also works in J in the exact form you gave. \$\endgroup\$ Commented Sep 1, 2014 at 15:09
  • \$\begingroup\$ Actually, doesn't APL print both the 3 and the 5 in the 90° rotation? \$\endgroup\$
    – FUZxxl
    Commented Apr 8, 2015 at 11:14
  • \$\begingroup\$ @FUZxxl I think this only works in ngn/apl. \$\endgroup\$
    – Adám
    Commented Nov 28, 2016 at 9:36
44
\$\begingroup\$

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

rotate=->s{s.split($/).map{|i|i.chars.reverse}.transpose.map(&:join).join($/)}

s=<<EOD.gsub(?#,"")
 30;p  #
0||p=p #
0|0;p;p#
;p;p|p;#
p=p ||0#
;p   p;#
2||p =p#
 00;1  #
     p #
EOD


puts ">>> 0°"
eval s
puts ">>> 90°"
eval rotate[s]
puts ">>> 180°"
eval rotate[rotate[s]]
puts ">>> 270°"
eval rotate[rotate[rotate[s]]]
\$\endgroup\$
7
  • 10
    \$\begingroup\$ It may not be the shortest but this is exactly the kind of obfuscated brilliance I was looking for :D \$\endgroup\$ Commented Jul 8, 2014 at 23:39
  • 1
    \$\begingroup\$ @Calvin'sHobbies Does this meet the "Your code must really be rectangular with no missing characters at the end of lines" restriction? \$\endgroup\$ Commented Jul 10, 2014 at 15:00
  • \$\begingroup\$ @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 ... \$\endgroup\$
    – Ventero
    Commented Jul 10, 2014 at 15:06
  • \$\begingroup\$ @Ventero I certainly hope this legal; I'm just trying to understand what was meant in the question. \$\endgroup\$ Commented Jul 10, 2014 at 15:10
  • 1
    \$\begingroup\$ @Ventero Padding with spaces (or really anything besides comments and newlines) is just fine. \$\endgroup\$ Commented Jul 10, 2014 at 18:52
34
\$\begingroup\$

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:

''"''""''"''"''"'"''""'
"  ""                 "
"  \\                 "
'"\'';print 1;"'""\'"''
'"\''"';3 tnirp;""\'"''
"  ;""                "
"  p'                 "
'  r;                 '
'  i2                 '
"  n                  "
"  tt                 "
'   n                 '
'  4i                 '
"  ;r     .-=<>=-.    "
' \"p    /__----__\   '
"  ';'  |/ (')(') \|  "
"  ""    \   __   /   "
'  ""    .`--__--`.   '
"  \\   /    :|    \  "
'  ''  (_)   :|   (_) '
'  ""    |___:|____|  '
"  ''    |_________|  "
''"''""''"''"''"'"''""'

Edit: I found a way to deal with all that whitespace. ;)

\$\endgroup\$
4
  • \$\begingroup\$ Is there any way to also put p=print in the file, since you've got so much whitespace currently? (Python 3, of course) \$\endgroup\$
    – isaacg
    Commented Aug 22, 2014 at 0:09
  • \$\begingroup\$ @isaacg: I don't think so. The definition p=print is also lengthy. And more importantly: You can't reuse p after rotating the code! \$\endgroup\$
    – Falko
    Commented Aug 22, 2014 at 0:18
  • \$\begingroup\$ I guess you're right. It's two bad, though. \$\endgroup\$
    – isaacg
    Commented Aug 22, 2014 at 0:21
  • 15
    \$\begingroup\$ Nominated: Best Use of Whitespace Award. \$\endgroup\$
    – primo
    Commented Sep 1, 2014 at 13:41
33
\$\begingroup\$

GolfScript, 4 (2x2)

43
12

Prints 4312 which is 0 (mod 4). The rotations print 3241 (1 mod 4), 2134 (2 mod 4), and 1423 (3 mod 4).

Prompted by:

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.

There are actually many sets of numbers for which this works. I found these with this Python program:

def f(a,b,c,d):
    return int("%i%i%i%i"%(a,b,c,d))
for a in range(10):
    for b in range(10):
        for c in range(10):
            for d in range(10):
                candidate = f(a,b,c,d) % 4 == 0
                candidate &= f(b,d,a,c) % 4 == 1
                candidate &= f(d,c,b,a) % 4 == 2
                candidate &= f(c,a,d,b) % 4 == 3
                if candidate:
                    print("%i, %i, %i, %i"%(a,b,c,d))

Although the program outputs 0s (which probably wouldn't work), the valid solutions are of the form

ab
cd

Where a∈{4,8}, b∈{3,7}, c∈{1,5,9}, d∈{2,6}. IE (a,b,c,d)∈{4,8}×{3,7}×{1,5,9}×{2,6} which is 24 solutions.

\$\endgroup\$
10
  • 7
    \$\begingroup\$ Very clever. I honestly thought that extra rule was innocuous. \$\endgroup\$ Commented Jul 8, 2014 at 23:24
  • 5
    \$\begingroup\$ @Everybody Note that this is not the smallest possible program. A 2x1 is possible, and it is the smallest possible. So get to work! \$\endgroup\$
    – Justin
    Commented Jul 8, 2014 at 23:37
  • 1
    \$\begingroup\$ @isaacg Your score is w*h, the area of your rectangle. Newlines are excluded. In other words, code-golf, newlines not counted. \$\endgroup\$ Commented Jul 9, 2014 at 2:03
  • 2
    \$\begingroup\$ @Quincunx Well then post it! \$\endgroup\$
    – tomsmeding
    Commented Jul 10, 2014 at 12:41
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – xnor
    Commented Jul 10, 2014 at 20:10
25
\$\begingroup\$

BASIC, 64

Won't win, but here it is anyway. (Tested in Chipmunk Basic)

?0:END:?
:::::::1
D:::::::
N::::::E
E::::::N
:::::::D
3:::::::
?:DNE:2?

Note: ? is shorthand for PRINT in various dialects of BASIC. Although there are lots of syntax errors in the code, the END statement in the first line prevents them from being seen by the interpreter.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Actually, your scores are 64 and 16, respectively. Newlines don't count. Also, why not split the answer? \$\endgroup\$
    – Justin
    Commented Jul 8, 2014 at 23:26
  • \$\begingroup\$ Oh, missed that bit. I would have split the answers if they weren't occupying joint last place. There doesn't seem to be much point otherwise :-) \$\endgroup\$
    – r3mainer
    Commented Jul 8, 2014 at 23:29
  • \$\begingroup\$ You're not last anymore :-) \$\endgroup\$
    – Justin
    Commented Jul 8, 2014 at 23:31
  • \$\begingroup\$ I'd give you bonus points for getting a nice round number of characters. The actual +1 is for the creative use of END though :) \$\endgroup\$
    – CompuChip
    Commented Jul 10, 2014 at 14:15
21
\$\begingroup\$

Pyth, 9 characters (3x3)

0 1

3 2

In pyth, everything is printed by default, unless it is preceded by a space. Lines after the first line are for user input, and are not evaluated in this program.

Another way to get 9 characters:

"0"
3 1
"2"

Pyth 1.0.5, 4 characters

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:

32
41

Prints 32,21,14,43.

\$\endgroup\$
4
  • \$\begingroup\$ Well, that's just fun. +1 \$\endgroup\$
    – seequ
    Commented Jul 8, 2014 at 22:36
  • \$\begingroup\$ Shucks, I was hoping it wasn't that easy. You are missing 3 characters on your middle line but I'm guessing that's due to the auto-formatting here. \$\endgroup\$ Commented Jul 8, 2014 at 22:50
  • \$\begingroup\$ @Calvin'sHobbies Sorry, I see. It chomped my trailing spaces. \$\endgroup\$
    – isaacg
    Commented Jul 8, 2014 at 22:53
  • \$\begingroup\$ @Quincunx Oh, I missed that part of the scoring. Thanks for the fixes. \$\endgroup\$
    – isaacg
    Commented Jul 8, 2014 at 23:29
18
\$\begingroup\$

Befunge, 16

0.@1
@@@.
.@@@
[email protected]

Explanation: Digits from 0 to 9 push the corresponding number onto the stack, . pops a value from the stack and prints it as an integer, and @ ends the program.

(tested here)

\$\endgroup\$
4
  • \$\begingroup\$ Doesn't this output the number of times it was rotated counterclockwise? \$\endgroup\$
    – seequ
    Commented Jul 10, 2014 at 16:23
  • \$\begingroup\$ @TheRare Yes. It does. That is what it should do. \$\endgroup\$
    – Justin
    Commented Jul 10, 2014 at 20:32
  • 1
    \$\begingroup\$ I seem to be utterly blind. Have a +1 because of that. \$\endgroup\$
    – seequ
    Commented Jul 10, 2014 at 20:43
  • \$\begingroup\$ Befunge!!! Still my favourite language of all times ever forever eternally so! \$\endgroup\$
    – steffen
    Commented May 4, 2017 at 20:13
18
\$\begingroup\$

Piet, 49

A Piet Program

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Seems like the right tool for the job. \$\endgroup\$ Commented Apr 6, 2016 at 7:38
  • \$\begingroup\$ O_o, I cried finding out about this programming language! Just now. Beautiful. In so many ways! Thank you! \$\endgroup\$
    – steffen
    Commented May 4, 2017 at 20:21
16
\$\begingroup\$

GNU dc, 6 (3x2)

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.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ So 311/0p2 would also work? \$\endgroup\$
    – Neil
    Commented Jul 13, 2014 at 9:18
  • \$\begingroup\$ @Neil Yes, good one. Doesn't affect the golf score though :) \$\endgroup\$ Commented Jul 14, 2014 at 14:51
  • \$\begingroup\$ It does affect the golf score, but negatively, since newlines are not counted, but slashes are. \$\endgroup\$
    – M.Herzkamp
    Commented Aug 31, 2015 at 11:58
  • \$\begingroup\$ @M.Herzkamp My assumption was that Neil meant 311\n0p2, where \n is a newline character. Otherwise it would be meaningless within the context of dc. \$\endgroup\$ Commented Aug 31, 2015 at 17:35
10
\$\begingroup\$

GolfScript, 9 (3x3)

0}1
} }
3}2

Sort of abusing the rules. The } happens to end the program if there is no matching {, and the contents of the stack are printed at program end.

\$\endgroup\$
3
  • \$\begingroup\$ Don't forget to count the newline as a character. \$\endgroup\$
    – isaacg
    Commented Jul 8, 2014 at 23:27
  • 2
    \$\begingroup\$ @isaacg Your score is w*h, the area of your rectangle. Newlines are excluded. In other words, code-golf, newlines not counted. \$\endgroup\$ Commented Jul 9, 2014 at 2:02
  • \$\begingroup\$ I didn't know that } can be unbalanced. Nice trick. \$\endgroup\$ Commented Jul 9, 2014 at 7:00
9
\$\begingroup\$

JavaScript, 4

03
12

When you execute this program (or a rotation of this program) in a javaScript console, only the last line is evaluated and echoed in the console.

So:

12 modulo 4 == 0
01 modulo 4 == 1
30 modulo 4 == 2
23 modulo 4 == 3

Here are all the similar 2x2 programs that work too:

03
12

03
16

03
52

03
56

03
92

03
96

07
12

07
16

07
52

07
56

07
92

07
96

43
12

43
16

43
52

43
56

43
92

43
96

47
12

47
16

47
52

47
56

47
92

47
96

83
12

83
16

83
52

83
56

83
92

83
96

87
12

87
16

87
52

87
56

87
92

87
96

In other terms,

ab
cd

where a is in [0,4,8], b is in [3,7], c is in [1,5,9], and d is in [2,6]

\$\endgroup\$
8
\$\begingroup\$

CJam / GolfScript - 3*3

2;3
;7;
1;0

The semicolon pops the previous number, thus only the bottom right corner is printed.

\$\endgroup\$
5
\$\begingroup\$

Aheui, 8

바몽희뷸
뷷희몽반

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.

\$\endgroup\$
5
\$\begingroup\$

JavaScript

(Entered in browser console, shell or another REPL, so result is printed)

1+2
-3-
4+5

Should work for any other language with expressions, non-significant newlines and automatic printing of the result.

\$\endgroup\$
6
  • 2
    \$\begingroup\$ I like the idea behind this, but doesn't it always print 1 (mod 4)? \$\endgroup\$
    – zennehoy
    Commented Jul 10, 2014 at 15:51
  • 2
    \$\begingroup\$ A simpler variation might be '2,3\n,0,\n1,0'. \$\endgroup\$
    – Keen
    Commented Jul 10, 2014 at 17:43
  • \$\begingroup\$ It does give different results when rotated. @Cory, That's clever! I'm too used to working in a language without the comma operator. \$\endgroup\$
    – lrn
    Commented Jul 10, 2014 at 20:01
  • \$\begingroup\$ @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... \$\endgroup\$
    – zennehoy
    Commented Jul 11, 2014 at 13:48
  • \$\begingroup\$ The second note of the original poster allows any values for the rotated versions, as long as they are distinct. \$\endgroup\$
    – lrn
    Commented Jul 11, 2014 at 23:58
5
\$\begingroup\$

Matlab/Octave - 144 100

Golfed: 10 x 10 = 100

....d.....
....i.....
....s.....
... p2 ...
disp  1...
...3  psid
... 4p ...
.....s....
.....i....
.....d....

Alternative solution: 15 x 15 = 225

.......d.......
.......i.......
.......s.......
...    p    ...
...         ...
...    4    ...
...    .    ...
disp 1...3 psid
...    .    ...
...    2    ...
...         ...
...    p    ...
.......s.......
.......i.......
.......d.......
\$\endgroup\$
2
  • \$\begingroup\$ Can the first solution be reduced to size 8x8, if vrtical and horizontal operators 'disp' share letter 'p'? ...d.... ...i.... ...s2... disp 1.. ..3 psid ...4s... ....i... ....d... \$\endgroup\$
    – AMK
    Commented Sep 1, 2014 at 15:26
  • \$\begingroup\$ @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. \$\endgroup\$
    – Falko
    Commented Sep 1, 2014 at 15:53
5
\$\begingroup\$

Perl 5x7 (35)

1+p+t+1
-print+
1+i+i+1
+tnirp+
1+t+p+1

A bit late to the party. The solitary - determines which number is printed.

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

Befunge, 12 (6x2)

I Managed to come up with a slight improvement on the existing Befunge answer by making the most of Befunge's two-dimensional nature and having the code path run vertically in two of the orientations.

[email protected]
[email protected]

Try it online: Starting 0, Rotation 1, Rotation 2, Rotation 3.

\$\endgroup\$
3
\$\begingroup\$

JavaScript, 3

2
1
4

It works... in base 7.

Base 9 version:

2
7
0

Explanation

When run interactively, e.g. from a debugging console, the value of the last statement/expression will be output.

47 = 4100 (mod 4)
4127 = 205101 (mod 4)
27 = 2102 (mod 4)
2147 = 109103 (mod 4)

Similar solutions could be found for any odd base.

\$\endgroup\$
2
  • \$\begingroup\$ This was flagged, but I don't understand the question or the answer so I had to skip it. Would be nice if downvoters left a comment explaining their downvotes. \$\endgroup\$
    – Rainbolt
    Commented Jul 11, 2014 at 1:56
  • \$\begingroup\$ @Rusher I suspect they didn't understand the base 7 bit. \$\endgroup\$
    – primo
    Commented Jul 11, 2014 at 6:19
3
\$\begingroup\$

Marbelous, 7*14 = 98

.. \/ \/ .. ..
.. 31 \\ 32 \/
\/ \\ \/ \\ \/
\/ 30 \\ 33 ..
.. .. \/ \/ ..
\$\endgroup\$
1
  • \$\begingroup\$ Are you expecting each cell to remain intact when rotated? \$\endgroup\$
    – Sparr
    Commented Jun 9, 2015 at 18:26
3
\$\begingroup\$

brainfuck, 12x10 = 120

Outputs 0, 1, 2, and 3, respectively.

++         >
++++++++++++
++++++++++++
++++++++++++
++++++++++++
      .+++++
++++++++++++
++++++ +++++
++++++ +++++
++++++ +++++

Try it online!

One turn

Two turns

Three turns

\$\endgroup\$
2
\$\begingroup\$

Argh!/Aargh! (4*4=16)

What was that about using the right tool for the job? There are no comments (in the language in general).

The entire family of programs (generated in J: ((|.@:|:) ^: (i. 4)) >'hpqh';'q01p';'p32q';'hqph' or ((|.@:|:) ^: (i. 4)) 4 4 $ 'hpqhq01pp32qhqph')

hpqh
q01p
p32q
hqph

rotated once:

hpqh
q12p
p03q
hqph

rotated twice:

hpqh
q23p
p10q
hqph

rotated three times:

hpqh
q30p
p21q
hqph

To explain this, it might be best to look at an "indented" version (That also works in all rotations):

hhpq h
  0  h
q   1p
p3   q
h  2  
h qphh

This version shows that the program consists of 4 separate parts, one for each individual rotation.

  • h - set control flow left

  • p - print item in data/code raster below it

  • q - quit the program

\$\endgroup\$
2
\$\begingroup\$

Floater - 9×5=45

enter image description here

Prints 4, 1, 2, or 3 to console.

Note that 'Black' is a valid instruction (NOP), and is syntactic. Without it, it can't find the starting position. Thus, all positions in the rectangle are occupied.

\$\endgroup\$
2
\$\begingroup\$

Triangular, 2x3 => 6

%%3
i%i

Try it online!

A "rectangular triangular program" - we certainly do live in a society.

The base version here prints the top value of the stack (initially 0), then the other two prints are missed due to triangular's execution order.

Rotation 1

i%
%%
i3

Increments the top value to 1, prints, misses the other two print commands.

Rotation 2

i%i
3%%

Increments twice, hits the last print command.

Rotation 3

3i
%%
%i

Pushes 3 onto the stack, hits a print statement, misses the other two.

\$\endgroup\$
1
  • \$\begingroup\$ Nice, except the rotation is supposed to be Counter clock-wise. \$\endgroup\$
    – roblogic
    Commented Feb 4, 2023 at 10:13
2
\$\begingroup\$

Pip, 2x2 = 4

tv
oi

Try it online!

All four letters are preinitialized variables; whichever one comes last in the program is autoprinted.

  • No rotation: i = 0
  • One rotation: o = 1
  • Two rotations: t = 10
  • Three rotations: v = -1
\$\endgroup\$
1
  • \$\begingroup\$ Interesting use of the mod 90 rule. \$\endgroup\$
    – Razetime
    Commented Aug 11, 2020 at 3:57
2
\$\begingroup\$

Python, 18x18 = 324

Uses escaping and mixed quotation marks to change how each string is parsed after rotation. Triple quotes allow the literals to span multiple lines and take up less space.

"""p         '   "
   r         '   "
   i         '   "
   n'\""",)2(tnirp
'''t          '
   (          \
   3          "
   )          "
   ,          "
   '          ,
   '          )
   '          1
   \          (
   "          t"""
print(0),'''\"n
'   "         i
'   "         r
'   "         p'''

Test code (needs to be cleaned up) :

with open('./rotpy.py','r') as fh:
    l=list(fh)
    ll=max(len(x)for x in l)
    l=[x[:-1]+' '*(ll-len(x)) for x in l]

def r(l):
    return list(''.join(reversed(z)) for z in zip(*l))

for i in range(4):
    exec('\n'+'\n'.join(l))
    l=r(l)
\$\endgroup\$
1
\$\begingroup\$

Element, 2x3 = 6

1*
`2
3 

This is an improvement over the 3x3 naive solution, which has a ` in the middle with a number on each side. The 0 case, shown above, is the most interesting, since the * is used to multiply the 3 by nothing to get 0. Other than that, it's not that complicated.

If you find the space awkward, you can replace it with pretty much any other character, excluding []{}`_.

For reference, here are the other three rotations:

case 1
*2 
1`3

case 2
 3
2`
*1

case 3
3`1
 2*
\$\endgroup\$
1
\$\begingroup\$

Japt, 3 * 1 = 3 bytes

5zB

Try it online: as-is, rotated once, twice, thrice.

Outputs 0, 5, 2, 11 respectively.

The variable B holds the value 11, and Number.z(other) is floor division (everyone looked for apparently :p). For multi-line code, the last line is passed to output, which is simply a constant here.


2 * 2 = 4 bytes

2J
1T

Try it online: as-is, rotated once, twice, thrice.

Outputs 0, 21, 2, -1 respectively.

T holds 0, and J holds -1.

The trick is that, if two literals or variables are put side-by-side, a comma is inserted and the output is just the last one.

2 * 2 JS solution works in Japt too.

\$\endgroup\$
1
\$\begingroup\$

MAWP, 2x2 = 4

0:
23

-2 characters from Bubbler.

The position of the : operator determines when things are output, so we can abuse that and position the numbers around it.

Bubbler's modification pops the existing 1 from the stack, saving two bytes. Try it!

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

05AB1E, 4 (2x2) bytes

YX
3¾

To-the-point approach.

Try it online or verify all four rotations.

Explanation:

  • Y is a variable with default value \$2\$
  • X is a variable with default value \$1\$
  • ¾ is a counter variable with default value \$0\$

So in all four rotations they are all three pushed to the stack, and then output the last integer on the stack is output implicitly as result.

An alternative 2x2 is a port of the @DLosc's Pip answer, where T = \$10\$ and ® = \$-1\$:

TX
®¾

Try it online or verify all four rotations.

\$\endgroup\$
1
\$\begingroup\$

Zsh, 36 (6x6)

 <<<0 
3    <
<    <
<    <
<    1
 2<<< 

Try it online!.

Alternative, (10x10):

printf 0 p
         r
3        i
         n
f        t
t        f
n         
i        1
r         
p 2 ftnirp

Exploits the fact that printf only prints the first word, if there are no quotes.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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