# Landmine Number II

## Background

You have again been given the task of calculating the number of landmines in a field. However, we have now travelled into the flatlands.

You must calculate the landmine score given a list/string of numbers (which will be 2D) and the landmine number.

The landmine number tells you where landmines are.

For each digit:

1. if the DIRECTLY ADJACENT digits above, left, right, and below add to the landmine number, add the digit in focus to the landmine score.
2. if the DIRECTLY ADJACENT digits above, left, right, and below multiply to the landmine number, add double the digit in focus to the landmine score.
3. if both 1 and 2 are satisfied, add triple the digit in focus to the landmine number.

Note: The very corner and edge digits cannot have landmines because they are not fully surrounded by other numbers.

• Sample Input: A two dimensional array of numbers, NxN, such that N is >= 3. Also, the landmine number.

• Output: Return the landmine score.

Explained Examples

Input => Output
111
111
111

4 => 1


There is 1 landmine here. The central 1 has 1+1+1+1 = 4.

Input => Output

1448
4441
4114
2114

16 => 12


Focusing only on central numbers,

• Top right 4 has 4x4x1x1 = 16, so we add 4x2 = 8.
• Bottom left 1 has 4x4x1x1 = 16, so we add 1x2 = 2.
• Bottom right 1 has 4x1x1x4 = 16, so we add 1x2 = 2.

8+2+2 = 12

Input => Output
12312
19173
04832
01010
00100

8 => 42


There are 42 landmines here:

• On 9, 2+1+4+1 = 2x1x4x1 = 8, so we add 9x3 = 27.
• On 7, 1+1+3+3 = 8, so we add 7.
• On 8, 1+4+3+0 = 8, so we add 8.
• On central 0, 8x1x1x1 = 8, so we add 0x2 = 0

27+7+8+0 = 42


Input => Output
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000

0 => 0


There are 36 central 0s. So, 36x0x3 = 0.

Test Cases

Input ~> Output

111
111
111

4 ~> 1

1448
4441
4114
2114

16 ~> 12

12312
19173
04832
01010
00100

8 ~> 42

00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000

0 ~> 0

090
999
090

36 ~> 9

1301
3110
0187
3009

9 ~> 12

48484
28442
84244
28448
48424

256 ~> 68

111111
111111
111111
114111
111111
111111

4 ~> 23


This is , so shortest answer wins.

...

Landmine Number Series

# J, 55 bytes

