R – Code Golf: Beehive

code-golflanguage-agnosticrosetta-stone

The challenge

The shortest code by character count that will generate a beehive from user input.

A beehive is defined a a grid of hexagons in a size inputted by the user as two positive numbers greater than zero (no need to validate input). The first number (W) represents the width of the beehive – or – how many hexagons are on each row. The second number (H) represents the height of the beehive – or – how many hexagons are on each column.

A Single hexagon is made from three ASCII characters: _, / and \, and three lines:

 __
/  \
\__/

Hexagons complete each other: the first column of the beehive will be 'low', and the second will be high – alternating and repeating in the same pattern forming W hexagons. This will be repeated H times to form a total of WxH hexagons.

Test cases:

Input:
    1 1
Output:
     __
    /  \
    \__/

Input:
    4 2
Output:
        __    __
     __/  \__/  \
    /  \__/  \__/
    \__/  \__/  \
    /  \__/  \__/
    \__/  \__/

Input:
    2 5
Output:
        __ 
     __/  \
    /  \__/
    \__/  \
    /  \__/
    \__/  \
    /  \__/
    \__/  \
    /  \__/
    \__/  \
    /  \__/
    \__/

Input:
    11 3
Output:
        __    __    __    __    __
     __/  \__/  \__/  \__/  \__/  \__
    /  \__/  \__/  \__/  \__/  \__/  \
    \__/  \__/  \__/  \__/  \__/  \__/
    /  \__/  \__/  \__/  \__/  \__/  \
    \__/  \__/  \__/  \__/  \__/  \__/
    /  \__/  \__/  \__/  \__/  \__/  \
    \__/  \__/  \__/  \__/  \__/  \__/

Code count includes input/output (i.e full program).

Best Answer

Perl, 99 characters

@P=map{$/.substr$".'__/  \\'x99,$_,$W||=1+3*pop}0,(3,6)x pop;
chop$P[0-$W%2];print"    __"x($W/6),@P

Last edit: Saved one character replacing -($W%2) with 0-$W%2 (thanks A. Rex)

Explanation:

For width W and height H, the output is 2+2 * H lines long and 3 * W+1 characters wide, with a lot of repetition in the middle of the output.

For convenience, we let $W be 3 * W + 1, the width of the output in characters.

The top line consists of the pattern " __", repeated W/2 == $W/6 times.

The even numbered lines consist of the repeating pattern "\__/ ", truncated to $W characters. The second line of output is a special case, where the first character of the second line should be a space instead of a \.

The odd numbered lines consist of the repeating pattern "/ \__", truncated to $W characters.

We construct a string: " " . "__/ \" x 99. Note that the beginning of this string is the desired output for the second line. This line starting at position 3 is the desired output for the odd lines, and starting at position 6 for the even numbered lines.

The LIST argument to the map call begins with 0 and is followed by H repetitions of (3,6). The map call creates a list of the substrings that begin at the appropriate positions and are $W = 3 * W + 1 characters long.

There is one more adjustment to make before printing the results. If W is odd, then there is an extra character on the second line ($P[0]) that needs to be chopped off. If W is even, then there is an extra character on the bottom line ($P[-1]) to chop.