Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The code on this site is rapidly being depleted. We need to invest in renewable strings. So you must write a program that takes a string and converts it into a windmill.

The Challenge

Let's take a simple wind-mill string as an example. Take the string abc. The pivot is the center character, in this case b. Since the string is 3 chars long, every output will be exactly three lines tall and three characters wide. Here is your output on step 1. (Note the whitespace)


abc

To get the next step, rotate each character around the pivot clockwise. Here is step 2:

a
 b
  c

Here are steps 3-8:

 a
 b
 c
  a
 b
c

cba

c
 b
  a
 c
 b
 a
  c
 b
a

And on the ninth step, it comes around full circle to the original string:


abc

Note that the b stayed in the same spot the whole time. This is because b is the pivot character. You must write a program or function that takes a string as input and repeatedly prints out this sequence until the program is closed.

Clarifications

  • All input strings will have an odd number of characters. (So that every windmill will have a pivot)

  • To keep the challenge simple, all strings will only contain upper and lowercase alphabet characters.

  • The output must be len(input_string) characters wide and tall.

  • It doesn't matter which step of the sequence you start on, just as long as you continue rotating and looping forever.

More Test IO:

Since the post is already pretty long, here is a link to the output for "windmill":

Sidenote:

Since this is supposed to be a windmill, it would be awesome if you include some boilerplate code to animate it with a small time delay or a user input between each step. However, since some languages don't have time builtins, this is not mandatory. The competing part of your submission can just print the sequence as fast as possible.

share|improve this question
    
Related – Luis Mendo 53 mins ago

MATL, 47 bytes

' 'jntX"tGtnXyg(wGtnQ2/Y(XJDXKD`J3X!XJDK3X!XKDT

Try in online! (but kill it immediately, infinite loop)

With 1-second pause: 59 bytes

' 'jntX"tGtnXyg(wGtnQ2/Y(XJD1Y.XKD1Y.`J3X!XJD1Y.K3X!XKD1Y.T

Try it online! (again, infinite loop)

share|improve this answer

05AB1E, 88 53 bytes

Code:

¹VYg;ïU[2FX¶×DYsJ,YvNð×y¶J?}YvðX×yJ,}Yv¹gN>-ð×yJ,}YRV

Try it online!. Make sure to hit the kill button right after you run it, because it goes into an infinite loop.

share|improve this answer

Ruby, 122 bytes

->n{c=0
m=1+l=n.size
loop{s=(' '*l+$/)*l
i=[l,m,m+1,1][3-c=c+1&7]*(3.5<=>c)
l.times{|j|s[m*l/2-1+(j-l/2)*i]=n[j]}
puts s}}

Ungolfed version with sleep, in test program

The rotation isn't very convincing at full console height. But if you reduce the height to the the length of the input string, the rotation is a lot more convincing.

f=->n{
  c=0                                     #loop counter
  m=1+l=n.size                            #l=string length. m=l+1
  loop{                                   #start infinite loop
    s=(' '*l+$/)*l                        #make a string of l newline-terminated lines of l spaces. Total m characters per line.              
    i=[m-1,m,m+1,1][3-c=c+1&7]*(3.5<=>c)  #array contains positive distance between characters in string 1=horizontal, m=vertical, etc.
                                          #c=c+1&7 cycles through 0..7. Array index 3..-4 (negative indices count from end of array, so 3=-1, 0=-4 etc)
                                          #(3.5<=>c) = 1 or -1. We use to flip the sign. This is shorter than simply using 8 element array [m-1,m,m+1,1,1-m,-m,-m+1,-1]  
    l.times{|j|s[m*l/2-1+i*(j-l/2)]=n[j]} #for each character in n, write the appropriate space character in s to the character in n
    puts s                                #print s
    sleep 1                               #sleep for 1 second
  }
}

f[gets.chomp]                             #get input, remove newline, call function
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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