57
\$\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
  • 19
    \$\begingroup\$ Why do we even have a hexagonal-grid tag? \$\endgroup\$ Commented Dec 26, 2016 at 7:25
  • 16
    \$\begingroup\$ Also, someone needs to write a hexagony solution. \$\endgroup\$ Commented Dec 26, 2016 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\$ Commented Jan 19, 2017 at 9:01
  • 7
    \$\begingroup\$ "Could you make me a hexagon please?" - sure, here you go: i.imgur.com/1emYIia.png \$\endgroup\$ Commented Jan 28, 2017 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\$ Commented Jun 28, 2018 at 5:25

64 Answers 64

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\$ Commented Jul 14, 2021 at 12:31
0
\$\begingroup\$

Knight, 58 57 bytes

;=n+~1P;=i~1W>+1*2n=i+1iO+*' '=xI>=x-n iFx~x*'* '+1-*2n x 

Try It Online!

\$\endgroup\$
0
\$\begingroup\$

AWK, 104 bytes

{for(;i++<=2*$1;print i<=$1?s[i]:s[$1-++x]){s[i]=sprintf("%"$1-i"s",X);for(k=$1+i-1;k--;)s[i]=s[i]"* "}} 

Attempt This Online!

\$\endgroup\$
1 2
3

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.