The alphanumeric spiral

24

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!

Adnan

Posted 2015-12-11T21:57:28.603

Reputation: 41 965

3Related . This kind of feels like a dupe but I can't find one :P – FryAmTheEggman – 2015-12-11T22:07:37.137

2More closely related. – Peter Taylor – 2015-12-11T22:52:48.563

2are the spaces necessary? – Maltysen – 2015-12-12T00:27:57.697

@Maltysen Yes, they are required. – Adnan – 2015-12-12T08:51:59.643

Answers

5

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.

Martin Ender

Posted 2015-12-11T21:57:28.603

Reputation: 184 808

10

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

Evil Sheep

Posted 2015-12-11T21:57:28.603

Reputation: 121

Strangely, when I try to run the program online, it balks and complains that it cannot find variable f. – DavidC – 2015-12-12T02:41:19.867

@DavidCarraher I recommend trying on Firefox, that's the browser the interpreter seems to work best in – Downgoat – 2015-12-12T04:14:23.320

Yes, it works on Firefox. Nice. – DavidC – 2015-12-12T04:52:25.640

Ooh, nice! Using a few Unicode shortcuts, you can cut the byte count by five: "FGHIJK EXYZ0L DW781M CV692N BU543O ATSRQP"¸zU ®¬¸} · – ETHproductions – 2015-12-12T21:38:29.230

@ETHproductions didn't see those before, thanks! – Evil Sheep – 2015-12-12T22:14:07.753

3

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"}}

DavidC

Posted 2015-12-11T21:57:28.603

Reputation: 24 524

Infix form can be used in some areas. – LegionMammal978 – 2015-12-12T12:48:50.743

3

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

Tom Carpenter

Posted 2015-12-11T21:57:28.603

Reputation: 3 990

2

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>

edc65

Posted 2015-12-11T21:57:28.603

Reputation: 31 086

You can put newline characters themselves inside template strings \<newline>`` is one byte shorter than \\n``. – user81655 – 2015-12-14T14:03:22.787

@user81655 thx, even 1 byte counts, but it's so awfully long anyway – edc65 – 2015-12-14T16:17:22.017

1

Pyth - 60 bytes

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

jjL\ @[_CKc6"ABCDEFTUVWXGS567YHR498ZIQ3210JPONMLK"KC_K__MK)Q

Test Suite.

Maltysen

Posted 2015-12-11T21:57:28.603

Reputation: 25 023

2+<G6" instead of "ABCDEF saves 2 bytes. – PurkkaKoodari – 2015-12-12T16:09:21.397

1

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

Vasu Adari

Posted 2015-12-11T21:57:28.603

Reputation: 941

1

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))

Gabriele D'Antona

Posted 2015-12-11T21:57:28.603

Reputation: 1 336