Hexplosive ASCII-art challenge

20

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!

Leaderboards

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

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# 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

var QUESTION_ID=92194,OVERRIDE_USER=31716;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

James

Posted 2016-09-04T04:28:57.447

Reputation: 54 537

1Related (but asking about the process rather than the number of neighbours). – trichoplax – 2016-09-04T22:16:13.763

1I'm tempted to learn Hexagony just for the sake of this challenge. ;) – Kevin Cruijssen – 2016-09-05T09:47:01.277

Answers

11

MATL, 39 37 bytes

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

Try it online! Or verify all test cases.

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  

Luis Mendo

Posted 2016-09-04T04:28:57.447

Reputation: 87 464

15

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.

Neil

Posted 2016-09-04T04:28:57.447

Reputation: 95 035

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 – 2016-09-04T16:52:50.650

@Arnauld I like that 3+!i%n! – Neil – 2016-09-04T18:45:01.200

7

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])

Jonathan Allan

Posted 2016-09-04T04:28:57.447

Reputation: 67 804

4

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...

Sp3000

Posted 2016-09-04T04:28:57.447

Reputation: 58 729

3

Java, 375 363 361 339 329 317 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.

Master_ex

Posted 2016-09-04T04:28:57.447

Reputation: 526

1I 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 – 2016-09-05T09:55:37.093

@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 – 2016-09-05T21:48:31.110

2

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!

Additional solution, also 44 bytes:

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

Emigna

Posted 2016-09-04T04:28:57.447

Reputation: 50 798

2

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]

Level River St

Posted 2016-09-04T04:28:57.447

Reputation: 22 049

1

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

James

Posted 2016-09-04T04:28:57.447

Reputation: 54 537

1

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))))

rnso

Posted 2016-09-04T04:28:57.447

Reputation: 1 635