24
\$\begingroup\$

The task is very simple, when given an input, output one of the following spirals:

Input = 1 gives a spiral with the letter A beginning in the top left corner:

A B C D E F
T U V W X G
S 5 6 7 Y H
R 4 9 8 Z I
Q 3 2 1 0 J
P O N M L K

Input = 2 gives a spiral with the letter A beginning in the top right corner:

P Q R S T A
O 3 4 5 U B
N 2 9 6 V C
M 1 8 7 W D
L 0 Z Y X E
K J I H G F

Input = 3 gives a spiral with the letter A beginning in the bottom right corner:

K L M N O P
J 0 1 2 3 Q
I Z 8 9 4 R
H Y 7 6 5 S
G X W V U T
F E D C B A

Input = 4 gives a spiral with the letter A beginning in the bottom left corner:

F G H I J K
E X Y Z 0 L
D W 7 8 1 M
C V 6 9 2 N
B U 5 4 3 O
A T S R Q P

As you can see, the spiral always goes clockwise and moves from the outside to the inside.

The rules are simple:

  • You need to provide a full program using STDIN and STDOUT, or the nearest equivalent if not possible.
  • Given an input (1, 2, 3, 4), output the related spiral.
  • Trailing whitespaces are allowed
  • Leading whitespaces are allowed when used consistently
  • You need to use uppercase letter for the output, lowercase letters are not allowed.
  • This is , so the program with the least amount of bytes wins!
\$\endgroup\$
4
  • 3
    \$\begingroup\$ Related . This kind of feels like a dupe but I can't find one :P \$\endgroup\$ Dec 11, 2015 at 22:07
  • 2
    \$\begingroup\$ More closely related. \$\endgroup\$ Dec 11, 2015 at 22:52
  • 2
    \$\begingroup\$ are the spaces necessary? \$\endgroup\$
    – Maltysen
    Dec 12, 2015 at 0:27
  • \$\begingroup\$ @Maltysen Yes, they are required. \$\endgroup\$
    – Adnan
    Dec 12, 2015 at 8:51

8 Answers 8

10
\$\begingroup\$

Japt, 53 bytes 58 59 60

Saved 5 bytes thanks to @ETHproductions

"FGHIJK EXYZ0L DW781M CV692N BU543O ATSRQP"¸zU ®¬¸} ·

This uses the rotate command which I never thought would be so useful

Explanation && Ungolfed

"FGHIJK EXYZ0L DW781M CV692N BU543O ATSRQP"qS zU m_q qS} qR

             // Implicit: U = input
"FGH...SRQP" // String, " " represent newlines
qS           // Split   " "
zU           // Rotate
m_           // Loop the lines
  q qS}      // Insert spaces
qR           // Join by newlines

Try it online

\$\endgroup\$
5
  • \$\begingroup\$ Strangely, when I try to run the program online, it balks and complains that it cannot find variable f. \$\endgroup\$
    – DavidC
    Dec 12, 2015 at 2:41
  • \$\begingroup\$ @DavidCarraher I recommend trying on Firefox, that's the browser the interpreter seems to work best in \$\endgroup\$
    – Downgoat
    Dec 12, 2015 at 4:14
  • \$\begingroup\$ Yes, it works on Firefox. Nice. \$\endgroup\$
    – DavidC
    Dec 12, 2015 at 4:52
  • \$\begingroup\$ Ooh, nice! Using a few Unicode shortcuts, you can cut the byte count by five: "FGHIJK EXYZ0L DW781M CV692N BU543O ATSRQP"¸zU ®¬¸} · \$\endgroup\$ Dec 12, 2015 at 21:38
  • \$\begingroup\$ @ETHproductions didn't see those before, thanks! \$\endgroup\$
    – Evil Sheep
    Dec 12, 2015 at 22:14
5
\$\begingroup\$

CJam, 45 43 42 bytes

'[,65>A,+W%(s{W%z_,@s/(@.+}A*ri{W%z}*Sf*N*

Test it here.

Explanation

