# Simulating Bombs

The aim of this puzzle is to simulate the impact of the shock wave of bombs on a square grid. The grid initially contains cells labelled 0 along with several bombs of type A, H, or B. Display the greatest intensity of shockwaves felt at each cell after the bombs explode.

The three bombs have different explosion patterns. Consider the following generic configuration, where the X will be replaced by the different types of bombs. Note that for each bomb, the bomb's location is not replaced by a number.

000000000
000000000
000000000
000000000
0000X0000
000000000
000000000
000000000
000000000


For the A-bomb:

000000000
011111110
012222210
012333210
0123A3210
012333210
012222210
011111110
000000000


For the H-bomb:

000000000
055555550
055555550
055555550
0555H5550
055555550
055555550
055555550
000000000


For the B-bomb:

000000000
000010000
000020000
000030000
0123B3210
000030000
000020000
000010000
000000000


Unlike the other bombs, the B-bomb will not explode unless it is within the shockwave of another exploding bomb

## Input & Output

• You are given an integer n, representing the grid size of n*n
• You are then given the grid of n*n
• You may take in the input as a list of n lines
• Output the shock waves of all bombs
• You may output as a matrix/list/array

## Test cases

1
H
->
H

15
000000000000000
A00A0000000000A
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
0000000A0000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
A000000000000AA
->
333332100001233
A33A3210000123A
333332100001233
222222100001222
111111100001111
000011111110000
000012222210000
000012333210000
0000123A3210000
000012333210000
000012222210000
111111111111111
222100000012222
332100000012333
A321000000123AA

18
000000000000000000
0AAAA00000000AAAA0
000000000000000000
000000000000000000
000000000000000000
000000000000000000
AA0000000000000000
000000000000000000
000000000000000000
000000000000000000
00000000000000A000
000000000000000000
000000000000000000
00000A000000000000
000000000000000000
000000000000000000
00000000000000000A
A00A000A0000A00000
->
333333210012333333
3AAAA32100123AAAA3
333333210012333333
222222210012222222
222211110011111111
333210000000000000
AA3210000000000000
333210000001111111
222210000001222221
111110000001233321
00111111100123A321
001222221001233321
001233321001222221
00123A321001111111
111233321111111222
222222222222222233
33333233322333223A
A33A323A3223A32233

18
H00000000000000000
000000000000000H00
000000000000000000
00000000H000000000
000000000000000000
000000000000000000
000000000000000000
000000HH0000000000
000000000000000000
000000000000000000
000000000000000H00
000000000000000000
000000000000000000
000000000000000000
HHH00000000H000000
000000000000000000
000000000000000000
000000000000000000
->
H55505555555555555
555505555555555H55
555505555555555555
55550555H555555555
000555555555555555
000555555555000000
000555555555000000
000555HH5550555555
000555555550555555
000555555550555555
000555555550555H55
555555005555555555
555555005555555555
555555005555555555
HHH55500555H555000
555555005555555000
555555005555555000
555555005555555000

22
000000000000000000000A
00000B0000000000000000
0000000000000000000000
0B0000000000000000000B
000000000000000000B000
0000000000000000000000
000000000000000000000B
0000000000000000000000
000000B00000000A000000
000000000000000000000B
0000000000000000000000
000000B000000000000000
000000000000000000000B
0000000000000000000000
000000000H000000000000
000000000000000000000B
0000000000000000000000
A000000000000000000000
A00000000000B000000000
A000000000000000000000
HHHH00000000000000A00H
00000000A0000000000000
->
000000000000000000123A
00000B0000000000001233
0000000000000000001223
0B0000000000000000123B
000000000000000000B003
0000001000001111111003
000000200000122222123B
0000003000001233321003
000123B32100123A321003
000000300000123332123B
0000003000001222221003
000123B555555111111003
000000555555500000123B
0000005555555000000003
111100555H555000000003
222100555555500000123B
3321005555555000000003
A555555555555001115555
A55555511111B001225555
A555555222210001235555
HHHH55533321000123A55H
55555553A3210001235555

10
0000000000
0A00000000
0000000000
000B00B000
0000000000
000B00B00B
0000000000
0B00B00000
0000000000
0000000000
->
3332101000
3A32102000
3333103001
223B33B322
1113103003
123B33B33B
0003003003
0B02B02002
0001001001
0000000000


## Others

¤ The place may already have felt the effect of other bombs before the grid bombs exploded. In this case the number of the cell will be different from 0 as soon as the grid is given

¤ There are only three types of bomb: A, B, H

¤ There may be no explosion

