Expand a hexagon

24

2

Given an ASCII art hexagon as input, output one whose sides are all one unit longer.

                     _____
  ____              /     \
 /    \            /       \
/      \          /         \
\       \   =>    \          \
 \      /          \         /
  \____/            \       /
                     \_____/

The input hexagons will have 180 degree symmetry, but otherwise the sides can be all different lengths. Above, the side lengths (2, 4, 3) get enlarged to (3, 5, 4). The side lengths will be nonzero.

The hexagons are made of underscores _, slashes /, and backslashes \. Note that of the horizontal edges (made with underscores), the top edge is on its own line but the bottom edge is not.

I/O

I'm going to be a stickler and require formatting here: the input and output should be a string with newlines representing the image, not a list of lines. Of course, your code may print each line in turn to produce the image, or read STDIN a line a time for input if your language can do that.

Details

The input may include an optional trailing newline if you wish, but will otherwise have no empty lines. You can choose to either have no trailing spaces in the input, or spaces to pad each line to the same length (that of the longest line).

The output should be flush with the left edge of the screen, like the input. You may have extra newlines above and below as well as trailing spaces.

Test cases

Input followed by output.

 _
/ \
\_/

  __
 /  \
/    \
\    /
 \__/

  ____
 /    \
/      \
\       \
 \      /
  \____/

   _____
  /     \
 /       \
/         \
\          \
 \         /
  \       /
   \_____/

     _
    / \
   /  /
  /  /
 /  /
/  /
\_/

      __  
     /  \
    /    \
   /     /
  /     /
 /     /
/     /
\    /
 \__/

Leaderboard

<iframe src="https://xmikee1.github.io/ppcg-leaderboard/?id=185760" width="100%" height="100%" style="border: none;">Oops, your browser is too old to view this content! Please upgrade to a newer version of your browser that supports HTML5.</iframe><style>html,body{margin:0;padding:0;height:100%;overflow:hidden}</style>

xnor

Posted 2019-05-18T02:13:56.530

Reputation: 115 687

Must the output hexagon be the same one but bigger, or can it be any hexagon with the appropriate side lengths? – Stephen – 2019-05-18T02:47:42.093

1@Stephen It must be the same one but bigger, in the same orientation. – xnor – 2019-05-18T02:48:43.787

2This is a great example of a really well written challenge. Short, clear and to the point. May I make a small suggestion? "Stickler" may not be clear for all non native English speakers. May I suggest that "I will only accept..." or something similar may be easier to understand? +1 anyway. – ElPedro – 2019-05-18T20:43:45.197

Can we return an array/IEnumerable of characters? – Embodiment of Ignorance – 2019-05-19T01:23:00.427

@EmbodimentofIgnorance Yes, those are fine, as long as the have the required newline characters. – xnor – 2019-05-19T03:06:03.027

Answers

6

Stax, 28 bytes

