55
\$\begingroup\$

This is what we'll call a bowl of alphabet soup - a roughly circular ascii-art shape with the 26 uppercase English letters (A-Z) arranged clockwise to form the perimeter:

   XYZABC
 VW      DE
U          F
T          G
S          H
 RQ      JI
   PONMLK

Write a program that takes in a single letter character, A-Z, and outputs that same bowl of alphabet soup "rotated", so to speak, so the input letter appears where the A does in the example above and the rest of the alphabet cycles fully around clockwise.

So the output for input A would be that same original bowl of alphabet soup.

And the output for input B would be this one:

   YZABCD
 WX      EF
V          G
U          H
T          I
 SR      KJ
   QPONML

Likewise the output for H would be:

   EFGHIJ
 CD      KL
B          M
A          N
Z          O
 YX      QP
   WVUTSR

Or for Z:

   WXYZAB
 UV      CD
T          E
S          F
R          G
 QP      IH
   ONMLKJ

This needs to work for all 26 letters, A through Z.

Details:

  • You can assume the only input will be a single letter, A through Z.
  • If convenient you may use lowercase a-z for input and/or output, you can even mix and match lower and uppercase.
  • The alphabet order must cycle clockwise, not counter-clockwise.
  • You must use spaces, not something else, to indent and fill the soup bowl.
  • There may be leading or trailing newlines or spaces in the output as long as the soup bowl is arranged properly.
  • Note that the bowl shape is 12 characters wide by 7 tall to make it appear roughly circular as text. Your bowls need to be the same shape.

This is code golf so the shortest code wins!

\$\endgroup\$
2
  • 14
    \$\begingroup\$ Great challenge! It initially seems easy, but it's not \$\endgroup\$
    – Luis Mendo
    Mar 19, 2019 at 21:56
  • 1
    \$\begingroup\$ It's quite late, but you won two of the Best of CGCC 2019 awards, this one for Trickiest Challenge, and another for Best first-time Challenge Writer! We want to award bounties for you, but they can only be awarded on answers. Can you write one or two answers so that you can receive the bounty? \$\endgroup\$
    – Bubbler
    Jan 19, 2021 at 8:09

24 Answers 24

24
\$\begingroup\$

05AB1E, 21 bytes

Defines a program \$f : \color{purple}{\texttt{AlphabeticChar}} \rightarrow \color{purple}{\texttt{String}}\$

Code:

2AA¹k._•1못*Ć,ãiDΣ•Λ

Try it online!


Breakdown:

2AA¹k._•1못*Ć,ãiDΣ•Λ

2                       # <length>
 AA¹k._                 # <filler>
       •1못*Ć,ãiDΣ•    # <pattern>
                    Λ   # Invoke the canvas function.


Explanation:

The canvas (Λ) in this particular context works as a function with the following signature:

$$ \mathsf{\Lambda} : \left(\texttt{length}: \color{purple}{\texttt{Nat}},\ \texttt{filler}: \color{purple}{\texttt{String}},\ \texttt{pattern}: \color{purple}{\texttt{Nat}}\right) \rightarrow \color{purple}{\texttt{String}} $$

The \$\texttt{pattern}\$ parameter is in this situation a number defining the directions. In the code, this number is represented as •1못*Ć,ãiDΣ•, which is a compressed version of the big number \$2232344565666667670012122\$. Directions are denoted in the following manner:


$$ \begin{array}{l} 7 & & 0 & & 1 \\ & \nwarrow & \uparrow & \nearrow & \\ 6 & \leftarrow & \bullet & \rightarrow & 2 \\ & \swarrow & \downarrow & \searrow & \\ 5 & & 4 & & 3 \end{array} $$


This means that the big number represents the following pattern of directions:

