56
\$\begingroup\$

Today, we're going to make an ASCII hexagon. You must write a program or function that takes a positive integer n, and outputs a hexagon grid of size n, made up of asterisks. For example, a hexagon of size 2 looks like this:

 * *
* * *
 * *

While a hexagon of size 3 looks like this:

  * * *
 * * * *
* * * * *
 * * * *
  * * *

You may use any of the default input and output methods, for example STDIO/STDOUT, function arguments and return values or reading/writing a file.

You may assume that input is always valid, so if it's not a positive integer, your program may do whatever you want. You do however have to handle the special case of a size 1 hexagon, which happens to be a single asterisk:

*

Leading and trailing whitespace is allowed as long as the output is visually the same.

Examples:

1:
*

2:
 * *
* * *
 * *

3:
  * * *
 * * * *
* * * * *
 * * * *
  * * *

4:
   * * * *
  * * * * *
 * * * * * *
* * * * * * *
 * * * * * *
  * * * * *
   * * * *

5:
    * * * * *
   * * * * * *
  * * * * * * *
 * * * * * * * *
* * * * * * * * *
 * * * * * * * *
  * * * * * * *
   * * * * * *
    * * * * *

6:
     * * * * * *
    * * * * * * *
   * * * * * * * *
  * * * * * * * * *
 * * * * * * * * * *
* * * * * * * * * * *
 * * * * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * * * * * *
     * * * * * *

12:
           * * * * * * * * * * * *
          * * * * * * * * * * * * *
         * * * * * * * * * * * * * *
        * * * * * * * * * * * * * * *
       * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * * *
     * * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * * * * * *
   * * * * * * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * *
 * * * * * * * * * * * * * * * * * * * * * *
  * * * * * * * * * * * * * * * * * * * * *
   * * * * * * * * * * * * * * * * * * * *
    * * * * * * * * * * * * * * * * * * *
     * * * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * * *
       * * * * * * * * * * * * * * * *
        * * * * * * * * * * * * * * *
         * * * * * * * * * * * * * *
          * * * * * * * * * * * * *
           * * * * * * * * * * * *

As usual, this is , so standard loopholes apply, and you should try to write the shortest possible program measured in bytes. Of course, some languages are inherently shorter or longer than others, so remember that the goal is not necessarily to have the shortest overall byte count, but to beat submissions in the same or similar languages.

May the best golfer win!

\$\endgroup\$
5
  • 18
    \$\begingroup\$ Why do we even have a hexagonal-grid tag? \$\endgroup\$
    – Pavel
    Dec 26 '16 at 7:25
  • 15
    \$\begingroup\$ Also, someone needs to write a hexagony solution. \$\endgroup\$
    – Pavel
    Dec 26 '16 at 7:40
  • \$\begingroup\$ If anyone wants to go for the bounty, you can probably reuse the output loop of my Hexagony answer over here. \$\endgroup\$ Jan 19 '17 at 9:01
  • 7
    \$\begingroup\$ "Could you make me a hexagon please?" - sure, here you go: i.imgur.com/1emYIia.png \$\endgroup\$ Jan 28 '17 at 18:45
  • \$\begingroup\$ @Pavel because a lot of operations on a hexagonal grid are distinct from on the more standard square grid, and portable between solutions to different problems. Such operations as coordinate manipulation, rotation, output layout, etc. \$\endgroup\$
    – Sparr
    Jun 28 '18 at 5:25

62 Answers 62

1 2
3
0
\$\begingroup\$

Excel, 63 bytes

=LET(r,2*A1-1,x,ABS(A1-SEQUENCE(r)),REPT(" ",x)&REPT("* ",r-x))

Link to Spreadsheet

The above spills the results to multiple cells. The 75 byte formula for a single cells answer is below.

=LET(r,2*A1-1,x,ABS(A1-SEQUENCE(r)),CONCAT(REPT(" ",x)&REPT("* ",r-x)&"
"))
\$\endgroup\$
0
\$\begingroup\$

C (gcc), 164 125 124 123 bytes

i,h;main(z,b)int**b;{z=atoi(b[1]);for(i=z--+z;i--;puts(""))for(h=z<i?z-i+z:i,printf("%*s",z-h,""),h-=~z;h--;)printf("* ");}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ i completely didn't see arguments at all lol, also i forgot about puts lol \$\endgroup\$
    – mekb
    Jul 14 '21 at 12:31
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.