[:+/@,[({.@]*[:I.=)"#.3 3(4&{,](+/,*/)@#~1-2|#\)@,;._3]


Attempt This Online!

• Uses J's subarray verb 3 3...;._3, which automatically extracts the 3x3 tiles
• The rest is figuring out to extract the compass directions from each of those, and then do the needed arithmetic and comparisons with the landmine number

# JavaScript (ES11), 113 bytes

Expects (n)(matrix).

n=>m=>m.map((r,y)=>r.map(a=(v,x)=>t+=v*=a+(b=m[y+1]?.[x])+(c=m[y-1]?.[x])+r[++x]==n|2*(a*b*c*r[a=v,x]==n)),t=0)|t


Attempt This Online!

### Commented

n =>                          // n = target number
m =>                          // m[] = digit matrix
m.map((r, y) =>               // for each row r[] at index y in m[]:
r.map(a =                   //   initialize a to something NaN'ish
(v, x) =>                 //   for each value v at index x in r[]:
t +=                      //     add to t:
v *=                    //       v multiplied by:
//         1 if the sum of ...
a +                   //           a, the value on the left
(b = m[y + 1]?.[x]) + //           b, the value below
(c = m[y - 1]?.[x]) + //           c, the value above
r[++x]                //           and the value on the right
== n |                //         is equal to n
2 * (                 //         2 if the product of:
a * b * c *         //           a, b, c
r[a = v, x]         //           and the value on the right
//           (copy v into a afterwards)
== n                //         is equal to n
)                     //
),                          //   end of inner map()
t = 0                       //   start with t = 0
) | t                         // end of outer map(); return t


# Excel, 155 bytes

=SUM(TOCOL(
MAP(A1#,
LAMBDA(a,
LET(
b,OFFSET(a,{-1,0,0,1},{0,-1,1,0}),
c,SUBTOTAL(2,b),
d,SUBTOTAL(9,b),
IF(SUM(c)=4,IF(SUM(d)=Z1,a)+IF(PRODUCT(d)=Z1,2*a))
)
)
),2
)
)


Matrix is spilled range A1#; landmine number in cell Z1 (or, if necessary, some other cell which does not form part of A1#).

# Python 3, 125 bytes

lambda n,l:sum(e*(2*(a*b*c*d==l)+(a+b+c+d==l))for x,y,z in zip(n,n[1:],n[2:])for a,b,c,d,e in zip(x[1:],z[1:],y,y[2:],y[1:]))


Try it online!

The function takes in a 2D list of integers (n: list[list[int]]) and the landline number (l: int). Magical zip stuff happens which assigns a,b,c,d to the values adjacent to every "central" coordinate, and e to the value of the coordinate in focus.

Explanation by gsitcia:

_ for x,y,z in zip(n,n[1:],n[2:]) is equivalent to for i in range(1,len(n)-1): x,y,z=n[i-1],n[i],n[i+1]; _. _ for a,b,c,d,e in zip(x[1:],z[1:],y,y[2:],y[1:]) is equivalent to for j in range(1, len(n[0])-1): a,b,c,d,e = n[i-1][j], n[i+1][j], n[i][j-1], n[i][j+1], n[i][j]; _ both of these work because zip will cut off at the shortest length

-1 byte thanks to spooky_simon

-50 bytes thanks to gsitcia

• I made it into a single for loop, but didn't save any bytes Commented Jul 10, 2023 at 6:09
• you can save 1 byte by moving t=0 to the function definition as an default argument like def f(n,l,t=0): Commented Jul 10, 2023 at 17:23
• 125 Commented Jul 11, 2023 at 2:22
• @gsitcia yours is a completely different answer altogether, haha Commented Jul 11, 2023 at 3:19
• @gsitcia I'll put that in but would you mind explaining how it works? Commented Jul 11, 2023 at 16:23

# Charcoal, 49 bytes

ＷＳ⊞υιＰ⪫υ⸿Ｎθ≔⁰ηＦ⪫υ⸿«Ｆ⌊ＫＶ≧⁺×ＩＫＫ⁺⁼ΣΣＫＶθ⊗⁼ΠΣＫＶθηι»⎚Ｉη


Try it online! Link is to verbose version of code. Explanation:

ＷＳ⊞υι


Input the field.

Ｐ⪫υ⸿


Output the field without moving the cursor.

Ｎθ


Input the target.

≔⁰η


Ｆ⪫υ⸿«


Loop over the field.

Ｆ⌊ＫＶ


If there are digits on all four sides, then...

≧⁺×ＩＫＫ⁺⁼ΣΣＫＶθ⊗⁼ΠΣＫＶθη


... multiply the current digit by the sum of 1 if the digital sum equals the target and 2 if the digital product of those digits equals the target.

ι


Move to the next digit.

»⎚Ｉη


Output the final total.

Removing the Ｆ⌊ＫＶ test has the effect of only considering those adjacent digits that exist e.g. in the top left corner it would search for the target from the sum and products of the digits to the right and below only.

# Python 3, 174 bytes

lambda a,b:sum(a[c][d]*((b==a[c-1][d]+a[c][d-1]+a[c+1][d]+a[c][d+1])+2*(b==a[c-1][d]*a[c][d-1]*a[c+1][d]*a[c][d+1])) for d in range(1,len(a)-1) for c in range(1,len(a[d])-1))


Try it online!

If I could condense the doubled calls to a[c-1][d], a[c+1][d], a[c][d-1], and a[c][d+1], I could save several bytes.

• You can use walrus operator at Python 3.7+: lambda a,b:sum(a[c][d]*((b==(e:=a[c-1][d])+(f:=a[c][d-1])+(g:=a[c+1][d])+(h:=a[c][d+1]))+2*(b==efg*h)) for d in range(1,len(a)-1) for c in range(1,len(a[d])-1)) Commented Jul 10, 2023 at 6:12
• Full code with test case. TIO Commented Jul 10, 2023 at 11:28

# 05AB1E, 33 28 bytes

2Fø€ü3}εε˜ιθDOsPD)QOy˜4è*]˜O


-5 bytes thanks to @ovs.

Inputs in the order matrix, landmineNumber.

Explanation:

2Fø€ü3}   # Create all overlapping 3x3 blocks of the first (implicit) input-matrix:
2F    }   #  Loop 2 times:
ø       #   Zip/transpose; swapping rows/columns
#   (which will use the implicit first input-matrix in the first iteration)
€      #   Map over each row:
ü3    #    Create overlapping triplets
εε        # (Nested) map over each 3x3 block:
˜       #   Flatten it to a single nonet-list
ι      #   Uninterleave it into two parts
θ     #   Pop and leave just the last list (with the even-indiced values)
D    #   Duplicate this quadruplet
O   #   Sum it together
s    #   Swap so the quadruplet is at the top again
P   #   Take its product
D  #   Duplicate this product
) #   Wrap all three values into a list: [sum,product,product]
Q       #   Check for each whether it's equal to the second (implicit) input-integer
O      #   Sum the checks together
y˜    #   Push the flattened 3x3 block again
4è  #   Pop and leave the 0-based 4th integer (the center)
* #   Multiply it to the earlier sum of checks
]         # Close the nested map
˜        # Flatten it to a single list
O       # Sum this list together
# (after which it is output implicitly as result)

• If you flatten a 3x3 matrix, the 4 values from the edges are on the odd indices: 28
– ovs
Commented Jul 10, 2023 at 9:19
• @ovs Ah, of course. Thanks! :) Commented Jul 10, 2023 at 9:30

# C (clang), 148 $$\\cdots\$$ 118 117 bytes

#define y(z)*a*(a[-1]z+a[1]z+a[n]z+a[-n]==l)
i;j;f(*a,n,l,*s){for(*s=0,a+=j=n-2,i=j*j;i;*s+=y()+2*y(*))a+=i--%j?1:3;}


Try it online!

Saved a whopping 12 19 30 31 bytes thanks to ceilingcat!!!

• @ceilingcat Great golfing - thanks! :D Commented Jan 12 at 11:17

# JavaScript (Node.js), 120 bytes

x=>k=>x.map((y,i)=>y.map((z,j)=>r+=[a=0,b=1,2,-1].map(d=>{a+=c=(x[i+d%2]||0)[--d%2+j],b*=c})&&z*(a==k|2*(b==k))),r=0)&&r


Try it online!

Some of ur test cases seem wrong, 2x1x4x1 = 8 = 2+1+4+1

• Thanks, and sorry, should be fixed now. Commented Jul 9, 2023 at 9:21

# Python3, 237 bytes:

R=range
def f(n,B):
t=0
for x in R(1,len(B)-1):
for y in R(1,len(B[0])-1):
a,b,c,d=B[x-1][y],B[x][y-1],B[x][y+1],B[x+1][y]
j,k=a+b+c+d,a*b*c*d;v=B[x][y]
if k==n and j==n:t+=v*3
elif k==n:t+=v*2
elif j==n:t+=v
return t


Try it online!

# Vyxals, 210 bitsv1, 26.25 bytes

żṪ:Ẋ:ƛk□¨V+ƛ¹nÞi;₍Π∑⁰=;¹„¨VÞi*vB


Try it Online!

I forgot how this worked like 3 times while writing it. Chances are after I paste in the explanation, it'll be 4 times.

## Explained