$$ [\rightarrow, \rightarrow, \searrow, \rightarrow, \searrow, \downarrow, \downarrow, \swarrow, \leftarrow, \swarrow, \leftarrow, \leftarrow, \leftarrow, \leftarrow, \leftarrow, \nwarrow, \leftarrow, \nwarrow, \uparrow, \uparrow, \nearrow, \rightarrow, \nearrow, \rightarrow, \rightarrow] $$

With this signature context, the canvas iterates through the \$\texttt{pattern}\$ list and writes \$\texttt{length}\$ characters from the \$\texttt{filler}\$ in the current direction.

The \$\texttt{length}\$ is specified in the code as \$2\$ (at the beginning of the code). For the \$\texttt{filler}\$, we need a rotated version of the alphabet such that it starts with the given input. That is done with the following code (try it here):

AA¹k._

 A¹k       # Find the <index> of the given input character in the alphabet
A   ._     # Rotate the alphabet to the left <index> times.

In pseudocode, this would be executed by the canvas function:

\$ \begin{array}{l} 1. & \text{Write } \color{blue}{\texttt{ab}} \text{ in the direction} \rightarrow \\ 2. & \text{Write } \color{blue}{\texttt{bc}} \text{ in the direction} \rightarrow \\ 3. & \text{Write } \color{blue}{\texttt{cd}} \text{ in the direction} \searrow \\ 4. & \text{Write } \color{blue}{\texttt{de}} \text{ in the direction} \rightarrow \\ 5. & \text{Write } \color{blue}{\texttt{ef}} \text{ in the direction} \searrow \\ 6. & \text{Write } \color{blue}{\texttt{fg}} \text{ in the direction} \downarrow \\ \dots \end{array} \$

Lastly, you can see that the filler argument is 'rotated' \$\texttt{length} - 1\$ times to the right, meaning that the canvas would iterate through the following (cycled and therefore infinite) list:

