HexaGolf: Wordagons

25

2

See also: Rotatagons

Challenge

Given a string as input, output its wordagon.

Wordagons

A wordagon is a way of representing a string in a hexagon. Now, let's create a wordagon from the string hexa:

Firstly, you start with the first character in the string a place it in the centre:

h

Then, you take the next character in the string and add a hexagonal layer:

 e e
e h e
 e e

Then, add the next layer:

  x x x
 x e e x
x e h e x
 x e e x
  x x x

Finally, add the last layer:

   a a a a
  a x x x a
 a x e e x a
a x e h e x a
 a x e e x a
  a x x x a
   a a a a

And you now have the wordagon for the string hexa.

Examples

Here's some I prepared earlier:

hello

    o o o o o
   o l l l l o
  o l l l l l o
 o l l e e l l o
o l l e h e l l o
 o l l e e l l o
  o l l l l l o
   o l l l l o
    o o o o o

PPcg

   g g g g
  g c c c g
 g c P P c g
g c P P P c g
 g c P P c g
  g c c c g
   g g g g

o *

  * * *
 *     *
*   o   *
 *     *
  * * *

(T_T)

    ) ) ) ) )
   ) T T T T )
  ) T _ _ _ T )
 ) T _ T T _ T )
) T _ T ( T _ T )
 ) T _ T T _ T )
  ) T _ _ _ T )
   ) T T T T )
    ) ) ) ) )

Note that trailing and/or leading newlines are allowed.

Winning

The shortest code in bytes wins.

Beta Decay

Posted 2016-09-05T12:25:52.017

Reputation: 21 478

14Bonus points if someone does this in Hexagony? ;) – Kevin Cruijssen – 2016-09-05T12:46:54.093

Can the input string contain linefeeds? I don't think it would pose any implementation problem, just produce invalid wordagons – Aaron – 2016-09-05T12:52:58.420

@Aaron No, the input string will never include newlines – Beta Decay – 2016-09-05T12:57:59.163

is it ok if the output is a string with newlines returned from a function? – Daniel – 2016-09-06T01:24:34.990

@Dopapp Yes, that's fine – Beta Decay – 2016-09-06T08:32:24.733

@KevinCruijssen You could always start a bounty ;) – Beta Decay – 2016-09-06T11:46:47.677

2Aww. I'm disappointed because I read the title as "word-dragons"... – mbomb007 – 2016-09-06T21:47:16.930

Answers

1

Pyth - 29 bytes

j+_K.e+*kdjdjF+*bhk*L2>zhkztK

Test Suite.

Maltysen

Posted 2016-09-05T12:25:52.017

Reputation: 25 023

19

Python 2, 83 bytes

s=input()
l=len(s)
while 1:l-=1;y=abs(l);print' '*y+' '.join(s[:y:-1]+s[y]*y+s[y:])

Prints the wordagon and then crashes (which only prints to STDERR).

Example:

% python2.7 wordagon.py <<<'"abcde"' 2&>/dev/null
    e e e e e
   e d d d d e
  e d c c c d e
 e d c b b c d e
e d c b a b c d e
 e d c b b c d e
  e d c c c d e
   e d d d d e
    e e e e e

xnor saved 5 bytes. Thanks!

Lynn

Posted 2016-09-05T12:25:52.017

Reputation: 55 648

2Elegant solution. – DavidC – 2016-09-05T15:24:49.323

1You can loop through the y's by doing n=len(s)\nwhile 1:n-=1;y=abs(n);... and terminating with error. – xnor – 2016-09-05T21:19:19.357

10

Vim, 92 bytes

:se ri|s/./ &/g
ⓋCⓇ"Ⓓ␛$vpmlmrqqYpi ␛`ljxxhmlylv`rjlmr:s/\%V\(.\)./Ⓡ" /g
@qq@qVdy2G:g/^/m0
Gp

Circled letters represent Control + letter; ␛ is escape.

asciicast

Lynn

Posted 2016-09-05T12:25:52.017

Reputation: 55 648

3Wouldn't this be counted as keystrokes? – Soren – 2016-09-05T16:29:47.623