'[,65>  e# Push the upper-case alphabet.
A,+     e# Append the digits.
W%      e# Reverse everything.
(s      e# Pull off the 9 and turn it into a string.
{       e# Repeat this 10 times to roll the string up in a spiral...
  W%z   e#   Rotate the existing grid clockwise.
  _,    e#   Duplicate grid so far and get the number of rows.
  @s    e#   Pull up the list of characters and flatten it into one string.
  /     e#   Split the string into chunks of the size of the number of rows.
  (     e#   Pull off the first chunk.
  @.+   e#   Pull up the grid so far and prepend the chunk as a new column.
}A*     e# We now have the spiral as desired, with the A in the bottom left corner.
ri      e# Read input and convert to integer.
{       e# Repeat this code that often..
  W%z   e#   Rotate the spiral clockwise.
}*
Sf*     e# Join each line with spaces.
N*      e# Join the lines with linefeeds.
\$\endgroup\$
3
\$\begingroup\$

Mathematica 156 bytes

Converts initial string of letters, "ABCDEFTUVWXGS567YHR498ZIQ3210JPONMLK", into an array. Nest applies f to that array n-1times, where n is the input number. f works by Transpose-ing the array followed by Reverseapplied to each row. g converts the final array into a string.

s=StringJoin;r=Riffle;f=Nest[Reverse/@Transpose@#&,Partition[Characters@"ABCDEFTUVWXGS567YHR498ZIQ3210JPONMLK",6],#-1]&;
g@n_:=s@r[s/@(r[#," "]&/@f[n]),"\n"]

Example

g[4]

output


If the output could be given as an array, the function g would be unnecessary.

f[4]

{{"F", "G", "H", "I", "J", "K"}, {"E", "X", "Y", "Z", "0", "L"}, {"D", "W", "7", "8", "1", "M"}, {"C", "V", "6", "9", "2", "N"}, {"B", "U", "5", "4", "3", "O"}, {"A", "T", "S", "R", "Q", "P"}}

\$\endgroup\$
1
  • \$\begingroup\$ Infix form can be used in some areas. \$\endgroup\$ Dec 12, 2015 at 12:48
3
\$\begingroup\$

MATLAB, 61 89 bytes

b=[65:90 48:57];n=zeros(12,6);n(2:2:end)=rot90(b(37-spiral(6)),input('')-2);disp([n' ''])

I'll see if I can get it down a bit. Not sure though.

This creates an array of all letters from A to Z followed by 0 to 9, then takes a spiral and uses that to arrange the data in the correct order. The array is then rotated by the amount the user specifies and then printed out.

The output consistently uses leading spaces as allowed by the question (in fact at no extra byte cost, it could do trailing spaces instead). Here is an example:

 F G H I J K
 E X Y Z 0 L
 D W 7 8 1 M
 C V 6 9 2 N
 B U 5 4 3 O
 A T S R Q P

As I saw that spaces are required, this original code (for 61) is not valid because it doesn't add a space between each character. But I will add it here for reference.

b=['A':'Z' '0':'9'];disp(rot90(b(37-spiral(6)'),6-input('')))

and produces:

ABCDEF
TUVWXG
S567YH
R498ZI
Q3210J
PONMLK
\$\endgroup\$
2
\$\begingroup\$

JavaScript ES6, 165 172

Simple rotation, starting from a hardcoded string

Note 1 byte saved thx @user81655

p=prompt();alert("ABCDEF TUVWXG S567YH R498ZI Q3210J PONMLK".split` `.map((r,y,a)=>[...r].map((c,x)=>p<2?c:a[p<3?5-x:p<4?5-y:x][p<3?y:p<4?5-x:5-y]).join` `).join`
`)

Test snippet:

// Test: redefine alert to write inside the snippet
alert=x=>P.innerHTML=x

p=prompt();
alert(
  "ABCDEF TUVWXG S567YH R498ZI Q3210J PONMLK"
  .split` `
  .map(
    (r,y,a)=>
    [...r].map(
      (c,x)=>p<2?c:
      a
       [p<3?5-x:p<4?5-y:x]
       [p<3?y:p<4?5-x:5-y]
    ).join` `
  ).join`\n`
)
<pre id=P></pre>

\$\endgroup\$
2
  • \$\begingroup\$ You can put newline characters themselves inside template strings `<newline>` is one byte shorter than `\n`. \$\endgroup\$
    – user81655
    Dec 14, 2015 at 14:03
  • \$\begingroup\$ @user81655 thx, even 1 byte counts, but it's so awfully long anyway \$\endgroup\$
    – edc65
    Dec 14, 2015 at 16:17
1
\$\begingroup\$

Pyth - 60 bytes

Hardcodes the string and uses matrix operations to get all the options.

jjL\ @[_CKc6"ABCDEFTUVWXGS567YHR498ZIQ3210JPONMLK"KC_K__MK)Q

Test Suite.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ +<G6" instead of "ABCDEF saves 2 bytes. \$\endgroup\$ Dec 12, 2015 at 16:09
1
\$\begingroup\$

Ruby, 173 bytes

->i{_,r,o=->s{s.map{|i|i*' '}},->s{s.transpose.map{|i|i.reverse}},%W(ABCDEF TUVWXG S567YH R498ZI Q3210J PONMLK).map(&:chars);puts i<2?_[o]:i<3?_[t=r[o]]:i<4?_[r[t]]:_[r[r[t]]]}

Ungolfed:

-> i {
  _ = -> s { s.map{|i| i*' ' } }
  r = -> s { s.transpose.map{|i| i.reverse } }
  o = %W(ABCDEF TUVWXG S567YH R498ZI Q3210J PONMLK).map(&:chars)
  puts i<2?_[o]:i<3?_[t=r[o]]:i<4?_[r[t]]:_[r[r[t]]]
}

Usage:

->i{_,r,o=->s{s.map{|i|i*' '}},->s{s.transpose.map{|i|i.reverse}},%W(ABCDEF TUVWXG S567YH R498ZI Q3210J PONMLK).map(&:chars);puts i<2?_[o]:i<3?_[t=r[o]]:i<4?_[r[t]]:_[r[r[t]]]}[4]
F G H I J K
E X Y Z 0 L
D W 7 8 1 M
C V 6 9 2 N
B U 5 4 3 O
A T S R Q P
\$\endgroup\$
1
\$\begingroup\$

Python, 152 bytes

s=[r for r in "ABCDEF TUVWXG S567YH R498ZI Q3210J PONMLK".split(" ")]
for i in range(1,int(input())):s=zip(*list(s)[::-1])
for x in s:print(" ".join(x))
\$\endgroup\$

Your Answer

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

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