$$ [\color{blue}{\texttt{ab}}, \color{blue}{\texttt{bc}}, \color{blue}{\texttt{cd}}, \color{blue}{\texttt{de}}, \color{blue}{\texttt{ef}}, \color{blue}{\texttt{fg}}, \color{blue}{\texttt{gh}}, \color{blue}{\texttt{hi}}, \color{blue}{\texttt{ij}}, \color{blue}{\texttt{jk}}, ... $$

Which results in the desired alphabet soup ascii-art shape.

\$\endgroup\$
0
11
\$\begingroup\$

Perl 6, 100 bytes

{"2XYZABC
 VW5DE
U9F
T9G
S9H
 RQ5JI
2PONMLK".trans(/\S/=>{(try ' 'x$/+1)||chr ($/.ord+.ord)%26+65})}

Try it online!

Replaces all letters in the string with their shifted counterparts, while replacing digits with the number of spaces they represent plus one.

Explanation

{                                                            }# Anonymous code block
 "...".trans(/\S/=>{                                       }) # Translate non-whitespace
                    (try ' 'x$/+1)      # If digits, the amount of spaces plus one
                                  ||chr ($/.ord+.ord)%26+64  # Else the shifted letter

\$\endgroup\$
9
\$\begingroup\$

Ruby, 107 bytes

->n{a=(0..6).map{' '*11}
(?A..?Z).map{|i|j,k=(1i**((i.ord-n.ord-6)/6.5)).rect;a[3.5*k+=1][5.2*j+6]=i}
a*$/}

Try it online!

Improved syntax "i".to_c -> 1i (Suggested by Jordan)

Changed coordinate system so 0 degrees is at right instead of top. This enables 0.5 -> 6

Adjusted multipliers of j and k for shortness

Rather than print output puts a, concatenate array elements and return a string a*$/

Ruby, 119 bytes

->n{a=(0..6).map{' '*11}
(?A..?Z).map{|i|j,k=("i".to_c**((i.ord-n.ord+0.5)/6.5)).rect;a[3.5-j*3.3][6+k*5.17]=i}
puts a}

Uses a complex number raised to a power to map to an ellipse. A complete turn is 26, so each quadrant is 6.5.

This approach relies on the required output resembling an ellipse sufficiently that a valid mapping can be achieved.

Try it online!

\$\endgroup\$
0
9
\$\begingroup\$

MATL, 49 bytes

7I8*32tvB[1b]&Zvc2Y2j7+_YSy&f7-w4-_Z;YPE\,&S])yg(

What a mess. But it was fun writing. There's even an arctangent involved.

Try it online!

Explanation

The code

7I8*32tvB

creates an array of numbers and converts them to binary. This gives the zero-one matrix

0 0 0 1 1 1
0 1 1 0 0 0
1 0 0 0 0 0
1 0 0 0 0 0

which is the top left quadrant of a matrix specfying the positions of the letters.

[1b]&Zv

reflects that quadrant vertically without repeating the last row, and horizontally repeating the last column to produce the full matrix:

0 0 0 1 1 1 1 1 1 0 0 0
0 1 1 0 0 0 0 0 0 1 1 0
1 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 1
0 1 1 0 0 0 0 0 0 1 1 0
0 0 0 1 1 1 1 1 1 0 0 0

We now have a mask with the positions. The code

c

converts this to char, because the final result will be a char matrix. Character 0 is displayed as space, and the nonzero entries will be written with the appropriate letters.

2Y2

pushes the string 'abc···xyz', which contains the 26 letters. This string needs to be circularly shifted as per the input. To do that,

j7+_YS

reads the input letter, adds 7 to its ASCII code, and negates the result. For input 'a' this gives −104, which is a multiple of 26, so circularly shifting by this amount will do nothing. If the input is b this gives −105, which shifts the string 1 step to the left to produce 'bcd···yza'; etc.

The next step is to define the order in which the shifted string will be written into the nonzero entries of the matrix. To this end,

y&f

creates a copy of the matrix and pushes two vectors containing the 1-based row and column positions of the nonzeros. Then

7-w4-_

subtracts 7 from the latter, brings the former to top, substracts 4 from it, and negates it. The 7 and 4 specify an origin of coordinates, so that the angles of the position vectors of the nonzero entries with respect to that origin define the desired order.

Z;YPE\

computes the two-argument arctangent modulo 2*pi to produce those angles. Now the smallest angle, which is 0, corresponds to the entry where the first letter should go, and the rest advance counter-clockwise.

,&S])

rearranges the letters in the string according to those angles, so that when the letters are written into the nonzero entries of the matrix in column-major order (down, then across) the result will be correct. This is done by

