90° Self-Rotating Program
CJam, 26 25 21 bytes
WqN/":.+""\%"(~+N-~N*
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:
W
q
N
/
"
:
.
+
"
"
\
%
"
(
~
+
N
-
~
N
*
Idea
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.
Code
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.
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}
r[l++]=j,r[l]=//n(r,c=6=R)c;
0)r=realloc(r,//;rajoL1q()t)
l+2);l&&R((L= //roh=l(,,r"u)
realloc(L,++n*//*fc]l(Lro"p]
16))[n-1]=r,q=//,{t+aR(=f(;q
l);}main(){for//L)e+e&c]{sn[
(R();i<q;i++, //*lglr&o1)t<]
puts(""))for(j//*(=[=ll-(uj+
=n;j--;putchar//rRjrr;lnnp;+
(L[j][i]));} //a;(;))a[i;0j
////////////////hq;002e)a-=[
////////////////c,01=+r)m-jL
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:
A
char**L,*r;n,i,j,q;R(l){for(r=l=0;(j=getchar())>10;r[l++]=j,r[l]=0)r=realloc(r,l+2);l&&R((L=realloc(L,16*++n))[n-1]=r,q=l);}main(){for(R();i<q;i++,puts(""))for(j=n;j--;)putchar(L[j][i]);}
B
char**L,*r;n,i,j,q;R(l){for(r=l=0;(j=getchar())>10;r[l++]=j,r[l]=0)r=realloc(r,l+2);l&&R((L=realloc(L,16*++n))[n-1]=r,q=l);}main(){for(R();q--;puts(""))for(j=0;j<n;j++)putchar(L[j][q]);}
Make a rectangle of reasonable proportions and confine A to that, and put guards of comments around it:
char**L,*r;n,i//
,j,q;R(l){for(//
r=l=0;(j= //
getchar())>10;//
r[l++]=j,r[l]=//
0)r=realloc(r,//
l+2);l&&R((L= //
realloc(L,++n*//
16))[n-1]=r,q=//
l);}main(){for//
(R();i<q;i++, //
puts(""))for(j//
=n;j--;putchar//
(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 */
char**L,*s,*r;n,i,q;R(l,c){for(r=l=0;(c=getchar())>10;r[l++]=c,r[l]=0)r=realloc(r,l+2);q=l?l:q;l=r;}main(j){for(;s=R();L[n++]=s)L=realloc(L,16*n);for(;i<q;i++,puts(""))for(j=n;j--;)putchar(L[j][i]);}
/// // //e//// / / //e//// /// // / //e//// / // // //// /// / // ;/ /// //u//// /
/// // //g//// / / //r//// /// // / //r//// / // // //// /// / // 0/ /// //p//// /
Try it online!
Output of the above