# Making octogonal words

Last Thursday user @SpookyGengar delighted us with his/her first challenge about Making Squared Words. What if we double the number of sides?

### The challenge

Take a string as input in any reasonable format you need (string, char array...) and output an octogonal representation of the input (also in any reasonable format: string, list of strings, char matrix...) as in the next examples:

Input: golf
Output:

golf
o    l
l      o
f        g
l        o
o        l
g        f
o      l
l    o
flog

Input: HelloWorld
Output:

HelloWorld
e          l
l            r
l              o
o                W
W                  o
o                    l
r                      l
l                        e
d                          H
l                          e
r                          l
o                          l
W                          o
o                          W
l                          o
l                          r
e                          l
H                          d
e                        l
l                      r
l                    o
o                  W
W                o
o              l
r            l
l          e
dlroWolleH

Input: a
Output:

a

Input: ab
Output:

ab
b  a
a  b
ba

Input: code golf
Output:

code golf
o         l
d           o
e             g

g                 e
o                   d
l                     o
f                       c
l                       o
o                       d
g                       e

e                       g
d                       o
o                       l
c                       f
o                     l
d                   o
e                 g

g             e
o           d
l         o
flog edoc


### Notes

• Input will consist only of printable ASCII characters.
• Leading and/or trailing whitespaces and newlines allowed as long as the octogonal shape is maintained.
• This is , so may the sortest program/function for each language win!
• "What if we double the number of sides?" <-- then charcoal would still win – Leaky Nun Jul 24 '17 at 6:30
• Suggested testcase: code golf – Leaky Nun Jul 24 '17 at 6:31
• @LeakyNun is <Space> considered printable? – V. Courtois Jul 24 '17 at 6:43
• @V.Courtois yes – Leaky Nun Jul 24 '17 at 6:44
• @LeakyNun added test case. – Charlie Jul 24 '17 at 7:29

# Charcoal, 16 bytes (10 bytes noncompeting)

Ｆ⁸«✂θ⁰±¹↷¹Ａ⮌θθ»θ


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

Ｆ⁸«                 Repeat for each side of the octagon
✂θ⁰±    ¹        Print the input except the last character
↷¹          Rotate 45° clockwise
Ａ⮌θθ      Reverse the input string
»θ    Print the input again, to handle the length 1 case


Alternative length 1 fix, also 16 bytes: Verbose version.

ＰθＦ⁸«✂θ⁰±¹↷¹Ａ⮌θθ


A Charcoal bugfix means that the following 10-byte code now works: Verbose version.

Ｆ⁴«θ←↘⮌θ↖⟲

• ⟲ seems like it should just do this. Lol – Magic Octopus Urn Jul 24 '17 at 14:39
• Umm, I don't think code golf works does it? – Erik the Outgolfer Jul 24 '17 at 15:17
• Oh, and this is in fact 16 bytes (replace θθθ with θθ»θ). – Erik the Outgolfer Jul 24 '17 at 15:26
• @EriktheOutgolfer Oops, sorry about that, I didn't think to test my fix for the length 1 case... my test record on the question hasn't been very good has it? – Neil Jul 24 '17 at 15:53
• @Neil But what about the issue with code golf? I think that has to do with how Charcoal takes input, which, unfortunately, disqualifies it completely from this challenge as that can't be circumvented. – Erik the Outgolfer Jul 24 '17 at 15:55

## JavaScript (ES6), 156 bytes

f=
s=>[...Array((l=s.length-1)*3+1)].map((_,i,a)=>a.map((_,j)=>s[i+j-l?l*5-i-j?i+l*2-j?j+l*2-i?i%(l*3)?j%(l*3)?-1:j?i-l:l+l-i:i?l+l-j:j-l:j:l-i:l*3-i:i]|| ))
<input oninput=o.textContent=this.value?f(this.value).map(function(a){return(a.join)}).join\n:><pre id=o>

Returns a character array.

• It seems to work only with strings of length 4. – Charlie Jul 24 '17 at 9:47
• @CarlosAlejo Sorry, didn't check carefully enough. Conveniently, fix saved 2 bytes! – Neil Jul 24 '17 at 11:42
• Oh my that's a lot of ternary ?:s! – Erik the Outgolfer Jul 24 '17 at 14:59
• @EriktheOutgolfer good thing it's not python or that would be really verbose :P – Stephen Jul 24 '17 at 15:11

# Mathematica, 247 bytes