¤ For overlapping of bomb explosions, please note that the highest intensity will outshine the lower intensity shock waves. E.g. a shockwave of 3 emitted by the A-bomb, will be replaced by a 5 if there is a H-bomb nearby

¤ For the order of checking the bombs, you can do a left to right, top to bottom checking system for easy checking (the 4th rule ensures overlapping will not cause any problems)

## Scoring

This is code-golf, you know the rules ;)

## Credits

Credits to this puzzle

• Is the grid size input (first input n) optional? Many languages don't need it (since it's implicit in the grid anyway), and might just have to spend extra bytes getting rid of it. Commented Feb 16, 2022 at 3:50
• ...how is this balanced-string? Commented Feb 16, 2022 at 3:51
• @SundarR Ignoring inputs (i.e. not taking them in the first place) is allowed by default. Commented Feb 16, 2022 at 4:26
• Suggested testcase: HHHH\nHHHH\nHHHH\nHHHH, output should be itself. HA00\nHA00\nHB00\nHB00, output should be HA33\nHA33\nHB33\nHB33.
– tsh
Commented Feb 16, 2022 at 5:24
• @tsh Shouldn't all 3 be 5 in that second test case you suggested (rule "For overlapping of bomb explosions, please note that the highest intensity will outshine the lower intensity shock waves."). Commented Feb 16, 2022 at 8:41

# Java 10, 312310308 307 bytes

n->m->{for(int i=n*n,b;i-->0;)if((b=m[i/n][i%n])>57)e(m,i/n,i%n,b);}int e(char[][]m,int x,int y,int b){for(int j=-4,k,u,r,X,Y;++j<4;m[x][y]*=b/~-b)for(k=-4;++k<4;)try{u=m[X=x-j][Y=y-k];r=b>67?53:52-Math.max(j<0?-j:j,k<0?-k:k);m[X][Y]+=b==j*k|b%7==2?u>r?u==66?e(m,X,Y,0):0:r-u:0;}finally{continue;}return 0;}


-2 bytes thanks to @Neil.
-1 byte thanks to @ceilingcat.

Input as a matrix of characters, and it modifies it directly. Also takes the dimension n as additional input, since it's shorter.

Try it online.

Explanation:

n->m->{                  // Method with integer & character-matrix parameters
for(int i=n*n,b;i-->0;)//  Loop i over all cells in the range (n²,0]:
if((b=m[i/n][i%n])   //   Save the current cell's value in b
>57)              //   If b is a bomb:
e(m,i/n,i%n,b);}   //    Explode it with the method below

// Separated recursive method to explode a bomb
// Takes the matrix, current x,y, and b as parameters (and always returns 0)
int e(char[][]m,int x,int y,int b){
for(int j=-4,k,u,r,X,Y;++j<4;
//  Loop j in the range (-4,4):
m[x][y]*=b/~-b)        //    If the current cell is an exploding B-bomb:
//     Remove the B-bomb so we know it's exploded
//    Else:
//     Keep the cell's value unchanged
for(k=-4;++k<4;)     //   Inner loop k in the range (-4,4) as well:
try{u=m[X=x-j]     //    Set X to x-j
[Y=y-k]     //    Set Y to y-k
;    //    Set u to the value at cell X,Y
r=b>67?            //    If we're exploding an H-bomb:
53              //     Set r to 53 (unicode value for 5)
:                //    Else:
52-             //     Set r to 52, subtracted by:
Math.max(    //      The max of:
j<0?-j:j,  //       The absolute value of j
k<0?-k:k); //       and the absolute value of k
m[X][Y]+=          //    Change the value at cell X,Y to:
b==j*k          //     If we're exploding a B-bomb
//     and either j or k is 0
//     (which means X,Y shares a row/column with x,y)
|                //     OR
b%7==2?         //     We're exploding an A- or H-bomb:
u>r?           //      If the current value u is larger than r:
u==66?       //       If u is a B-bomb that has yet to explode:
e(m,X,Y,0)  //        Do a recursive call with b=0 to explode it
:0           //        And keep the current cell's value unchanged
:              //      Else (u is smaller than r):
r-u           //       Change the current cell's value to r
:                //     Else (it's an already exploded B-bomb or digit):
0;              //      Keep the current cell's value unchanged
}finally{continue;}//    Catch and ignore ArrayIndexOutOfBoundsExceptions
//    (which is shorter than manual in-bounds checks)
return 0;}             //  Always return 0

• finally{continue;} saves a couple of bytes.
– Neil
Commented Feb 17, 2022 at 3:38
• @Neil Ah of course, thanks! Commented Feb 17, 2022 at 7:40
• @ceilingcat Well, basically the same response as to Neil: Ah of course, thanks! :) Commented Feb 19, 2022 at 12:29

