Write a square program that outputs the number of times it has been "unrolled"

22

1

Consider a square block of text, N characters wide by N tall, for some odd integer N greater than 1.

As an example let N = 5 and the text be:

MLKJI
NWVUH
OXYTG
PQRSF
ABCDE

Notice that this is the alphabet (besides Z) spiraled around counter-clockwise from the lower left corner. It's kind of like a rolled up carpet.

Spiral Alphabet

"Unrolling" the text by one quarter turn clockwise so FGHI are on the same level as ABCDE results in:

     PONM
     QXWL
     RYVK
     STUJ
ABCDEFGHI

This unrolling can be done 7 more times until the text is a single line:

         SRQP
         TYXO
         UVWN
ABCDEFGHIJKLM

             UTS
             VYR
             WXQ
ABCDEFGHIJKLMNOP

                WVU
                XYT
ABCDEFGHIJKLMNOPQRS

                   XW
                   YV
ABCDEFGHIJKLMNOPQRSTU

                     YX
ABCDEFGHIJKLMNOPQRSTUVW

                       Y
ABCDEFGHIJKLMNOPQRSTUVWX

ABCDEFGHIJKLMNOPQRSTUVWXY

Challenge

The challenge is to write a program that is an N×N block of text that outputs the number of times it has "unrolled" by a quarter turn when it is rearranged into the unrolling patterns and run.

There are really two contests here: (hopefully it won't be too messy)

  1. Do this with the smallest N. (down to a limit of N = 3)
  2. Do this with the largest N. (no limit)

There will not be an accepted answer but the winner in each of these categories will receive at least 50 bounty rep from me. In case of ties the oldest answers win.

Example

If your code block is

MyP
rog
ram

running it as is should output 0.

Running

   rM
   oy
ramgP

should output 1.

Running

     or
ramgPyM

should output 2.

Running

       o
ramgPyMr

should output 3.

Finally, running ramgPyMro should output 4.

Details

  • The output should be printed to stdout (or the closest alternative) by itself. There is no input.
  • You may only use printable ASCII (hex codes 20 to 7E, that includes space) in your code.
  • Spaces fill the empty space in the unrolling arrangements. (Unless you're unrolling to the left.)
  • Only the arrangements from completely square to completely flat need to have valid output. No other arrangements will be run.
  • You may not read your own source.
  • You may use comments.
  • N = 1 is excluded since in many languages the program 0 would work.
  • If desired you may unroll to the left rather than the right. So e.g.

    MyP
    rog
    ram
    

    becomes

    Pg
    yo
    Mrram
    

    and so on. No extra spaces are added when rolling this way. The lines just end

(Related: Write a Rectangular Program that Outputs the Number of Times it was Rotated)

Calvin's Hobbies

Posted 2014-10-18T06:46:50.593

Reputation: 84 000

Before I read the "challenge" paragraph, I was expecting a challenge to write a program that outputs itself unrolled – John Dvorak – 2014-10-18T07:04:14.953

1why does N have to be odd? – John Dvorak – 2014-10-18T07:05:36.600

1@JanDvorak I suppose N didn't have to be odd, but it makes the spirals more standardized. It's staying that way but feel free to post an N = 2 as a comment if you find one. – Calvin's Hobbies – 2014-10-18T07:11:59.160

Do you know if N=3 is possible? – John Dvorak – 2014-10-18T08:18:20.347

8Just an idea: Unrolling the "carpet" to the right creates many lines starting with whitespace, eliminating languages like Python. If you'd allow unrolling to the left, there'd be no need for additional whitespace and Python is (theoretically) possible. – Falko – 2014-10-18T10:27:54.057

@Falko Good point. I'll allow unrolling to the left instead. – Calvin's Hobbies – 2014-10-18T15:32:22.287

5Do you have a magic book with infinite great challenge ideas? How else do you keep coming up with such interesting challenges? – Justin – 2014-10-18T16:55:46.330

@Quincunx Someday when I'm tired of PPCG I'll upload my entire tome ;) (But sadly this particular challenge is already almost totally solved by Jan.) – Calvin's Hobbies – 2014-10-18T17:39:22.033

@JanDvorak I don't know about N = 3. I'm guessing no. – Calvin's Hobbies – 2014-10-18T17:40:00.770

It's the first time I want a language (APL for instance) to have base 10 logarithm by default. 00\n⍟1 returns the result times ln(10). – jimmy23013 – 2014-10-19T09:48:21.047

Answers

27

Golfscript, N <- [5,7..]

.   .
 . . 
 ..  
.  .#
],9\-

Fully unrolled:

],9\-#  .   .  .  . . ...

Explanation:

  • . (multiple times) - duplicate the input
  • ] - collect the stack into a single array
  • , - take its length
  • 9\- - subtract it from 9
  • # - line comment

Whitespace is a NOP, but any other NOP would have worked just as well.

Fully rolled up, it uses nine copies of the input (contents ignored) as the stack; 9 - 9 = 0; it has not been unrolled.

Each unroll hides one more dot (duplicate) behind the comment, shrinking the stack once, incrementing the output.

Fully unrolled, it uses only the input (contents ignored) as the stack; 9 - 1 = 8; it has been unrolled 8 times.

The same approach works for any N > 4: Change 9 to the appropriate value of 2*N+1, then extend the pattern of dots (duplicate) using the same spiral pattern that ensures exactly one dot gets unrolled during each unroll.

John Dvorak

Posted 2014-10-18T06:46:50.593

Reputation: 9 048

Well, unless someone finds N = 3, this will be the winning answer in both categories. – Calvin's Hobbies – 2014-10-18T17:36:08.187

