Write a complete program that rotates a rectangular block of ASCII characters 90 degrees clockwise. When the program itself is rotated 90 degrees clockwise, it rotates a block of ASCII characters 90 counterclockwise.


  • You many not use built-ins that rotate or transpose matrices. For example, in MATLAB/Octave rot90 and the transpose operator ' are not allowed.
  • You must write a complete program that uses STDIN and STDOUT or the closest equivalent.
  • Your program must be rectangular and assume the input is also rectangular.
  • The input and output are a newline separated strings and will not have trailing newlines.

When run with its source code as input, your program must rotate itself 90 degrees clockwise. The output must be a second program in the same language that rotates its input 90 degrees counterclockwise. When the rotated program is given its source code as input, it should output the source code of the original program.

Note: Both programs must work for any input, not just their own source code, so a one character quine is not allowed.


Say the following is a valid program that rotates its input 90 degrees in a hypothetical language ExampleLang.

^f a2% 3
lk (^_^&
D8 $4  /

When run with itself as input, it outputs another valid program that rotates its input counterclockwise:

D l^
8 kf
$ (a
4 ^2

This second program, when given to itself as input, outputs the original program. Note that the blank line should have four spaces and there is a trailing space on the second to last line that cannot be rendered in markdown. To clarify:

$ examplelang program < program > rotProg
$ examplelang rotProg < rotProg > program1
$ diff -s program program1
Files program and program1 are identical

Shortest program wins. Standard loopholes are banned.


CJam, 26 25 21 bytes


Thanks to @MartinBüttner for golfing off 4 bytes!

Try it online in the CJam interpreter: original program | rotated program

This is the rotated program:



We can rotate the input a quarter turn clockwise by splitting it at linefeeds, reversing the order of the resulting rows, transposing rows with columns, and finally joining the rows, separated by linefeeds.

Likewise, we can rotate counterclockwise by transposing first, then reversing the rows.

Since the transposition built-in z is forbidden, we can use :.+ (reduce by vectorized character or string-character concatenation) to achieve the same effect.

:.+ is the only part of the source code that cannot be broken up. We push the strings "W%" and ":.+", conditionally reverse them if the second string contains a linefeed, concatenate, remove all linefeeds, and evaluate the result.


W     e# Push -1.
qN/   e# Read all input at split it at linefeeds.
":.+" e# Push a string that, when evaluated, transposes rows and columns.
      e# As explained in the previous section, this does NOT use a built-in
      e# for matrix transposition.
"\%"  e# Push a string that, when evaluated, reverses the rows.
(~    e# Shift out the first character and evaluate it.
      e# For the original code, this evaluates '\', swapping the strings on
      e# the stack. For the rotated code, this evaluates `\n', doing nothing.
+N-   e# Concatenate and remove linefeeds.
      e# The stack now contains:   -1 input "%:.+"   or   -1 input ":.+\%"
~     e# Evaluate the string on top of the stack.
N*    e# Join the resulting array, separating by linefeeds.
  • \$\begingroup\$ How is this so short? Seriously though, why can't :.+ be broken over several lines? \$\endgroup\$ Nov 11 '15 at 12:28
  • 1
    \$\begingroup\$ @intrepidcoder For syntactical reasons. The meaning of both : and . depends on the character after them, and linefeeds aren't valid after either of those (and even if they were, that would change the meaning of the program). \$\endgroup\$ Nov 11 '15 at 13:30

C (gcc), 1420 1399 463 bytes

Ah... the joy of strings of undetermined length!

Assumes sizeof(char*) == sizeof(int) and sizeof(char**) <= 16.

The new approach

char**L,*r;n,i//j=>]l n}q(( 
,j,q;R(l){for(//,l)l, +;;rr 
r=l=0;(j=     //i=)[r +))oa 
getchar())>10;//,r(r( *l(fh}
l+2);l&&R((L= //roh=l(,,r"u)
(R();i<q;i++, //*lglr&o1)t<]
(L[j][i]));}  //a;(;))a[i;0j

Try it online!

Output of the above

The solution was embarrassingly easy in the end. You make one program A that rotates things clockwise, and one program B that rotates counter-clockwise:





Make a rectangle of reasonable proportions and confine A to that, and put guards of comments around it:

r=l=0;(j=     //
l+2);l&&R((L= //
(R();i<q;i++, //
(L[j][i]));}  //

Confine program B to a square that is the same width as the one for A plus two (for the extra lines of comments at the bottom edge), rotate it CCW and slap it to the right of program A and you get the solution above.

The old approach

 /*                                       r                               c                                                         c                                                  r               
r                                         a                               o                         n                               o                          s                       a               
a                          r              h                               l                         i       r                       l             r      -     t        r  =    +      h         q     
h                          o              c        0     +                l                         a       o             +         l       6     o      -     u    "   o  j<   +      c  */           
 ///                        //          //e////     /     /             //e////                      ///     //            /      //e////    /     //  //  //// ///  /   // ;/   /// //u////      /    
 ///                        //          //g////     /     /             //r////                      ///     //            /      //r////    /     //  //  //// ///  /   // 0/   /// //p////      /    

Try it online!

Output of the above


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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