14
\$\begingroup\$

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!
\$\endgroup\$
5
  • 6
    \$\begingroup\$ "What if we double the number of sides?" <-- then charcoal would still win \$\endgroup\$
    – Leaky Nun
    Jul 24, 2017 at 6:30
  • \$\begingroup\$ Suggested testcase: code golf \$\endgroup\$
    – Leaky Nun
    Jul 24, 2017 at 6:31
  • \$\begingroup\$ @LeakyNun is <Space> considered printable? \$\endgroup\$ Jul 24, 2017 at 6:43
  • 2
    \$\begingroup\$ @V.Courtois yes \$\endgroup\$
    – Leaky Nun
    Jul 24, 2017 at 6:44
  • \$\begingroup\$ @LeakyNun added test case. \$\endgroup\$
    – Charlie
    Jul 24, 2017 at 7:29

9 Answers 9

10
\$\begingroup\$

Charcoal, 16 bytes (10 bytes noncompeting)

F⁸«✂θ⁰±¹↷¹A⮌θθ»θ

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

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

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

PθF⁸«✂θ⁰±¹↷¹A⮌θθ

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

F⁴«θ←↘⮌θ↖⟲
\$\endgroup\$
15
  • \$\begingroup\$ seems like it should just do this. Lol \$\endgroup\$ Jul 24, 2017 at 14:39
  • 1
    \$\begingroup\$ Umm, I don't think code golf works does it? \$\endgroup\$ Jul 24, 2017 at 15:17
  • \$\begingroup\$ Oh, and this is in fact 16 bytes (replace θθθ with θθ»θ). \$\endgroup\$ Jul 24, 2017 at 15:26
  • \$\begingroup\$ @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? \$\endgroup\$
    – Neil
    Jul 24, 2017 at 15:53
  • \$\begingroup\$ @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. \$\endgroup\$ Jul 24, 2017 at 15:55
5
\$\begingroup\$

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.

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

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]])&
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Mark S.
    Jul 26, 2017 at 10:44
2
\$\begingroup\$

Pyth, 79 69 65 bytes

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

Test suite.

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

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°.

\$\endgroup\$
3
  • \$\begingroup\$ 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/… \$\endgroup\$ Jul 25, 2017 at 14:09
  • \$\begingroup\$ Also, AnV ? -> V-A?, and Uq £ -> U¬£ \$\endgroup\$ Jul 25, 2017 at 14:11
  • \$\begingroup\$ @ETHproductions Awesome, thanks! Can't believe I forgot about ¬. \$\endgroup\$ Jul 25, 2017 at 18:03
1
\$\begingroup\$

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!

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

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
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 168 166 147 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
\$\endgroup\$
1
\$\begingroup\$

Canvas, 14 bytes

4H┐⁸k⁸±\+⁸L1╋↶

Try it here!

\$\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.