żṪ:Ẋ:ƛk□¨V+ƛ¹nÞi;₍Π∑⁰=;¹„¨VÞi*vB­⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁢⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁣⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁢⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁣⁪‏‏​⁡⁠⁡‌⁢⁡​‎‎⁪⁡⁪⁠⁪⁣⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁡⁪‏⁠⁪⁪‏​⁡⁠⁡‌⁢⁢​‎‎⁪⁡⁪⁠⁪⁤⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁤⁪‏‏​⁡⁠⁡‌⁢⁣​‎‎⁪⁡⁪⁠⁪⁢⁡⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁤⁪‏‏​⁡⁠⁡‌⁢⁤​‎‎⁪⁡⁪⁠⁪⁢⁢⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁢⁪‏‏​⁡⁠⁡‌⁣⁡​‎‎⁪⁡⁪⁠⁪⁢⁢⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁡⁪‏‏​⁡⁠⁡‌⁣⁢​‎‎⁪⁡⁪⁠⁪⁢⁣⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁡⁪‏‏​⁡⁠⁡‌⁣⁣​‎‎⁪⁡⁪⁠⁪⁢⁤⁢⁪‏‏​⁡⁠⁡‌⁣⁤​‎‎⁪⁡⁪⁠⁪⁢⁤⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁤⁪‏‏​⁡⁠⁡‌⁤⁡​‎‏​⁢⁠⁡‌­
żṪ                                # ‎⁡The range [1 ... len(field) - 1]
:Ẋ                              # ‎⁢Cartesian producted with itself. This generates a list of indices of central numbers
ƛ                ;           # ‎⁣To each central number index:
k□¨V+                       # ‎⁤  Add it to ⟨ ⟨ 0 | 1 ⟩ | ⟨ 1 | 0 ⟩ | ⟨ 0 | -1 ⟩ | ⟨ -1 | 0 ⟩ ⟩ to get the numbers above, below, left and right of this number
ƛ    ;                 # ‎⁢⁡  To each adjacent number index:
¹nÞi                  # ‎⁢⁢    Retrieve it from the field
₍Π∑              # ‎⁢⁣  Push a list of [product(adjacents), sum(adjacents)]. Keen readers will notice that I used
# ‎⁢⁣  this in the previous challenge
⁰=            # ‎⁢⁤  And test whether that equals the landmine number. Also in the previous challenge.
¹„         # ‎⁣⁡Push a copy of the field and rotate the stack left so that the central indices are on the top
¨VÞi     # ‎⁣⁢Retrieve the numbers that are central in the field
*    # ‎⁣⁣And multiply by the list of checksums.
vB  # ‎⁣⁤Convert each from binary.
# ‎⁤⁡The s flag sums the resulting list

💎 Created with the help of Luminespire at https://vyxal.github.io/Luminespire


# Pyth, 37 bytes

sm+*yJ@d4qQ*FK%2td*JqQsKsmsM.:Cd3.:E3


Try it online!

# Scala, 224 bytes

Port of @Ajax1234's Python answer in Scala.

Golfed version. Try it online!

def f(n:Int,B:Array[Array[Int]])=(for(x<-1 to B.length-2;y<-1 to B(0).length-2;a=B(x-1)(y);b=B(x)(y-1);c=B(x)(y+1);d=B(x+1)(y);j=a+b+c+d;k=a*b*c*d;v=B(x)(y))yield if(k==n&&j==n)v*3 else if(k==n)v*2 else if(j==n)v else 0).sum


Ungolfed version. Try it online!

object Main {
def f(n: Int, B: Array[Array[Int]]): Int = {
var t = 0
for (x <- 1 until B.length - 1) {
for (y <- 1 until B(0).length - 1) {
val a = B(x - 1)(y)
val b = B(x)(y - 1)
val c = B(x)(y + 1)
val d = B(x + 1)(y)
val j = a + b + c + d
val k = a * b * c * d
val v = B(x)(y)
if (k == n && j == n) t += v * 3
else if (k == n) t += v * 2
else if (j == n) t += v
}
}
t
}

def main(args: Array[String]): Unit = {
val s1 = Array(Array(1, 1, 1), Array(1, 1, 1), Array(1, 1, 1))
val s2 = Array(Array(1, 4, 4, 8), Array(4, 4, 4, 1), Array(4, 1, 1, 4), Array(2, 1, 1, 4))
val s3 = Array(Array(1, 2, 3, 1, 2), Array(1, 9, 1, 7, 3), Array(0, 4, 8, 3, 2), Array(0, 1, 0, 1, 0), Array(0, 0, 1, 0, 0))
val s4 = Array.fill(8)(Array.fill(8)(0))
val s5 = Array(Array(0, 9, 0), Array(9, 9, 9), Array(0, 9, 0))
val s6 = Array(Array(4, 8, 4, 8, 4), Array(2, 8, 4, 4, 2), Array(8, 4, 2, 4, 4), Array(2, 8, 4, 4, 8), Array(4, 8, 4, 2, 4))
val s7 = Array(Array(1, 1, 1, 1, 1, 1), Array(1, 1, 1, 1, 1, 1), Array(1, 1, 1, 1, 1, 1), Array(1, 1, 4, 1, 1, 1), Array(1, 1, 1, 1, 1, 1), Array(1, 1, 1, 1, 1, 1))

println(f(4, s1))
println(f(16, s2))
println(f(8, s3))
println(f(0, s4))
println(f(36, s5))
println(f(256, s6))
println(f(4, s7))
}
}