17
\$\begingroup\$

Given a square of text representing a spiral of characters, rotate it!

The spiral starts at the center and moves counterclockwise to the outer edge, starting to the left of center:

987
216
345

This translates to the string 123456789. Rotation is done to the left, so if you rotate it one position, it will be 234567891. This is represented by:

198
327
456

Input

Input consists of the spiral and the distance to rotate it.

Distance will always be a positive integer or zero, and can be capped at your language's data type limit.

The spiral should be taken as a string, with a line delimiter of your choice (including no delimiter). It will always be a square, not including delimiters, and have an odd side length.

Assume all characters are alphanumeric [A-Za-z0-9].

Output

Output is the rotated spiral. It should be a square on multiple lines (whether printed or returned).

Examples

Input

3
tne
atd
bin

Output

bat
nit
den

Input

18
efilr
naepo
umshf
tootr
butte

Output

rettu
omseb
oofft
trliu
hpean

This is code golf, with score counted in bytes as usual.

\$\endgroup\$

3 Answers 3

8
\$\begingroup\$

CJam, 45 44 bytes

]]l~LqN/{(W%@+\zW%}h;m<{1$,/(W%a@W%z+\s}h;N*

Test it here.

Explanation

The lazy solution: unwrap the spiral, use CJam's built-in array rotation, roll the spiral up again.

]]       e# Push [""]. We'll later use this to roll up the spiral.
l~       e# Read the integer and evaluate it.
L        e# Push an empty string: we'll unroll the input into this.
qN/      e# Read the spiral and split it into lines.
{        e# While the top of the stack is truthy...
  (W%    e#   Pull the first line off the spiral and reverse it.
  @+     e#   Pull up the string we've built so far and prepend the reversed line.
  \zW%   e#   Swap with the remaining spiral, and rotate the spiral.
}h       e# This terminates when the centre character has been added to the string and
         e# the spiral becomes an empty array.
;        e# Discard the empty array.
         e# Note that we've unrolled the spiral from outside in, but we've also built up
         e# the string in reverse, which gives us the string from inside out.
m<       e# Rotate to the left by the given number of characters.
{        e# While the top of the stack is truthy...
  1$,    e#   Copy the spiral so far and get the number of lines.
  /      e#   Split the string into chunks of that size.
  (W%a   e#   Pull off the first chunk, reverse it and wrap it in an array.
  @zW%   e#   Pull up the spiral so far, rotate it.
  +      e#   Prepend the chunk to the spiral as a line.
  \s     e#   Swap with the other chunks and flatten them into a string again.
}h       e# This terminates when the string has been used up completely.
;        e# Discard the empty string.
N*       e# Join the lines with linefeed characters.
\$\endgroup\$
1
\$\begingroup\$

Haskell, 188 bytes

import Data.List;r=reverse;t=transpose;f=take;g=drop
u[[c]]=[c];u(a:b)=a++u(r.t$b)
w k[]=k;w[](a:b)=w[[a]]b;w k l=w((r.f h$l):(t.r$k))$g h l where h=length k
s n=w[].(\s->g n s++f n s).r.u

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python3, 336 bytes:

R=range
def S(N):
 x,y=N//2,N//2;r=[(x,y)]
 for i in R(2,N,2):
  y-=1;F=0;r+=[(x,y)]
  for X,Y in[(1,0),(0,1),(-1,0),(0,-1)]:
   for _ in R(i):
    if F:x+=X;y+=Y;r+=[(x,y)]
    F=1
 return r
def f(n,s):
 s=[[*i]for i in s.split('\n')];T=[s[x][y]for x,y in S(len(s[0]))];T=T[n:]+T[:n]
 for x,y in S(len(s[0])):s[x][y]=T.pop(0)
 return s

Try it online!

\$\endgroup\$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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