yg(

For example, if the input is 'a' the string was not circularly shifted:

abcdefghijklmnopqrstuvwxyz

The rearranging as per the angles transforms this into

utsvrwqxpyoznamblckdjeifgh

so that 'u' will correctly go to the first (in column-major order) nonzero entry, which is (3,1) in matrix notation; 't' will go to (4,1), 's' to (5,1); 'v' to (2,2) etc:

   ······   
 v·      ·· 
u          ·
t          ·
s          ·
 ··      ·· 
   ······   
\$\endgroup\$
2
  • \$\begingroup\$ @EriktheOutgolfer I finally found some time to add an explanation \$\endgroup\$
    – Luis Mendo
    Mar 22, 2019 at 10:06
  • 2
    \$\begingroup\$ Woah... I actually thought you abandoned this because you deleted that comment. :P \$\endgroup\$ Mar 22, 2019 at 10:08
8
\$\begingroup\$

Charcoal, 33 bytes

GH→→↘→↘↓↓77←←←←↖←↖↑↑↗→↗→→²✂⁺αα⌕αS

Try it online! Link is to verbose version of code. Explanation:

GH

Trace a path.

→→↘→↘↓↓77←←←←↖←↖↑↑↗→↗→→

Outline the bowl. Each 7 expands to ↙←.

²

Move one character at a time (this API overlaps each line's ends with the next).

✂⁺αα⌕αS

Draw using the doubled alphabet, but starting at the position of the input character.

\$\endgroup\$
7
\$\begingroup\$

Python 2, 129 bytes

lambda x:''.join((i,chr((ord(x)+ord(i))%26+65),' '*5)[ord(i)/46]for i in'''   XYZABC
 VW] DE
U]]F
T]]G
S]]H
 RQ] JI
   PONMLK''')

Try it online!

\$\endgroup\$
7
\$\begingroup\$

R, 139 122 bytes

-17 bytes thanks to Giuseppe

u=utf8ToInt;`*`=rep;o=c(' '*12,'
')*7;o[u("  &3@LKWVUTSRDC5(")]=LETTERS[(13:38+u(scan(,'')))%%26+1];cat(o,sep='')

Explanation:

o=rep(c(rep(' ',12),'
'),7) 

Builds an empty box of spaces

u(" &3@LKWVUTSRDC5(")

is a set of indices for letter positions corresponding to:

c(7:9,23,24,38,51,64,76,75,87:82,68,67,53,40,27,15,16,4:6)

TIO

\$\endgroup\$
3
  • 1
    \$\begingroup\$ you never use intToUtf8 so those are extraneous bytes, but if you use * in place of rep, you can get save 2 bytes and get to 125 bytes \$\endgroup\$
    – Giuseppe
    Mar 20, 2019 at 18:00
  • 1
    \$\begingroup\$ Oh, and using the low-byte characters instead of printable ascii you can shave off the -32, for 122 bytes. You can generate them yourself by using cat(intToUtf8(bytes)). \$\endgroup\$
    – Giuseppe
    Mar 20, 2019 at 18:03
  • \$\begingroup\$ @Giuseppe I was pretty sure I had already removed the intToUtf8, too many versions of the function open at once I guess. Nice saves all roundthough, thanks \$\endgroup\$ Mar 20, 2019 at 21:30
6
\$\begingroup\$

JavaScript (Node.js),  121  119 bytes

Saved 2 bytes thanks to @tsh

c=>`2XYZABC
0VW5DE
U9F
T9G
S9H
0RQ5JI
2PONMLK`.replace(/./g,x=>''.padEnd(+x+1)||(B=Buffer)([65+([a,b]=B(c+x),a+b)%26]))

Try it online!

How?

This code abuses Buffer to extract the ASCII codes of the template letter \$x\$ and input letter \$c\$ and convert them back into the target letter.

Example with \$c=\$"H" and \$x=\$"B"

// extracting the ASCII codes
Buffer(c + x) → Buffer("HB") → <Buffer 48 42>

// assigning them to variables
[a, b] = Buffer(c + x) → a = 0x48 (72) and b = 0x42 (66)

// computing the ASCII code of the target letter
65 + ((a + b) % 26) → 65 + (138 % 26) → 65 + 8 → 73

// turning it back into a character
Buffer([73]) → <Buffer 49> → implicitly coerced to "I" by replace()
\$\endgroup\$
1
  • \$\begingroup\$ 119 bytes \$\endgroup\$
    – tsh
    Mar 20, 2019 at 5:56
4
\$\begingroup\$

APL+WIN, 72 bytes

Prompts for character

m←7 12⍴' '⋄n←(7⍴2)⊤v,⌽v←(⍳3)/28 34 65⋄((,n)/,m)←(22+s⍳⎕)⌽s←⎕av[65+⍳26]⋄m

Try it online!Coutesy of Dyalog Classic

\$\endgroup\$
4
\$\begingroup\$

R, 218 197 bytes

-21 bytes thanks to Giuseppe

function(t,l=letters,`*`=rep,s=" ",n="
",`~`=`[`,r=c(l~l>=t,l))cat(s*3,r~24:26,r~1:3,n,s,r~22:23,q<-s*6,r~4:5,n,r~21,u<-s*10,r~6,n,r~20,u,r~7,n,r~19,u,r~8,n,s,r~17:18,q,r~10:9,n,s*3,r~16:11,sep='')

Try it online!

Ungolfed:

alphasoup <- function(startlet){
  startnum <- which(l == startlet)
  rotatedletters <- c(letters[startnum:26], letters[1:(startnum -1)])[1:26]
  cat('   ',rotatedletters[24:26],rotatedletters[1:3], '\n ', 
      rotatedletters[22:23], s6 <- '      ', rotatedletters[4:5], '\n',
      rotatedletters[21], s10 <- rep(' ', 10), rotatedletters[6], '\n',
      rotatedletters[20], s10, rotatedletters[7], '\n',
      rotatedletters[19], s10, rotatedletters[8], '\n ',
      rotatedletters[17:18], s6, rotatedletters[10:9], '\n   ',
      rotatedletters[16:11],
      sep = '')
}

Created rotated letter vector and uses cat to fill out rim of bowl with that vector.

\$\endgroup\$
3
  • \$\begingroup\$ 203 bytes if you don't mind one-line monstrosities; the biggest improvement was probably removing the which and using l>=t as the index directly, which was worth 12 bytes. \$\endgroup\$
    – Giuseppe
    Mar 20, 2019 at 17:52
  • 2
    \$\begingroup\$ 198 bytes by aliasing [ with ~. This is a great answer; I was spending about 250 bytes in my first few attempts with a much more complex approach. \$\endgroup\$
    – Giuseppe
    Mar 20, 2019 at 17:56
  • \$\begingroup\$ Ah, that's clever, I forget about string comparisons. \$\endgroup\$
    – CT Hall
    Mar 20, 2019 at 17:56
4
\$\begingroup\$

Java 11, 134 bytes

c->"2XYZABC_0VW5DE_U9F_T9G_S9H_0RQ5JI_2PONMLK".chars().forEach(i->System.out.print(i<59?" ".repeat(i-47):(char)(i>90?10:(c+i)%26+65)))

Try it online.

136 bytes version with potential to be golfed?

c->"2XYZABC_0VW5DE_U9F_T9G_S9H_0RQ5JI_2PONMLK".chars().forEach(i->System.out.printf("%"+(i<59?i-47:"")+"c",i>90?10:i<59?32:(c+i)%26+65))

Try it online.

Explanation (of the first answer)

c->                          // Method with character parameter and no return-type
  "2XYZABC_0VW5DE_U9F_T9G_S9H_0RQ5JI_2PONMLK"
                             //  Template-String
    .chars().forEach(i->     //  Loop over the unicode values of its characters:
    System.out.print(        //   Print:
     i<59?                   //    If the value is below 59 (so a digit character):
      " ".repeat(i-47)       //     Repeat a space that digit + 1 amount of times
     :(char)(i>90?           //    Else-if the value is above 90 (an underscore character):
              10             //     Print a newline
             :               //    Else:
              (c+i)          //     Add the current value and the input together
                   %26       //     Take modulo-26 of it to get the index in the alphabet
                      +65))) //     And add 65 to make it an uppercase letter
\$\endgroup\$
2
  • \$\begingroup\$ Why not replace digits with unprintables? That way, you could just omit the i-47 \$\endgroup\$
    – Gymhgy
    Mar 24, 2019 at 2:34
  • \$\begingroup\$ @EmbodimentofIgnorance I'm afraid it wouldn't save any bytes. The numbers of spaces are 3, 1, 6, and 10. The 10 is used three times and is 2 bytes each as character (\n). So whether I use unprintables and 3x \n with i or digits-1 with i-47, both are the same 134 byte-count. And unfortunately I can't have an unprintable 0, otherwise I could have used 2,0,5,9 instead, and use i+1 to save 1 byte in total. \$\endgroup\$ Mar 24, 2019 at 10:44
2
\$\begingroup\$

Wolfram Language (Mathematica), 258 bytes

(t[x_]:=Table[" ",x];w=RotateRight[Alphabet[],4-LetterNumber@#];j=Join;a[x_,y_]:=j[{w[[x]]},t@10,{w[[y]]}];b[m_,n_]:=j[t@1,w[[m;;m+1]],t@6,w[[n;;n+1]],t@1];""<>#&/@{j[t@3,w[[1;;6]]],b[25,7],a[24,9],a[23,10],a[22,11],Reverse@b[12,20],j[t@3,w[[19;;14;;-1]]]})&

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Haskell, 127 bytes

("cXYZABC aVWfDE UjF TjG SjH aRQfJI cPONMLK">>=).(?)
t?c|c>'Z'=' '<$['a'..c]|c<'!'="\n"|t<'B'=[c]|c>'Y'=t?'@'|1<2=pred t?succ c

Try it online!

Each character in the encoded string is decoded by function ? into a string:

t?c                             -- 't' is the starting char,
                                -- 'c' the char from the encoded string
   |c>'Z'=' '<$['a'..c]         -- if 'c' is a lowercase letter, return some spaces
                                -- 'a': one, 'b': two, etc
   |c<'!'="\n"                  -- if 'c' is a space, return a newline
   |t<'B'=[c]                   -- if 't' is the letter A, return 'c'
   |c>'Y'=t?'@'                 -- wrap around Z
   |1<2=pred t?succ c           -- else the result is the same as starting one letter
                                -- earlier (pred t) but looking at the successor of 'c'
\$\endgroup\$
2
\$\begingroup\$

Kotlin, 148 146 145 bytes

Removed extra parentheses for -2
Replaced raw string for -1

{l:Char->"2XYZABC 0VW5DE U9F T9G S9H 0RQ5JI 2PONMLK".map{c->if(c>'@')((c-'A'+(l-'A'))%26+65).toChar()
else if(c>' ')" ".repeat(c-'/')
else '\n'}}

Try it online!

\$\endgroup\$
0
2
\$\begingroup\$

C# (Visual C# Interactive Compiler), 126 118 bytes

n=>$@"   XYZABC
 VW{"",6}DE
U {"",9}F
T {"",9}G
S {"",9}H
 RQ{"",6}JI
   PONMLK".Select(b=>b<65?b:(char)((b+n)%26+65))

Saved 8 bytes thanks to @someone. Yes, that's actually his username.

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ String interpolation for 118 bytes. It is likely acceptable to return a array of charcodes (saving ~8 bytes), but I'm not sure. \$\endgroup\$ Mar 23, 2019 at 14:25
  • \$\begingroup\$ @someone Nice, forgot about string interpolation \$\endgroup\$
    – Gymhgy
    Mar 24, 2019 at 2:32
2
\$\begingroup\$

TSQL query, 238 bytes

DECLARE @y char='G'

,@ char(91)=3;WITH C as(SELECT'5585877636333330301125255'z,8a,ascii(@y)x
UNION ALL
SELECT stuff(z,1,1,''),a+left(z,1)/3*13+left(z,1)%3-14,(x+14)%26+65FROM
C WHERE''<z)SELECT
@=stuff(stuff(@,a,1,char(x)),1+a/13*13,1,char(13))FROM
C PRINT @

The test link for this answer broke the line breaks and excluded the spaces. I have replaced the spaces with period and replaced char(13) with char(13)+char(10) in order to show a readable result.

Try it online

Ungolfed:

DECLARE @y char='G'

-- @ is the string being printed last. 
-- @ is populated with 3 to save a byte
-- the number 3 gets replaced later
-- (this could have been any 1 digit value), 
-- @ is automatically filled with spaces, because
-- it is declared as a char(x) and assigned a value
,@ char(91)=3;
-- recursive query
WITH C as
(
-- z string containing digits for the direction of next letter
-- z should not contain 4 because it will point to same position.
-- values in z 0,1,2,3,4,5,6,7,8 can logally convert to 
-- (-1,-1),(-1,0),(-1,1),(0,-1),(0,0),(0,1),(1,-1),(1,0),(1,1)
-- a is the starting position
  SELECT'5585877636333330301125255'z,8a,ascii(@y)x
  UNION ALL
-- stuff remove first character from the z string
-- a calculate next position of the next letter
-- x cycle the input letter
  SELECT stuff(z,1,1,''),a+left(z,1)/3*13+left(z,1)%3-14,(x+14)%26+65
-- repeat recursive until long string is empty
  FROM C
  WHERE''<z
)
SELECT
-- 1st stuff replace the character to created the start of a 
--   logical line in the string @ this is where 3 gets overwritten
-- 2nd stuff replaces a character(space if coded correct) 
--  with the letter at the calculated position.
  @=stuff(stuff(@,a,1,char(x)),1+a/13*13,1,char(13))
FROM C

PRINT @
\$\endgroup\$
3
  • \$\begingroup\$ @MickyT ok, thanks for your feedback, I will fix it later today if i can get access to a db \$\endgroup\$ Mar 20, 2019 at 18:16
  • \$\begingroup\$ @MickyT it should be fixed now \$\endgroup\$ Mar 21, 2019 at 10:24
  • 1
    \$\begingroup\$ looks good now. \$\endgroup\$
    – MickyT
    Mar 21, 2019 at 17:17
2
\$\begingroup\$

C(GCC) 286 270 bytes

-12 bytes ceilingcat

Not exactly the shortest golf, but it works

#define r(a)(a+c)%26+65
#define R(a)for(i=10;i;a[--i]>32?a[i]=r(a[i]):0);
i;f(c){char T[]="   XYZABC\n",E[]="VW      DE\n",F[]="RQ      JI\n",B[]="   PONMLK";R(T)R(E)R(F)R(B)printf("%s %s%c%*c\n%c%*c\n%c%*c\n %s%s",T,E,r(85),11,r(70),r(84),11,r(71),r(83),11,r(72),F,B);}

Try it online!

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

PHP, 236 229 226 bytes

<?=($a=ord(file_get_contents('php://stdin'))-65)?preg_replace_callback('~\w~',function($m)use($a){return chr((ord($m[0])-65+$a)%26+65);},'   XYZABC
 VW      DE
U          F
T          G
S          H
 RQ      JI
   PONMLK'):'';

Try it online!

Pre-golf:

<?php
$adjust = ord(file_get_contents('php://stdin')) - 65;
echo preg_replace_callback('~\w~', function($match) use ($adjust) {
    $new = ord($match[0]) - 65;
    $new = ($new + $adjust) % 26;
    $new += 65;
    return chr($new);
}, '   XYZABC
 VW      DE
U          F
T          G
S          H
 RQ      JI
   PONMLK');

Explanation:

Using ord we convert to an integer between 0 and 255. A is 65 and Z is 90.
Using this knowledge, we take the input and reduce it by 65 so we have an adjustment value.
We then iterate over all characters, call ord on them, reduce them by 65, increase them by our adjustment value. Using modulo we loop back down to 0 if they exceed 26.
We then increase them by 65 again and convert them back into letters with chr.

Sadly php://stdin can only be interogated once, so we need to pass the input into the function in our loop, preventing us from saving bytes on use($a) and having to declare a variable outside the function stops us from cleanly using the <?= echo method - we have to wrap everything in a giant ternary.

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

Javascript (V8), 316 bytes

function a(b){n="abcdefghijklmnopqrstuvwxyz".split(b);n=b+n[1]+n[0],console.log(`   ${n[23]+n[24]+n[25]+n[0]+n[1]+n[2]}\n ${n[21]+n[22]}      ${n[3]+n[4]}\n${n[20]}          ${n[5]}\n${n[19]}          ${n[6]}\n${n[18]}          ${n[7]}\n ${n[17]+n[16]}      ${n[9]+n[8]}\n   ${n[15]+n[14]+n[13]+n[12]+n[11]+n[10]}`)}

Try it online

First time trying code golf out. Appreciate any tips / feedback.

Original code before minifying:

function a(b){
    var c = ("abcdefghijklmnopqrstuvwxyz").split(b);
    c = b+c[1]+c[0]
    console.log(`   ${c[23]+c[24]+c[25]+c[0]+c[1]+c[2]}\n ${c[21]+c[22]}      ${c[3]+c[4]}\n${c[20]}          ${c[5]}\n${c[19]}          ${c[6]}\n${c[18]}          ${c[7]}\n ${c[17]+c[16]}      ${c[9]+c[8]}\n   ${c[15]+c[14]+c[13]+c[12]+c[11]+c[10]}`)
}
\$\endgroup\$
3
  • \$\begingroup\$ Hello and welcome to PPCG. As it stands, your submission is a snippet, which is invalid I/O. Please fix your answer to either be a full program or function -- as in you non-minified version. \$\endgroup\$ Mar 22, 2019 at 9:37
  • \$\begingroup\$ @Jonathan Fresch thanks! Would that suffice? \$\endgroup\$
    – Edwin Chua
    Mar 22, 2019 at 11:50
  • 1
    \$\begingroup\$ Yes, this is now a valid submission. \$\endgroup\$ Mar 22, 2019 at 14:34
0
\$\begingroup\$

Red, 139 bytes

func[a][foreach c{   XYZABC
 VW      DE
U          F
T          G
S          H
 RQ      JI
   PONMLK}[if c > sp[c: c + a % 26 + 65]prin c]]

Try it online!

A really naive solution.

\$\endgroup\$
0
\$\begingroup\$

Perl 5 -p, 110 bytes

$b=/A/?Z:chr(-1+ord);$_=eval("'2XYZABC
 VW5DE
U9F
T9G
S9H
 RQ5JI
2PONMLK'=~y/A-Z/$_-ZA-$b/r");s/\d/$".$"x$&/eg

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ $b is unnecessary. -24 bytes: TIO. \$\endgroup\$
    – Grimmy
    Mar 21, 2019 at 8:44
  • \$\begingroup\$ -2 bytes TIO \$\endgroup\$ Mar 21, 2019 at 13:30
0
\$\begingroup\$

APL (Dyalog Classic), 50 bytes

⎕a@((-⎕a⍳⍞)⌽∪⌊.5+{3 5.5×1-2 1○○⍵÷¯19}¨⍳38)⊢7 12⍴''

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ So this makes a circle of numbers 1-26 and indexes ⎕A into it, right? \$\endgroup\$
    – Razetime
    Sep 15, 2020 at 5:54
  • 1
    \$\begingroup\$ @Razetime actually, it makes a list of coordinate pairs for the letters, and amends a blank 7×12 matrix to put ⎕A in those positions \$\endgroup\$
    – ngn
    Sep 15, 2020 at 12:27
0
\$\begingroup\$

C (gcc), 200 198 197 bytes

-3 bytes thanks to ceilingcat.

x,z;f(c){char*y,t[27],i=0;for(c-=46;i<14;t[13+i++]=(c-i)%26+65)t[i]=(c+i)%26+65;for(i=-4;++i<4;printf("%*.*s%*.*s\n",3273>>x*3&7,x?:1,y,z,x?:1,y+2*!i+z))z="--*%"[x=abs(i)]-34,y=t+x+(x>2)+13*(i>0);}

Try it online!

\$\endgroup\$
0
0
\$\begingroup\$

PHP, 131 bytes

for(;$s='2YZABCD
 WX5EF
V9G
U9H
T9I
 SR5KJ
2QPONML'[$i++];)echo$s<A?$s<'0'?$s:str_pad('',$s+1,' '):chr(65+(ord($s)+ord($argn))%26);

Try it online!

\$\endgroup\$

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.