Espirais rotativas


12

Dado um quadrado de texto representando uma espiral de caracteres, gire-o!

A espiral começa no centro e se move no sentido anti - horário até a borda externa, começando à esquerda do centro:

987
216
345

Isso se traduz na string 123456789. A rotação é feita para a esquerda ; portanto, se você girar uma posição, ela será 234567891. Isso é representado por:

198
327
456

Entrada

A entrada consiste na espiral e a distância para girá-la.

A distância sempre será um número inteiro positivo ou zero e pode ser limitada no limite do tipo de dados do seu idioma.

A espiral deve ser tomada como uma string, com um delimitador de linha de sua escolha (incluindo nenhum delimitador). Sempre será um quadrado, sem incluir delimitadores, e terá um comprimento lateral ímpar.

Suponha que todos os caracteres sejam alfanuméricos [A-Za-z0-9].

Resultado

A saída é a espiral girada. Deve ser um quadrado em várias linhas (impressas ou retornadas).

Exemplos

Entrada

3
tne
atd
bin

Resultado

bat
nit
den

Entrada

18
efilr
naepo
umshf
tootr
butte

Resultado

rettu
omseb
oofft
trliu
hpean

Este é o código de golfe, com pontuação contada em bytes, como de costume.

Respostas:


6

CJam, 45 44 bytes

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

Teste aqui.

Explicação

A solução preguiçosa: desembrulhe a espiral, use a rotação de matriz integrada do CJam, role a espiral novamente.

]]       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.
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.