# Hexplosive ASCII-art challenge

In the strategy game "Hexplode", players take turns placing tokens on a hexagonal board. Once the number of tokens equals the number of adjacent tiles, that tile hexplodes, and moves all of the tokes on it to the surrounding neighbors. You can play the game online here.

I like this game, but sometimes it's hard to know exactly how many tokens go on a specific tile; I'm always counting the number of neighbors. It would really convenient if I had an ASCII-art to help me remember how many tokens go on each tile.

You need to write a program or function that takes a positive integer as input, and produces this ASCII representation of hexagon of size N. Each tile will be the number of neighbors that tile has. Since 1 is a weird corner case with zero neighbors, you only have to handle inputs larger than 1.

You may take this number in any reasonable format, such as STDIN, function arguments, command-line arguments, from a file, etc. The output may also be in any reasonable format, such as printing to STDOUT, writing to a file, returning a list of strings, a newline separated string, etc.

Here is some sample output for the first 5 inputs:

2)

3 3
3 6 3
3 3

3)

3 4 3
4 6 6 4
3 6 6 6 3
4 6 6 4
3 4 3

4)

3 4 4 3
4 6 6 6 4
4 6 6 6 6 4
3 6 6 6 6 6 3
4 6 6 6 6 4
4 6 6 6 4
3 4 4 3

5)

3 4 4 4 3
4 6 6 6 6 4
4 6 6 6 6 6 4
4 6 6 6 6 6 6 4
3 6 6 6 6 6 6 6 3
4 6 6 6 6 6 6 4
4 6 6 6 6 6 4
4 6 6 6 6 4
3 4 4 4 3

6)

3 4 4 4 4 3
4 6 6 6 6 6 4
4 6 6 6 6 6 6 4
4 6 6 6 6 6 6 6 4
4 6 6 6 6 6 6 6 6 4
3 6 6 6 6 6 6 6 6 6 3
4 6 6 6 6 6 6 6 6 4
4 6 6 6 6 6 6 6 4
4 6 6 6 6 6 6 4
4 6 6 6 6 6 4
3 4 4 4 4 3

And the pattern continues in a similar manner. As usual, standard loopholes apply, and the answer with the lowest byte-count will be crowned the winner!

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

# Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

• Related (but asking about the process rather than the number of neighbours). – trichoplax Sep 4 '16 at 22:16
• I'm tempted to learn Hexagony just for the sake of this challenge. ;) – Kevin Cruijssen Sep 5 '16 at 9:47

# MATL, 39 37 bytes

4*3-:!G:+o~YRtP*!tPw4LY)vtI5&lZ+47+*c

### Explanation

I get to use convolution again!

Consider input n = 3. The code first builds a matrix of size 4*n-3×n by adding the column vector [1; 2; ...; 9] to the row vector [1, 2, 3] with broadcast. This means computing a 2D array array of all pairwise additions:

2  3  4
3  4  5
4  5  6
5  6  7
6  7  8
7  8  9
8  9 10
9 10 11
10 11 12

Replacing even numbers by 1 and odd numbers by 0 gives the checkerboard pattern

1 0 1
0 1 0
1 0 1
0 1 0
1 0 1
0 1 0
1 0 1
0 1 0
1 0 1

This will be used for generating (part of) the hexagonal grid. Ones will represent points in the grid, and zeros will represent spaces.

The upper-right corner is removed by zeroing out all entries above the main "diagonal" of the matrix:

1 0 0
0 1 0
1 0 1
0 1 0
1 0 1
0 1 0
1 0 1
0 1 0
1 0 1

Element-wise multiplying this matrix by a vertically flipped version of itself removes the lower-right corner as well. Transposing then gives

1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
0 0 1 0 1 0 1 0 0

This begins to look like a hexagon. Using symmetry, the grid is extended to produce the upper half:

0 0 1 0 1 0 1 0 0
0 1 0 1 0 1 0 1 0
1 0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1 0
0 0 1 0 1 0 1 0 0

Now we need to replace each entry equal to one by the number of neighbours. For this we use convolution with a 3×5 neighbourhood (that is, the kernel is a 3×5 matrix of ones). The result,

2 3 4 5 5 5 4 3 2
4 5 7 7 8 7 7 5 4
4 6 7 8 7 8 7 6 4
4 5 7 7 8 7 7 5 4
2 3 4 5 5 5 4 3 2

has two issues (which will be solved later):

1. The values have been computed for all positions, whereas we only need them at the positions of the ones in the zero-one grid.
2. For each of those positions, the neighbour count includes the point itself, so it is off by 1.

The code now adds 47 to each computed value. This corresponds to subtracting 1 to solve issue (2) and adding 48 (ASCII for '0'), which converts each number to the code point of its corresponding char.

The resulting matrix is then multiplied by a copy of the zero-one grid. This solves issue (1) above, making the points that are not part of the hexagonal grid equal to zero again:

0  0 51  0 52  0 51  0  0
0 52  0 54  0 54  0 52  0
51  0 54  0 54  0 54  0 51
0 52  0 54  0 54  0 52  0
0  0 51  0 52  0 51  0  0