# JavaScript (ES7), 180 bytes

Modifies the input matrix.

f=(m,X,Y,t)=>m.map((r,y)=>r.map((c,x)=>t?(d=4-((h=(x-X)**2)>(v=(y-Y)**2)?h:v)**.5)>0?(n=t==b?h*v?0:d:t<b?d:5)?r[c==b&&f(m,x,y,c,r[x]=f),x]=c<n?n:c:0:0:1/c||c==b||f(m,x,y,c)),b='B')


Try it online!

# Charcoal, 118 bytes

ＷＳ«ＦAHＦ⌕Ａικ⊞υ⟦ⅉλκ⟧⟦ι⟧»Ｆυ«≔§ι⁰ζ≔§ι¹ε≔§ι²δＦ⁷Ｆ⁷¿¬∧∧⁻κ³⁻λ³⁼Bδ«Ｊ⁺ε⁻λ³⁺ζ⁻κ³¿⁼BＫＫＦ¬№υ⟦ⅉⅈB⟧⊞υ⟦ⅉⅈB⟧¿⁼ＫＫＩΣＫＫ⎇⁼Hδ5⌈⟦ＫＫＩ⁻⁴⌈↔⁻⟦κλ⟧³


Try it online! Link is to verbose version of code. Takes input as a list of newline-terminated strings. Explanation:

ＷＳ«


Loop over the rows of the grid.

ＦAHＦ⌕Ａικ⊞υ⟦ⅉλκ⟧


Collect the positions of the As and Hs.

⟦ι⟧


Print the row to the canvas on its own line.

»Ｆυ«


Loop over the positions of the letters.

≔§ι⁰ζ≔§ι¹ε≔§ι²δ


Extract the current position and letter.

Ｆ⁷Ｆ⁷


Process a square of size 7 centred on the current position.

¿¬∧∧⁻κ³⁻λ³⁼Bδ«


Skip this cell if the current letter is B and the cell isn't orthogonal to the current position.

Ｊ⁺ε⁻λ³⁺ζ⁻κ³


¿⁼BＫＫＦ¬№υ⟦ⅉⅈB⟧⊞υ⟦ⅉⅈB⟧


If this cell is a B that hasn't been seen before then add it to the list of letter positions.

¿⁼ＫＫＩΣＫＫ⎇⁼Hδ5⌈⟦ＫＫＩ⁻⁴⌈↔⁻⟦κλ⟧³


If this cell is a digit then calculate the shock wave and update the cell if it is greater than its current value.

# Python3, 747 bytes:

from math import*
R=range
L=len
p={'A':(k:=[3,2,1],1),'H':([5]*3,1),'B':(k,0)}
g=lambda x,i:[j[i] for j in x]
m=lambda x,y,X,Y:max(map(abs,[X-x,Y-y]))
def u(b,x,y,l,r,o,Q,i=0,H=0):
if i and(j:=b[x][y])not in p and int(j)<l[(P:=m(*o[0],x,y)-1)]:b[x][y]=l[P]
if i and j=='B' and m(*o[0],x,y)-1 < 3:Q.append((x,y))
for q,w in[(0,1),(1,0),(0,-1),(-1,0)]:
if(X:=x+q)>=0 and(Y:=y+w)>=0 and (X,Y)not in o and(r or 1 in[L(set(g(O:=o+[(X,Y)],0))),L(set(g(O,1)))]):
try:
if(K:=dist(o[0],(X,Y)))>=H:u(b,X,Y,l,r,o+[(X,Y)],Q,1,K)
except:1
def f(b):
Q,S=[],[]
for x in R(L(b)):
for y in R(L(b[0])):
if(E:=b[x][y])in['A','H']:u(b,x,y,*p[E],[(x,y)],Q)
while Q:
S+=Q;q=[]
for i in Q:u(b,*i,*p['B'],[i],q)
Q=[j for j in q if j not in S]


Try it online!

# Python 3.8, 457 bytes

import itertools as t
g={}
b={}
R=range(int(input()))
for i in R:
for j,v in enumerate(input()):
g[i,j]=v
if v in"AH":b[i,j]=v
def d(i,j,v):
g[i,j]=v.lower()
for x,y in t.product(range(-3,4),repeat=2):
if x==y==0 or v=="B"and x*y!=0:continue
if(o:=(i+x,j+y))in g:
if"B"==g[o]:d(*o,"B")
if g[o].isdigit():g[o]=max("5"if v=="H"else str(4-max(abs(x),abs(y))),g[o])
for i,j in b:d(i,j,b[i,j])
for i in R:print("".join(g[i,j].upper()for j in R))


Try it online!