(T=Table;k=Length[s=Characters@#];If[k==1,#,Column[Flatten@{#,T[""<>{s[[i]],T["  ",k/2-2+i],s[[-i]]},{i,2,k}],T[""<>{s[[-i]],T["  ",k+k/2-2],s[[i]]},{i,2,k}],T[""<>{s[[i]],T["  ",3k/2-1-i],s[[-i]]},{i,2,k-1}],StringReverse@#},Alignment->Center]])&

• You don't need Alignment and you'd save bytes by using delayedset (:=) to reduce the repetition of s[[i]] and s[[-i]], You get down to 224 bytes with these ideas: (T=Table;q:=s[[i]];r:=s[[-i]];k=Length[s=Characters@#];If[k==1,#,Column[Flatten@{#,T[""<>{q,T[" ",k/2-2+i],r},{i,2,k}],T[""<>{r,T[" ",k+k/2-2],q},{i,2,k}],T[""<>{q,T[" ",3k/2-1-i],r},{i,2,k-1}],StringReverse@#},Center]])& Also, I have an alternate solution that's only 145 bytes at the time of this comment. – Mark S. Jul 26 '17 at 10:44

# Pyth, 7969 65 bytes

j++K+]+*dJtlzzm+++*;-Jd@zd*;+ytdlz@z_hdSJjL*;t*3tlztC_B_zt__MC.tK


# Japt, 84 79 bytes

-5 bytes thanks to @ETHproductions.


Ål
VoA{A?(UÅw +Uê)£Y¥V*2+AªY¥V-A?X:SÃ:Vç +U+Vç
Wf cU¬£V?(V*3 ç hUg~Y)+X:XÃcWz2


Leading newline is part of program. Takes a string as input and returns an array of strings.

Try it online! with the -R flag to join the resulting array with newlines.

Not my proudest work, but I got it down from ~100 bytes, at least. My idea here was to create the top and middle parts, then append the top portion, rotated 180°.

• Nice. I don't see a lot of improvements right away, but you could change Ul É to UÅl, and switch V and W to save 2 more bytes: codepen.io/justinm53/full/… – ETHproductions Jul 25 '17 at 14:09
• Also, AnV ? -> V-A?, and Uq £ -> U¬£ – ETHproductions Jul 25 '17 at 14:11
• @ETHproductions Awesome, thanks! Can't believe I forgot about ¬. – Justin Mariner Jul 25 '17 at 18:03

# Python 2, 220 213 bytes

• Surprisingly longer than I had imagined.
a=input()
l=len(a)
r=range(l)
print'\n'.join(p.center(l*3-2)for p in[a]+(l>1)*([a[i]+(2*i-2+l)*' '+a[~i]for i in r[1:-1]]+[a[~i]+(l*3-4)*' '+a[i]for i in r]+[a[i]+(3*l-2*i-4)*' '+a[~i]for i in r[1:-1]]+[a[::-1]]))


Try it online!

# PHP 7.1, 230 156 155 bytes

for($x=$e=strlen($s=$argn)-1;$n<9;$r[$y][$x+=$n+1&3?$n&4?-1:1:0]=$s[$i],$i+=($n+=!$i||$i==$e)&1?:-1)$r[$y+=$n-1&3?$n<6?:-1:0]=$r[$y]?:"";echo join(" ",$r);


Run as pipe with -nR or try it online.

breakdown

for($x=$e=strlen($s=$argn)-1;   # import to $s, set$e to length-1, init $x$n<9;                       # loop through 8 repetitions of string
$r[$y][
$x+=$n+1&3?$n&4?-1:1:0 # 3. increment/decrement$x depending on $n ]=$s[$i], # 4. copy character to current position$i+=(
$n+=!$i||$i==$e             # 5. if first or last character of string, increment $n )&1?:-1 # 6. if odd repetition next, else previous character )$r[
$y+=$n-1&3?$n<6?:-1:0 # 1. increment/decrement$y depending on $n ]=$r[$y]?:""; # 2. if empty, init row to string echo join("\n",$r);             # output


# Mathematica, 168166147 127 bytes

(n=Length@#;b=Array[" "&,3{n,n}-2];Do[l={{1,n+k-1},{k,n-k+1}};l=Join[l,Cross/@l];b=ReplacePart[b,Join[l,-l]->#[[k]]],{k,n}];b)&


This takes a list of one-character strings and outputs a matrix of one-character strings.

I saved 18 bytes by exploiting the symmetry to use -l and Cross/@l which takes something like a cross-product of each of the two single 2D vectors to take {x,y} to {-y,x}. Basically, the two initial directions are East (top edge) and Southwest (top right edge). Then we add in 90-degree counterclockwise rotations of them with Cross: North for left edge and Southeast for bottom left edge. Then we add in the other four pieces using -l to flip the four we covered.

You can test it out on the sandbox with something like:

(n=Length@#;b=Array[" "&,3{n,n}-2];Do[l={{1,n+k-1},{k,n-k+1}};l=Join[l,Cross/@l];b=ReplacePart[b,Join[l,-l]->#[[k]]],{k,n}];b)&[{"H","e","l","l","o"," ","G","o","l","f"}]//MatrixForm


# Canvas, 14 bytes

４Ｈ┐⁸ｋ⁸±＼＋⁸Ｌ１╋↶


Try it here!