Embedded Hexagons!

18

1

Your task: given an integer n, generate an embedded hexagon pattern following the below rules, to the nth depth.

An embedded hexagon has the basic shape of this: (n=0)

 __      
/  \
\__/

Embedded Hexagons n=1 and n=2:

  ____    
 /  \ \
/\__/  \
\      /
 \____/

    ________    
   /  \ \   \
  /\__/  \   \
 /\      /    \
/  \____/      \
\              /
 \            /
  \          /
   \________/

The length of each side is 2 times the length of the same side in the previous depth times two. The top and bottom sides are 2 characters long when n=0 and the rest start out as 1 character long. The non top-bottom side lengths should be 2^n long (OEIS: A000079) and the top and bottom sides should follow the rule 2^(n+1) (same OEIS).

The current hexagons are 0-indexed, you may chose to use 1-indexed if you want.

This is , so the shortest answer wins!

Comrade SparklePony

Posted 2017-04-07T13:43:46.470

Reputation: 5 784

@LuisMendo Okay, I'll change the name. – Comrade SparklePony – 2017-04-07T14:10:21.417

It might be hard to handle big input (ex. 64). Is there a limit to n? – Matthew Roh – 2017-04-07T15:33:29.823

@SIGSEGV There is no limit to n. – Comrade SparklePony – 2017-04-07T16:12:51.820

1Would be amused to see an answer in Hexagony :)) – Mr. Xcoder – 2017-04-07T18:23:52.763

1

Heh, the turtle graphics of my Koch curve submission can do this too (only first function changed). Definitely too long for this, though :)

– Ørjan Johansen – 2017-04-07T18:25:40.787

@ØrjanJohansen A long answer is better than no answer! – Comrade SparklePony – 2017-04-07T18:46:37.980

I really like this challenge but am too tired to have a go now and I guess there will be some much better answers than I could make by the time I get a chance to have a go. Much fun to all who try :) – ElPedro – 2017-04-07T19:34:49.200

Answers

10

Charcoal, 40 29 bytes

11 bytes saved thanks to @Neil by changing the while loop to a for-loop amongst other tricks

FN«AX²ιβ×__β↓↘β←↙β↑←×__β↖β→↗β

Try it online!

Explanation (outdated)

This program starts with generating the largest hexagon, and then does the smaller ones one-by-one in a while loop (1-indexed). For reference, α is the input number, β is the variable that contains 2^(α-1) and ι is the iterating variable in the loop.

Nα                        # Take input and store in α
Wα«                       # While α do:
 ×_X²ι                    #  Write "_"*(2**ι); this forms the top edge of the hexagon
 ↓                         #  Go down
 AX²⁻ι¹β                 #  Assign 2**(ι-1) to β
 ↘β←                       #  Write \ β times in a SE direction (top right edge) and then go left
 ↙β↑                       #  Write / β times in a SW direction (bottom right edge) and then go up
 ←×_X²ι                   #  Write the bottom edge
 ↖β→↗β                    #  Just like before, write the top and bottom left edges
 A⁻α¹α                    #  Decrement α
                          # Now the pointer is at the top left corner of the hexagon,
                          # from where the other smaller hexagons will soon be generated

user41805

Posted 2017-04-07T13:43:46.470

Reputation: 16 320

I noticed there's no "Hello, World!" program for Charcoal yet. You should add it. – mbomb007 – 2017-04-07T21:09:25.547

@mbomb007 Wouldn't that just be a duplicate of the trivial "this language prints its source file if it contains no commands" answer? – Neil – 2017-05-25T20:58:40.700

I saved some bytes when I realised that ×_X²ι is the same as ×__β, and some more bytes by converting your into a , which also avoids having to store the input number. Try it online!.

– Neil – 2017-05-25T21:12:42.677

@Neil Thanks, that is pretty neat :) – user41805 – 2017-05-26T06:20:12.667

5

Haskell, 230 217 207 bytes

EDIT:

  • -13 bytes: @xnor saw that my # could be just max.
  • -10 bytes: And also that zipWith and p could be merged into a ? operator, and that I'd (somehow!) reimplemented replicate.

m takes an Integer and returns a String.

m n=unlines.foldr1 o$((2^n)&).(2^)<$>[0..n]
l&t|a<-c[l,2*t]" _",b<-[c[l-i,1,2*t+2*i-2,1,l-i]" / \\ "|i<-[1..t]]=a:b++r(r<$>o[a]b)
c=(concat.).z replicate
o=max?' '?""
f?e=z f.(++repeat e)
r=reverse
z=zipWith

Try it online!

