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 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)
- Do this with the smallest N. (down to a limit of N = 3)
- 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
0would work. If desired you may unroll to the left rather than the right. So e.g.
MyP rog rambecomes
Pg yo Mrramand 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)
-
\$\begingroup\$ Before I read the "challenge" paragraph, I was expecting a challenge to write a program that outputs itself unrolled \$\endgroup\$John Dvorak– John Dvorak2014年10月18日 07:04:14 +00:00Commented Oct 18, 2014 at 7:04
-
1\$\begingroup\$ why does N have to be odd? \$\endgroup\$John Dvorak– John Dvorak2014年10月18日 07:05:36 +00:00Commented Oct 18, 2014 at 7:05
-
1\$\begingroup\$ @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. \$\endgroup\$Calvin's Hobbies– Calvin's Hobbies2014年10月18日 07:11:59 +00:00Commented Oct 18, 2014 at 7:11
-
8\$\begingroup\$ Just 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. \$\endgroup\$Falko– Falko2014年10月18日 10:27:54 +00:00Commented Oct 18, 2014 at 10:27
-
5\$\begingroup\$ Do you have a magic book with infinite great challenge ideas? How else do you keep coming up with such interesting challenges? \$\endgroup\$Justin– Justin2014年10月18日 16:55:46 +00:00Commented Oct 18, 2014 at 16:55
5 Answers 5
Golfscript, N <- [5,7..]
. .
. .
..
. .#
],9\-
Fully unrolled:
],9\-# . . . . . ...
Explanation:
.(multiple times) - duplicate the input]- collect the stack into a single array,- take its length9\-- 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.
-
\$\begingroup\$ Well, unless someone finds N = 3, this will be the winning answer in both categories. \$\endgroup\$Calvin's Hobbies– Calvin's Hobbies2014年10月18日 17:36:08 +00:00Commented Oct 18, 2014 at 17:36
-
3\$\begingroup\$ @Calvin'sHobbies should I be a total dick and post a left-unrolling solution as well? :-) \$\endgroup\$John Dvorak– John Dvorak2014年10月18日 17:39:14 +00:00Commented Oct 18, 2014 at 17:39
-
\$\begingroup\$ Why not. Another answer seems unlikely otherwise :P \$\endgroup\$Calvin's Hobbies– Calvin's Hobbies2014年10月18日 17:45:34 +00:00Commented Oct 18, 2014 at 17:45
-
1\$\begingroup\$ Why not go for one which can unroll in both directions? :) \$\endgroup\$Beta Decay– Beta Decay2014年10月18日 19:59:52 +00:00Commented Oct 18, 2014 at 19:59
-
\$\begingroup\$ @BetaDecay hmm... :-) \$\endgroup\$John Dvorak– John Dvorak2014年10月18日 20:19:29 +00:00Commented Oct 18, 2014 at 20:19
GolfScript, N = 4
This one right rolls as original spec.
.. .
...#
.#.~
],8-
Here are the unrolls:
...
#..
..
],8-~#.
.#.
...
],8-~#. ..
..
.#
],8-~#. ....
..
],8-~#. ....#.
.
],8-~#. ....#..
],8-~#. ....#...
-
\$\begingroup\$ How did you think of this arrangement? \$\endgroup\$proud haskeller– proud haskeller2014年10月18日 22:31:51 +00:00Commented Oct 18, 2014 at 22:31
-
3\$\begingroup\$ @proudhaskeller It's better if you don't know ... \$\endgroup\$Optimizer– Optimizer2014年10月18日 22:32:47 +00:00Commented Oct 18, 2014 at 22:32
-
8\$\begingroup\$ Did you brute-search a solution? \$\endgroup\$proud haskeller– proud haskeller2014年10月18日 22:33:30 +00:00Commented Oct 18, 2014 at 22:33
-
\$\begingroup\$ Special challenge: can you make one out of
.s and#s? \$\endgroup\$John Dvorak– John Dvorak2014年10月19日 06:29:51 +00:00Commented Oct 19, 2014 at 6:29 -
\$\begingroup\$ I like the trailing
~. Maybe I can steal it for N=3? \$\endgroup\$John Dvorak– John Dvorak2014年10月19日 06:31:11 +00:00Commented Oct 19, 2014 at 6:31
APL, N = 3
201
340
5|0
Unrolled:
32
40
5|001
43
5|00102
4
5|001023
5|0010234
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∞⍬
≡ returns the depth (not to be confused with the dimension or length) of an array:
0is not an array. So the depth is 0.0∞is an array with two items0and∞(infinity). It has depth 1.0∞⍬has another item⍬, which is an empty array with depth 1. So0∞⍬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.
-
\$\begingroup\$ Any explanations? \$\endgroup\$proud haskeller– proud haskeller2014年10月20日 06:48:49 +00:00Commented Oct 20, 2014 at 6:48
-
\$\begingroup\$ @proudhaskeller Edited. \$\endgroup\$jimmy23013– jimmy230132014年10月20日 07:42:40 +00:00Commented Oct 20, 2014 at 7:42
-
\$\begingroup\$ You can ideally use the same depth logic for any N. Thanks to APL \$\endgroup\$Optimizer– Optimizer2014年10月20日 08:52:01 +00:00Commented Oct 20, 2014 at 8:52
-
\$\begingroup\$ @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. \$\endgroup\$jimmy23013– jimmy230132014年10月20日 09:02:19 +00:00Commented Oct 20, 2014 at 9:02 -
\$\begingroup\$ I meant like: ` ⍬ \n⍬⍬0\n≡ ∞` (Not exactly that, but you get the idea) \$\endgroup\$Optimizer– Optimizer2014年10月20日 09:04:33 +00:00Commented Oct 20, 2014 at 9:04
Perl 5 + -p0513, 155 bytes, N=12
Not particularly competitive, but it was fun trying to get to this point. I tried with 1s originally, but struggled to make it work with 1s only separated by newlines. Also investigated using just list length (N=6) but had issues without using length and join.
# #
# #;
# # w
# # q
# # ,
## '
# # '
# # n
# # i
# # o
# #j
$\+=length
Vyxal, N=3
› ›
››
0
Unrolled:
››
›
0 ›
››
0 › ›
›
0 › ››
0 › ›››
› increments a number, so the rolled version prints 0, the once-unrolled version prints 0+1=1, the twice-unrolled version prints 0+1+1=2 and so on. This could probably be extended to arbitrary-sized grids.
-
\$\begingroup\$
Nmust be at least 3 \$\endgroup\$Shaggy– Shaggy2024年08月21日 09:08:37 +00:00Commented Aug 21, 2024 at 9:08 -
\$\begingroup\$ @Shaggy Ah. The APL answer has a solution with n=2. Will fix \$\endgroup\$emanresu A– emanresu A2024年08月21日 09:26:23 +00:00Commented Aug 21, 2024 at 9:26
Explore related questions
See similar questions with these tags.