3@Calvin'sHobbies should I be a total dick and post a left-unrolling solution as well? :-) – John Dvorak – 2014-10-18T17:39:14.647

Why not. Another answer seems unlikely otherwise :P – Calvin's Hobbies – 2014-10-18T17:45:34.057

1Why not go for one which can unroll in both directions? :) – Beta Decay – 2014-10-18T19:59:52.870

@BetaDecay hmm... :-) – John Dvorak – 2014-10-18T20:19:29.570

@Calvin'sHobbies So, I am not certain about how golfscript works with integer overflow, but I would assume this will actually explode around 2^32 (or sooner on a stack overflow?). If I'm right, would something that works for a larger value beat this? – FryAmTheEggman – 2014-10-19T06:22:49.853

@FryAmTheEggman Golfscript is based on Ruby, and Ruby has an arbitrary-sized integers. What are you in, Java? Moreover, this program doesn't use it anyways. As for stack overflow - I'm pretty sure your IDE will run out of memory (N=64ki yields a 4GiB source file) far sooner than my script (N=64ki = 256ki frames, but GFS stack actually lies on the heap ;-)) – John Dvorak – 2014-10-19T06:28:42.500

@FryAmTheEggman Even still, I wouldn't be a stickler about that. And where would you post such a huge spiral? ;) – Calvin's Hobbies – 2014-10-19T06:30:55.877

@Calvin'sHobbies Well, presumably I would post a program that writes the program... :) I was mostly asking to see if it was worth trying, because I think python might actually be able to beat 64ki. Although I actually have to get it to work first :p – FryAmTheEggman – 2014-10-19T16:14:29.983

13

GolfScript, N = 4

This one right rolls as original spec.

.. . 
...# 
.#.~
],8-

Here are the unrolls:

    ...
    #..
    ..
],8-~#.

       .#.
       ...
],8-~#. ..

          ..
          .#
],8-~#. ....

            ..
],8-~#. ....#.

              .
],8-~#. ....#..

],8-~#. ....#...

Try it here

Optimizer

Posted 2014-10-18T06:46:50.593

Reputation: 25 836

How did you think of this arrangement? – proud haskeller – 2014-10-18T22:31:51.743

3@proudhaskeller It's better if you don't know ... – Optimizer – 2014-10-18T22:32:47.710

8Did you brute-search a solution? – proud haskeller – 2014-10-18T22:33:30.863

Special challenge: can you make one out of .s and #s? – John Dvorak – 2014-10-19T06:29:51.443

I like the trailing ~. Maybe I can steal it for N=3? – John Dvorak – 2014-10-19T06:31:11.157

Note that I didn't look for an N=4 solution because the spec specified that N be 4 – John Dvorak – 2014-10-19T06:32:42.283

@JanDvorak I tried a lot for N = 3 with all the techniques at hand. Feel free to look for it. – Optimizer – 2014-10-19T09:50:19.053

9

APL, N = 3

201
340
5|0

Unrolled:

   32
   40
5|001

     43
5|00102

       4
5|001023

5|0010234

Try it online.

It calculates the remainder of that number divided by 5. Only the result of last line is printed.

APL, N = 2

⍬∞
≡0

Unrolled:

  ⍬
≡0∞

≡0∞⍬

Try it online.

returns the depth (not to be confused with the dimension or length) of an array:

  • 0 is not an array. So the depth is 0.
  • 0∞ is an array with two items 0 and (infinity). It has depth 1.
  • 0∞⍬ has another item , which is an empty array with depth 1. So 0∞⍬ has depth 2.

These two programs also works in the online interpreter. I'm not sure if the later one is syntactically correct.

⍬0
≡∞


⍬¯
≡0

APL, for any N >= 4

For N = 4:

∞  ∞
 ∞∞
∞ ∞
⍴1↓∞

Fully unrolled:

⍴1↓∞  ∞  ∞ ∞ ∞∞∞

For N = 5:

∞   ∞
 ∞ ∞
 ∞∞
∞  ∞
⍴1↓ ∞

Fully unrolled:

⍴1↓ ∞   ∞   ∞  ∞  ∞ ∞ ∞∞∞

1↓ removes an item in the array. It also returns the empty array if the argument is scalar. gets the array length.

jimmy23013

Posted 2014-10-18T06:46:50.593

Reputation: 34 042

Any explanations? – proud haskeller – 2014-10-20T06:48:49.727

@proudhaskeller Edited. – jimmy23013 – 2014-10-20T07:42:40.380

You can ideally use the same depth logic for any N. Thanks to APL – Optimizer – 2014-10-20T08:52:01.177

@Optimizer It is not that easy. Things before the last line still have to be syntactically correct. So I can't use most of the functions or other punctuation characters like ()[] as they will appear at some unwanted place. – jimmy23013 – 2014-10-20T09:02:19.767

I meant like: ⍬ \n⍬⍬0\n≡ ∞ (Not exactly that, but you get the idea) – Optimizer – 2014-10-20T09:04:33.083

@Optimizer ∞0 ⍬ ⍬⍬ is the array consisting of ∞, 0 and 3 empty arrays. It also have depth 2. – jimmy23013 – 2014-10-20T09:15:58.380

I basically meant that the depth technique can be used in N = 3 case as well. – Optimizer – 2014-10-20T09:18:01.650

@Optimizer But it doesn't work. Feel free to post another answer if you can fix it. I'll try some different techniques for N >= 4. – jimmy23013 – 2014-10-20T09:22:10.050

I don't know APL – Optimizer – 2014-10-20T09:28:11.270

@Optimizer Well, I think the array length will work for N >= 4. I'll edit soon. Did you think the depth should mean length or something? – jimmy23013 – 2014-10-20T09:39:34.553