╙Σ■♀♪«G[▀[TÖe╟╗ê'○▀ÄT→│╧(╡¢╩

Run and debug it

Seems like there should be a way to do it mostly with regex, but I'm still looking...

recursive

Posted 2019-05-18T02:13:56.530

Reputation: 8 616

5

JavaScript (ES6),  159 156 153  150 bytes

s=>s[r='replace'](/\S /g,'$&   ')[r](/.*/,s=>s[r](e=/_+/,` $&_
`+s[r](e,'/$& \\')[r](e=/_/g,' ')))[r](/ *\\_+/,s=>s[r](e,' ')+`   /
 `+s[r](/_/,'__'))

Try it online!

Commented

NB: Alternate characters are used below for the regex delimiters to prevent SE syntax highlighter from going berserk.

s =>                       // s = input
  s[r = 'replace'](        // r = alias for 'replace'
                           // STEP #1
    ∕\S ∕g,                // insert two middle spaces for all lines
    '$&   '                // that contain a border, followed by a space
  )                        // (i.e. all lines except the first and the last one)
  [r](                     // STEP #2
    ∕.*∕,                  // isolate the first line
    s =>                   // let s be this first line
      s[r](                //
        e = ∕_+∕,          // find the sequence of underscores and replace it with:
        ` $&_\n` +         //   the same sequence preceded by a space and followed by '_'
        s[r](              //   followed by a linefeed and:
          e,               //     the same sequence preceded by '/' and followed by ' \'
          '/$& \\'         //     
        )                  //     with:
        [r](e = ∕_∕g, ' ') //     all underscores replaced with spaces
  ))                       //
  [r](                     // STEP #3
    ∕ *\\_+∕,              // isolate the last line, without the trailing '/'
    s =>                   // let s be this last line
      s[r](e, ' ') +       // replace all underscores with spaces
      `   /\n ` +          // append 3 spaces and a trailing '/', followed by a linefeed
      s[r](∕_∕, '__')      // append s with an extra underscore
  )                        //

Arnauld

Posted 2019-05-18T02:13:56.530

Reputation: 111 334

5

Retina 0.8.2, 84 bytes

m`(¶.*)( .)$
$1   $2
(_+¶)(( */)  ( *.))
 _$1 $3$4¶$2
( *.)(_+/)$
$1$.2$*   /¶ $1_$2

Try it online! Works with irregular hexagons. I/O is unpadded. Explanation:

m`(¶.*)( .)$
$1   $2

Widen the interior of the hexagon.

(_+¶)(( */)  ( *.))
 _$1 $3$4¶$2

Fix up the top.

( *.)(_+/)$
$1$.2$*   /¶ $1_$2

Fix up the bottom.

Neil

Posted 2019-05-18T02:13:56.530

Reputation: 95 035

2

Charcoal, 48 bytes

SθW¬№ω_≔⁺ωSω≔⊕⊘№ω\η≔⊕⊘№ω/ζ≔×_⊕№ω_θ↗ζθ↓↘η←↙ζ↑←θ↖η

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

SθW¬№ω_≔⁺ωSω

Input and concatenate all of the lines except the first. (Input in JSON format would make most of this unnecessary at a saving of 11 bytes.)

≔⊕⊘№ω\η≔⊕⊘№ω/ζ≔×_⊕№ω_θ

Count the number of /s, /s, and _s in the string and use that to calculate the new side lengths (in the case of _s, as a string of _s of that length).

↗ζθ↓↘η←↙ζ↑←θ↖η

Draw the enlarged hexagon.

Alternative solution, also 48 bytes:

SθW¬№ω_≔⁺ωSω≔⁺θωθF²«≔E\/_⊕⊘№θκη×_⊟ηM⁰¬ιFη«↷¹κ↷¹¶

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

SθW¬№ω_≔⁺ωSω≔⁺θωθ

Input all of the lines. (Input in JSON format would make this unnecessary at a saving of 17 bytes.)

F²«

Draw the top right and bottom left sections of the hexagon separately.

≔E\/_⊕⊘№θκ

Count the number of /s, /s, and _s in the string and use that to calculate the new side lengths.

η×_⊟ηM⁰¬ι

Output the top or bottom, and move down a line if this was the top line.

Fη«↷¹κ↷¹¶

Draw both of the right or left sides.

Neil

Posted 2019-05-18T02:13:56.530

Reputation: 95 035

2

APL (Dyalog Unicode), 75 74 bytesSBCS

' /\_'∘{⍺[a×1=(+⍀×a)⌊⊖+⍀⊖×a←2⌈/{⊃0~⍨1⌷⍵,⍨⍉⍵}⌺3 3(0,0,⍨⍉)⍣3⍉⍺⍳↑⍵]}'.+'⎕s'&'

Try it online!

'.+'⎕s'&' split input into lines

↑⍵ mix lines into a matrix

⍺⍳ replace ' /\_' with 0 1 2 3

(0,0,⍨⍉)⍣3⍉ surround with a layer of 0s on top&bottom and two layers of 0s on the left&right

{⊃0~⍨1⌷⍵,⍨⍉⍵}⌺3 3 for each cell choose the first non-0 from: upper, lower, left, right from the 3x3 neighbourhood centred on it

2⌈/ max in pairs horizontally

a×1=(+⍀×a)⌊⊖+⍀⊖×a← keep only the outer boundary of non-0s

⍺[ ] replace 0 1 2 3 with ' /\_'

ngn

Posted 2019-05-18T02:13:56.530

Reputation: 11 449

1

PowerShell, 126 bytes

$args-replace' *\S +','$0   '-replace'( +)(_+)','$1 $2_
$1/$2 \'-replace'( *)(\\_+)/','$1$2   /
$1 $2_/'-replace'_(?=.* )',' '

Try it online!

mazzy

Posted 2019-05-18T02:13:56.530

Reputation: 4 832

0

Perl 5, 177 156 145 bytes

@l=pop=~/.+/g;splice@l,$_,0,$l[$_]=~s,_, ,gr for-1,1;$l[$_]=~s, (\S)( +)(\S),$_<2?" $1$2 $3":"$1$2   $3",e for 1..@l-2;join("\n",@l)=~s,_+,$&_,gr

Could be shorter? Don't see how just yet. With comments and added newlines and header+footer:

sub f {
@l=pop=~/.+/g;                                                      #1
splice@l,$_,0,$l[$_]=~s,_, ,gr for-1,1;                             #2
$l[$_]=~s, (\S)( +)(\S),$_<2?" $1$2 $3":"$1$2   $3",e for 1..@l-2;  #3
join("\n",@l)=~s,_+,$&_,gr                                          #4
}

Try it online!

Line #1 splits multi-line input string into the @l array.

Line #2 duplicates second and last lines without the _ chars.

Line #3 adds spaces where needed on line 2 to second last.

Line #4 widens the two ___ sides with one _ and returns the @l array of lines as one multi-line string.

Kjetil S.

Posted 2019-05-18T02:13:56.530

Reputation: 1 049