Finally, this array of numbers is cast to a char array. Zero chars are displayed as space, which gives the final result:

3 4 3
4 6 6 4
3 6 6 6 3
4 6 6 4
3 4 3

## JavaScript (ES6), 118 117 bytes

n=>[...Array(m=n+--n)].map((_,i,a)=>a.map((_,j)=>(k=j-(i>n?i-n:n-i))<0?:k&&++j<m?i/2%n?6:4:3+!i%n).join ).join\n

Where \n represents a literal newline character. Explanation: Suppose n=4. We start with the following space-separated digit square:

0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0

The first |n-i| 0s are deleted, but the spaces remain:

0 0 0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0
0 0 0 0

Instant hexagon! It then suffices to calculate the appropriate value in place of each 0 by checking whether we're on the first or last row and/or column. Edit: Saved 1 byte thanks to @Arnauld.

• Using some of your formula, I came to a 107 bytes version with for / console.log(): n=>{for(i=n+--n;i--;)console.log(' '.repeat(l=i>n?i-n:n-i)+(j=3+!l%n)+ ${l-n?6:4}.repeat(2*n-l-1)+' '+j)} – Arnauld Sep 4 '16 at 16:52 • @Arnauld I like that 3+!i%n! – Neil Sep 4 '16 at 18:45 # Python 2, 125 123 bytes def h(n):m=n-1;t=[' '*(m-r)+' '.join(('46'[r>0]*(r+m-1)).join('34'[r%m>0]*2))for r in range(n)];print'\n'.join(t+t[-2::-1]) Tests are on ideone Runs through the top to the middle rows, for r in range(n), constructing strings: - making two corners or two edges, '34'[r%m>0]*2; - filling by joining them with repeated '6' or '4', '46'[r>0]*(r+m-1); - joining the corners and edges with ' '; - prepending with spaces, ' '*(m-r); Then prints this and it's reflection in the middle row joined by new lines, print'\n'.join(t+t[-2::-1]) ## Python 2, 96 bytes n=input();m=n-1 while n+m:n-=1;j=abs(n);c='34'[0<j<m];print' '*j+c+' '+'46 '[j<m::2]*(2*m+~j)+c This looks pretty messy and somewhat golfable... # Java, 375363361339329317 293 bytes interface J{static void main(String[]r){int i=0,k,h=Integer.decode(r[0]),a=1,l,n=0;for(;i++<h*2-1;n+=a){if(n==h-1)a=-1;String s="";for(k=0;k<n+h;k++,s+=" ")s+=n==0?k==0||k==n+h-1?3:4:k!=0&&k!=n+h-1?6:n==h-1?3:4;l=(h*4-3-s.trim().length())/2;System.out.printf((l==0?"%":"%"+l)+"s%s\n","",s);}}} Ungolfed interface J { static void main(String[] r) { int i = 0, k, h = Integer.decode(r[0]), a = 1, l, n = 0; for (; i++ < h * 2 - 1; n += a) { if (n == h - 1) { a = -1; } String s = ""; for (k = 0; k < n + h; k++, s += " ") { s += n == 0 ? k == 0 || k == n + h - 1 ? 3 : 4 : k != 0 && k != n + h - 1 ? 6 : n == h - 1 ? 3 : 4; } l = (h * 4 - 3 - s.trim().length()) / 2; System.out.printf((l == 0 ? "%" : "%" + l) + "s%s\n", "", s); } } } Usage:$ java J 5
3 4 4 4 3
4 6 6 6 6 4
4 6 6 6 6 6 4
4 6 6 6 6 6 6 4
3 6 6 6 6 6 6 6 3
4 6 6 6 6 6 6 4
4 6 6 6 6 6 4
4 6 6 6 6 4
3 4 4 4 3

I am sure that the horrible nested if-else block can be rewritten to be smaller but I can't figure it out at the moment. Any suggestions are welcome :-)

Update

• Followed Kevin Cruijssen's suggestion and used decode instead of parseInt.
• Rewrote some ifs by using the ternary operator.
• More ternary operators.
• Moar ternary operators! I think I have created a monster!
• Rewrote the if-else block regarding the printing.
• I haven't looked that closely at the method you use itself, but some small golfing tips for your current code: Integer.parseInt can be golfed to Integer.decode. l=(h*4-3-s.trim().length())/2;if(l==0) can be golfed to if((l=(h*4-3-s.trim().length())/2)==0). Also, it's completely acceptable to just post a method without class (unless the question states otherwise), so void f(int i){...use i...} instead of interface J{static void main(String[]r){...i=Integer.decode(r[0])...use i...}, that should save you quite a few bytes as well. When I have more time I'll look further. – Kevin Cruijssen Sep 5 '16 at 9:55
• @KevinCruijssen: Thank you for your suggestions. l=(h*4-3-s.trim().length())/2;if(l==0) is actually the same length with if((l=(h*4-3-s.trim().length())/2)==0). – Master_ex Sep 5 '16 at 21:48

# 05AB1E, 44 bytes

FN_i4ë6}ð«¹ÍN+×ðìN_N¹<Q~i3ë4}.ø¹<N-ð×ì})Â¦«»