How it works

  • m is the main function. It uses & to generate the hexagons with proper padding, then folds them together with o.
  • l&t generates a small hexagon of side length t, padded inside a large one of side length l, as a list of String lines.
    • a is the top line of the hexagon, with the underscores.
    • b is a list of the other lines in the upper half of the hexagon. The lines of b are centered in the padding, which is rectangular; this allows the next step to work.
    • The bottom half of the hexagon is a overlaid on top of b with o, then reversed (both order of lines and within each line).
  • c takes two arguments, a list of lengths and a string, and generates a string that has as many copies of each character in the original as the corresponding length, e.g. c[1,3,2]"abc" == "abbbcc". It is used in & to generate the lines.
  • o takes two arguments representing pictures as lists of lines, and overlays the first, smaller one on top of the second one.
    • It is used both to combine hexagons and to add the bottom to each hexagon.
    • It essentially works by using ? twice to pad the first picture with infinitely many spaces both downwards and rightwards, then zipping together corresponding characters with max, which selects the non-space character if there is one.
  • (f?e)l m pads a list l by appending infinitely many 'e' elements, then zips the resulting list and the list m with the f function.

Ørjan Johansen

Posted 2017-04-07T13:43:46.470

Reputation: 6 914

1Nice solution! I think (#) can be max. – xnor – 2017-04-08T03:11:29.850

1The zipping can be combined with p to save bytes: o=max?' '?"";f?e=z f.(++repeat e). Might be shorter pointfree. – xnor – 2017-04-08T03:38:28.670

2(\n->(<$[1..n])) is replicate. – xnor – 2017-04-08T03:47:16.553

@xnor replicate? Now that's just embarassing. I'm just too used to <$[1..n] or [1..n]>> nearly always winning. However, I don't see how to shorten ? further. I already tried making p pointfree and the ++ is just in the wrong spot, blowing up things with flip. – Ørjan Johansen – 2017-04-08T04:17:16.717

3

JavaScript (ES6), 258 bytes

f=(n,s=` `.repeat(1<<n),p=(n?f(n-1):`


`).replace(/(.*)\n/g,s+`$1 `+s)+s,t=`_`.repeat(2<<n))=>(s+t+s+`
`+s.replace(/ /g,"$'/$'$'  $`$`$`$`\\$'\n")).replace(/ /g,(c,i)=>p[i+(1<<n>>1)])+s.replace(/ /g,"$`\\$`$`  $'$'$'$'/$`\n").replace(/ +\/( *)\n$/,t+`/$1
`)
<input type=number min=0 oninput=o.textContent=f(+this.value)><pre id=o>

Explanation: For hexagons after the first, the previous hexagon is first generated and padded on each side (this relies on the output being a rectangle). (For the first heading, some dummy padding is created.) The top and top sides of the hexagon are generated, and all the spaces merged with the previous hexagon. (There's some trickery in order to get the hexagons to line up; this would be easier if extra margins were allowed.) The bottom sides of the hexagon are generated analogously to the top sides, and the bottom of the hexagon is then filled in. Care has to be taken to return rectangular output, including a trailing newline, for the recursion to work.

Neil

Posted 2017-04-07T13:43:46.470

Reputation: 95 035

So, you're demonstrating that this, the Teflon one, and the Deep Dish Pizza one, are all really similar construction? That's kinda neat. – AdmBorkBork – 2017-04-07T20:37:23.297

1@AdmBorkBork I have a few other answers that do that; those diagonal /s are popular in ASCII art and the replace method is a relatively cheap way of generating them in JavaScript. – Neil – 2017-04-07T20:45:18.633

1<<n>>1: Nice symmetry ;-) – Luke – 2017-04-08T20:51:30.360

@Luke I could change the variable to, say, v but sadly the 1 isn't symmetric in any of my usual fonts. – Neil – 2017-04-08T20:55:42.577

2

PHP, 337 Bytes

0 Indexing

$h=array_fill(0,1+2*$v=2**($c=$argn),str_pad("",4*$v));for(;1+$c;$c--)for($i=0;$i<$e=2**$c*2+1;$i++){$z=$e-$i<2;$y=$i&&$i<$e/2;$x=$i>=$e/2&$i<$e-1;$h[$i]=substr_replace($h[$i],$s=str_pad(!$y?$z|$x?"\\":"":"/",$e-1+$z-$y+$y*$i*2-$x+$x*2*($e-$i),$z|!$i?"_":" ").(!$y?$z|$x?"/":"":"\\"),$v-$z-$y*$i-$x*($e-$i),strlen($s));}echo join("\n",$h);

Try it online!

Expanded

$h=array_fill(0,1+2*$v=2**($c=$argn),str_pad("",4*$v)); # fill array with maximal width
for(;1+$c;$c--)  # greatest hexagon to lowest
for($i=0;$i<$e=2**$c*2+1;$i++){ # loop through the rows
    $z=$e-$i<2;$y=$i&&$i<$e/2;$x=$i>=$e/2&$i<$e-1; # booleans last first ad second half
    $h[$i]=substr_replace($h[$i], # replace substring
    $s=str_pad(
        $z?"\\":($y?"/":($x?"\\":"")),
        $e-1+$z-$y+$y*$i*2-$x+$x*2*($e-$i),
        $z|!$i?"_":" "
        ).(!$z?!$y?$x?"/":"":"\\":"/"), # with string for smaller hexagon
    $v-$z-$y*$i-$x*($e-$i), # at offset
    strlen($s));
}
echo join("\n",$h); # Output

Jörg Hülsermann

Posted 2017-04-07T13:43:46.470

Reputation: 13 026