1

@moo_we_all_do No. This is [tag:code-golf] (as opposed to editor golf), so all answers should be scored in bytes.

– Martin Ender – 2016-09-06T06:25:44.880

The second @q causes an infinite loop for me. It works perfectly if it's removed. – MTCoster – 2016-09-06T09:51:07.203

6

Mathematica 100 219 bytes

If ASCII-Art need not be Terminal-Art this should be valid.

My earlier submission mistakenly drew a star rather than a hexagon. I can't see how I was so off!

c = CirclePoints@6;
f@s_:=Graphics[{Text[s~StringPart~1,{0,0}],Flatten@Table[Text[StringPart[s,n+1],#]&/@Subdivide[Sequence@@#,n]&/@Partition[Riffle[(n)CirclePoints@6,RotateLeft[n CirclePoints@6]],2],{n,1,StringLength@s-1}]},BaseStyle->20]

CirclePoints@6 returns the vertices of a unit hexagon, assuming that the center is at the origin.

Subdivideing the coordinates for neighboring vertices finds equally spaced positions along the respective edge.

A counter from 1 through the StringLength -1 of the input string allows each layer of the wordagon to be handled separately.
As n increases, so does the respective distance of each vertex from the origin.

Text[s~StringPart~1,{0,0}] prints the first letter of the input at the origin.


f@"Wordagon"

wordagon


For the curious, this is what the star version looked like. I know, it was way off the mark. It only showed the letters at the hexagon's vertices.

Graphics@Table[Text[Style[StringPart[#, r + 1], 54], r {Cos@t, Sin@t}], {t, 0, 2π, π/3}, {r, 0, StringLength@# - 1}] &["Hexa"]

hexa

DavidC

Posted 2016-09-05T12:25:52.017

Reputation: 24 524

5Can you post what the star looked like? :D – Beta Decay – 2016-09-05T14:36:08.707

Sure. It's now posted. – DavidC – 2016-09-05T15:14:42.650

2Wow, that is a nice star – Beta Decay – 2016-09-05T15:18:05.157

3

Ruby, 82 bytes

->s{n=s.size-1
(r=-n..n).map{|i|(" "*k=i.abs)+r.map{|j|s[[k+j,k,-j].max]}*" "}*$/}

iterates through 1-n..n-1 in both i=y and j=x directions. Without the leading spaces on each line, the output looks like the below, as a result of picking a character from s with the index [[i.abs+j,i.abs,-j].max]. Adding leading spaces forms the required hexagon.

f f f f
f l l l f
f l o o l f
f l o G o l f
f l o o l f
f l l l f
f f f f

Ungolfed in test program

f=->s{
  n=s.size-1             n=string length - 1
  (r=-n..n).map{|i|      iterate from -n to n, build an array of lines
    (" "*k=i.abs)+       k=i.abs. Start each line with k spaces.
    r.map{|j|            iterate from -n to n, build an array of characters.
      s[[k+j,k,-j].max]  select character from s (store null string in array if index past end of string)
    }*" "                concatenate the array of characters into a line, separated by spaces
  }*$/                   concatenate the array of lines into a single string, separate by newlines
}

puts f[gets.chomp]

Typical output

   f f f f
  f l l l f
 f l o o l f
f l o G o l f
 f l o o l f
  f l l l f
   f f f f

Level River St

Posted 2016-09-05T12:25:52.017

Reputation: 22 049

3

JavaScript (ES6), 118 bytes

s=>[...Array((l=s.length-1)*2+1)].map((_,i,a)=>a.map((_,j)=>s[Math.max(i-l,l-i,j-l,i-j,l+l-i-j)]||``).join` `).join`\n`

Where \n represents the literal newline character. Based on my answer to Hexplosive ASCII-art challenge although parts of the solution resemble @LevelRiverSt's Ruby answer. The various components of the Math.max produce the following output for l=3:

    i - l           l - i           j - l            i - j        l + l - i - j
- - - - - - -   3 3 3 3 3 3 3   - - - 0 1 2 3    0 - - - - - -    6 5 4 3 2 1 0
- - - - - - -   2 2 2 2 2 2 2   - - - 0 1 2 3    1 0 - - - - -    5 4 3 2 1 0 -
- - - - - - -   1 1 1 1 1 1 1   - - - 0 1 2 3    2 1 0 - - - -    4 3 2 1 0 - -
0 0 0 0 0 0 0   0 0 0 0 0 0 0   - - - 0 1 2 3    3 2 1 0 - - -    3 2 1 0 - - -
1 1 1 1 1 1 1   - - - - - - -   - - - 0 1 2 3    4 3 2 1 0 - -    2 1 0 - - - -
2 2 2 2 2 2 2   - - - - - - -   - - - 0 1 2 3    5 4 3 2 1 0 -    1 0 - - - - -
3 3 3 3 3 3 3   - - - - - - -   - - - 0 1 2 3    6 5 4 3 2 1 0    0 - - - - - -

The maximum value is taken, and the values greater than l are removed, thus producing the hexagon shape, while the remaining values map to characters from the string:

6 5 4 3 3 3 3      3 3 3 3          a a a a
5 4 3 2 2 2 3     3 2 2 2 3        a x x x a
4 3 2 1 1 2 3    3 2 1 1 2 3      a x e e x a
3 2 1 0 1 2 3   3 2 1 0 1 2 3    a x e h e x a
4 3 2 1 1 2 3    3 2 1 1 2 3      a x e e x a
5 4 3 2 2 2 3     3 2 2 2 3        a x x x a
6 5 4 3 3 3 3      3 3 3 3          a a a a

Neil

Posted 2016-09-05T12:25:52.017

Reputation: 95 035

1

05AB1E, 31 bytes

R.pvy`¹gN-©×NFs.ø}Sðý®ð×ì})¦«»

Explanation

Utilizing symmetry to only generate the top part of the hexagon,
then mirroring that to form the lower part.

R.pv                     }       # for each prefix of the reversed string
                                 # ['f', 'fl', 'flo', 'floG']
    y`                           # split into chars, ex: 'f', 'l', 'o'
      ¹gN-©×                     # repeat the last char len(input)-N times, 
                                 # where N is the 0-based list index of the current prefix
                                 # ex: 'oo'
            NF   }               # N times do
              s.ø                # surround current char with the next char on stack
                                 # ex: 'floolf'
                  Sðý            # insert spaces between each letter, ex: 'f l o o l f'
                     ®ð×ì        # prefix string with len(input)-N spaces
                                 # ex: '  f l o o l f'
                          )      # wrap all strings in a list
                           ¦    # create a reversed copy of the list and 
                                 # remove the first item (as we only need the middle once)
                             «»  # concatenate the lists and merge with newlines

Try it online!

Emigna

Posted 2016-09-05T12:25:52.017

Reputation: 50 798

0

Python 2, 104 bytes

def f(s):
 for n in range(len(s)*2-1):x=abs(n-len(s)+1);print' '*x+' '.join(s[x+1:][::-1]+s[x]*x+s[x:])

Daniel

Posted 2016-09-05T12:25:52.017

Reputation: 6 425

0

PHP - 202 bytes

$w=$argv[1];$l=$i=$a=strlen($w)-1;while(-$l<=$i){$s=join(" ",str_split(str_repeat($w[$l],($a-1)/2).substr($w,$a?$a:1,$l+1),1));echo str_pad("",$a).strrev($s).($a%2?" ":" {$w[$a]} ")."$s
";$a=abs(--$i);}

Usage from command line:

php.exe -r "put the escaped code here" "put your desired word here"

for example:

php.exe -r "$w=$argv[1];$l=$i=$a=strlen($w)-1;while(-$l<=$i){$s=join(\" \",str_split(str_repeat($w[$l],($a-1)/2).substr($w,$a?$a:1,$l+1),1));echo str_pad(\"\",$a).strrev($s).($a%2?\" \":\" {$w[$a]} \").\"$s\n\";$a=abs(--$i);}" "example"

Test Suite.

nl-x

Posted 2016-09-05T12:25:52.017

Reputation: 306