Explanation

As the top and bottom of the hexagon are mirrored we only need to generate the upper part.
So for an input of X we need to generate X rows. That is what the main loop does.

F                                        }

Then we do the center part of the rows.
This is 4 for the first row and 6 for the rest (as we're only doing the upper part).
We concatenate this number with a space as the pattern will require spacing between numbers.

N_i4ë6}ð«

We then repeat this string X-2+N times, where N is the current row 0-indexed and prepend a space character on the left side.

¹ÍN+×ðì

After this it's time for the corners. They will be 3 for the first and last row and 4 for the middle rows.

N_N¹<Q~i3ë4}.ø

Now we need to make sure the rows are lined up correctly by adding spaces to the front of each row. The number of spaces added will be X-1-N.

¹<N-ð×ì

Now that we have the upper part of the grid done, we add the rows to a list, create a reversed copy and remove the first item from that copy (as we only need the central row once), then merge these 2 lists together and print.

)Â¦«»

Try it online!

ÍÅ10.øvN_i4ë6}ð«¹ÍN+×ðìyi4ë3}.ø¹<N-ð×ì})Â¦«»

# Ruby, 87 bytes

Anonymous function takes n as argument and returns an array of strings.

->n{(1-n..n-=1).map{|i|j=i.abs
" "*j+(e=j%n>0?"4 ":"3 ")+["6 ","4 "][j/n]*(2*n-1-j)+e}}

Ungolfed in test program

Input through stdin. Writes the whole shape to stdout. Pretty self explanatory.

f=->n{
(1-n..n-=1).map{|i|            #reduce n by 1 and iterate i from -n to n
j=i.abs;                     #absolute magnitude of i
" "*j+                       #j spaces +
(e=j%n>0?"4 ":"3 ")+         #start the string with 3 or 4 +
["6 ","4 "][j/n]*(2*n-1-j)+  #2*n-1-j 6's or 4`s as appropriate +
e                            #end the string with another 3 or 4
}
}

puts f[gets.to_i]

# V, 60 bytes

Àé x@aA4 xr3^.òhYpXa 6^òkyHç^/:m0
Pç 3.*6/^r4$. òÍ6 4./6 Try it online! This is really way too long. Here is a hexdump, since this contains unprintable characters: 0000000: c0e9 2078 4061 4134 201b 7872 335e 2ef2 .. x@aA4 .xr3^.. 0000010: 6859 7058 6120 361b 5ef2 6b79 48e7 5e2f hYpXa 6.^.kyH.^/ 0000020: 3a6d 300a 50e7 2033 2e2a 362f 5e72 3424 :m0.P. 3.*6/^r4$
0000030: 2e0a f2cd 3620 9334 852e 2f36            ....6 .4../6

# Racket, 487 bytes

(λ(n c e o)(let((sp(append(range(- n 1)-1 -1)(reverse(range(- n 1)0 -1))))
(mm(append(range(- n 2)(-(+ n(- n 1))2))(range(-(+ n(- n 1))2)(-(- n 1)2)-1)))
(r""))(for((i sp)(j mm))(define str"")(for((ss i))(set! str(string-append str" ")))
(set! str(string-append str(if(or(= i 0)(= i(- n 1))(= i(* 2(- n 1))))c e)" "))
(for((jj j))(set! str(string-append str(if(= j(- n 2))e o)" ")))(set! r(if(or(= i 0)
(= i(- n 1))(= i(* 2(- n 1))))c e))(set! str(string-append str r))(displayln str))))

Testing:

(f 4 "3" "4" "6")

3 4 4 3
4 6 6 6 4
4 6 6 6 6 4
3 6 6 6 6 6 3
4 6 6 6 6 4
4 6 6 6 4
3 4 4 3

(f 5 "o" "*" "-")

o * * * o
* - - - - *
* - - - - - *
* - - - - - - *
o - - - - - - - o
* - - - - - - *
* - - - - - *
* - - - - *
o * * * o

Detailed version:

(define(f1 n c e o)
(let ((sp(append(range(sub1 n) -1 -1)
(reverse(range(sub1 n) 0 -1))))
(mm(append(range(- n 2)(-(+ n(sub1 n)) 2))
(range(-(+ n(sub1 n)) 2)(-(sub1 n)2) -1) ))
(r ""))
(for((i sp)(j mm))
(define str "")
(for((ss i))(set! str(string-append str " ")))
(set! str(string-append str
(if(or(= i 0)(= i(sub1 n))
(= i(* 2(sub1 n)))) c e)
" "))
(for((jj j))
(set! str(string-append str
(if(= j(- n 2)) e o)
" ")))
(set! r(if(or(= i 0)
(= i(sub1 n))
(= i(* 2(sub1 n)))) c e))
(set! str(string-append str r))
(displayln str))))