# Landmine Number IV

## Background

You have again x3 been given the task of calculating the number of landmines in a field. But this time, we have no landmine number, and some landmines have exploded.

You must calculate the landmine score given a string of numbers. This is the landscape.

You have no landmine number to start with. Instead, the landscape has gaps where possible landmines may have exploded. It may also be the case that the gaps did not have a landmine.

For each gap in the landscape, represented by a space, add and multiply the digits left and right of it to gain two candidates for the landmine number.

For example, given 1 3, two candidates would be 1+3 = 4 and 1*3 = 3.

After doing this for all gaps, the number most often given as a result of calculations (highest count) is the landmine number.

If there are ties for the highest number count, the landmine number is the largest number in the tie.

For each time the result occurs as the calculated landmine number, add the landmine number to the landmine score.

Note: The very first and last digits will always have numbers, never gaps. There will also never be two immediately consecutive gaps.

• Sample Input: The number landscape, which will only contain numbers and spaces. It is given that if there are no gaps, return 0.

This cannot be taken as a list of numbers (eg. taking 02 24 as ['02', '24']) or seperate inputs but it can be taken as a list of characters/ single digit numbers (eg. taking 02 24 as ['0', '2', ' ', '2', '4' ]). This is because the landscape is one entity, and could easily have used a different character other than space.

• Output: Return the landmine score.

Explained Examples

Input => Output
178 9 11 => 72

The possible landmine numbers are:
8+9 = 17
8*9 = 72
9+1 = 10
9*1 = 9

These all occur once, so we choose the largest number within the tie, which is 72.

Input => Output
12 32 21 32 22 => 20

Possible landmine numbers:
2+3 = 5
2*3 = 6
2+2 = 4
2*2 = 4
1+3 = 4
1*3 = 3
2+2 = 4
2*2 = 4

Count:
2 = 1
3 = 2
4 = 5

The highest count is 5, for 4. So 4 is the landmine number. We add 4 5 times to get the landmine score, 20.

Input => Output
999910 10 10 10 10 10 10 => 6

Possible landmine numbers:
0+1 = 1, which occurs 6 times.
0*1 = 0, which occurs 6 times.

The highest count is 6 and it is tied, so we choose the largest number in the tie, 1. We add this 6 times to get the landmine score, 6.

Input => Output
2746 27362 4815 383 6 21 745 => 40

Possible landmine numbers:
6+2 = 8
6*2 = 12
2+4 = 6
2*4 = 8
5+3 = 8
5*3 = 15
3+6 = 9
3*6 = 18
6+2 = 8
6*2 = 12
1+7 = 8
1*7 = 7

Count:
6 = 1
7 = 1
8 = 5
9 = 1
12 = 2
15 = 1
18 = 1

The highest count is 5, for 8. So 8 is the landmine number. Finally, we add 8 5 times to get the landmine score, 40.


Test Cases

Input => Output
178 9 11 => 72
999910 10 10 10 10 10 10 => 6
2746 27362 4815 383 6 21 745 => 40

=> 0
19383 => 0
1 1 1 1 1 1 1 1 1 1 => 18
187373 28377373 2837821 2837 => 12
000 001 010 011 100 101 110 111 => 0
10 11 12 13 14 15 16 17 18 19 20 => 16
28478382847738212848392928626188 => 0
28478382847 7382128483 92928626188 => 49


This is , so shortest answer wins.

...

Landmine Number Series

• I cannot understand why empty string or string without spaces gives 0. And what is expected if the string starts / ends with space?
– tsh
Commented Jul 13, 2023 at 8:57
• @tsh Your second question is already mentioned: "Note: The very first and last digits will always have numbers, never gaps. There will also never be two immediately consecutive gaps." Commented Jul 13, 2023 at 9:30
• Your second example is incorrect. The first gap is 2 3, and not 1 2. (12 32 21 32 22 should be 1 232 21 32 22). Alternatively, the input is correct, but the Possible landmine numbers and Counts in your steps are incorrect. The output will be 20 for both 12 32 21 32 22 and 1 232 21 32 22. Commented Jul 13, 2023 at 9:32
• @KevinCruijssen Thanks - missed that. Corrected now. Commented Jul 13, 2023 at 12:25

# Excel (ms365), 151 bytes

Formula in B1:

=IFERROR(LET(s,TEXTSPLIT(A1,," "),r,DROP(RIGHT(s),-1),l,DROP(LEFT(s),1),v,VSTACK(r*l,r+l),m,MAP(v,LAMBDA(y,SUM(N(v=y)))),@SORTBY(v,m,-1,v,-1)*MAX(m)),)


# 05AB1E, 23 bytes

ü3ʒðk}εþDOsP‚}˜{RÐ.MQÏO


Input as a list (with spaces as gaps).

Explanation:

ü3         # Push all overlapping triplets of the (implicit) input-list
ʒ  }     # Filter this list of triplets by:
ðk      #  Get the 0-based index of " ", or -1 if it isn't in the triplet
#  (only 1 is truthy in 05AB1E, so only triplets with spaces in the middle
#  are kept)
ε        # Then map over each remaining triplet:
þ       #  Remove the space by only leaving numbers
D      #  Duplicate it
O     #  Sum this pair
s      #  Swap so the pair is at the top again
P     #  Take the product of this pair
‚    #  Pair them together
}˜  # After the map: flatten it to a single list
{R         # Sort this list and reverse it, so it's sorted from highest to lowest value
Ð        # Triplicate this list
.M      # Pop one list, and push the first most frequent number
Q     # Check which values are equal to this most frequent max
Ï    # Only keep the values at those truthy positions
O   # Sum them together
# (after which the result is output implicitly)


# Scala, 234 bytes

Golfed version. Try it online!

s=>{val n=s.split(' ');val c=mutable.Map[Int,Int]().withDefaultValue(0);for(i<-1 to n.length-1){val l=n(i-1).last.asDigit;val r=n(i)(0).asDigit;c(l+r)+=1;c(l*r)+=1};if(c.isEmpty)0 else{val m=c.values.max;c.filter(_._2==m).keys.max*m}}


Ungolfed version. Try it online!

import scala.collection.mutable

object Main {
def landmineScore(s: String): Int = {
val numbers = s.split(' ')
val counts = mutable.Map[Int, Int]().withDefaultValue(0)

for (i <- 1 until numbers.length) {
val l = numbers(i - 1).last.asDigit

val sums = l + r
val prods = l * r

counts(sums) += 1
counts(prods) += 1
}

if (counts.isEmpty) 0
else {
val maxCount = counts.values.max
val maxNums = counts.filter { case (_, v) => v == maxCount }.keys
maxNums.max * maxCount
}
}

def main(args: Array[String]): Unit = {
val testCases = List(
"178 9 11",
"999910 10 10 10 10 10 10",
"2746 27362 4815 383 6 21 745",
" ",
"19383",
"1 1 1 1 1 1 1 1 1 1",
"187373 28377373 2837821 2837",
"000 001 010 011 100 101 110 111",
"10 11 12 13 14 15 16 17 18 19 20",
"28478382847738212848392928626188",
"28478382847 7382128483 92928626188"
)

testCases.foreach(tc => println(s"Input: $tc => Output:${landmineScore(tc)}"))
}
}



# Perl 5, 86 bytes

s/. (?=(.))/$h{$_}++for$&+$1,$&*$1/ge;($_)=sort{$h{$b}-$h{$a}||$b-$a}keys%h;$_*=$h{$_}


Try it online!

# Retina, 82 bytes

Lv$. (.)$.(*_$1*)¶$.(*$1* ^$
0
N
N^$(.+)(¶\1)*$#2
L$^(.+)(¶\1)*$.(*$($#2*__


Try it online! Link includes test cases. Explanation:

Lv$. (.)$.(*_$1*)¶$.(*$1*  Get the sums and products of the digits adjacent to spaces. ^$
0


If there are none then insert a dummy product.

N


Sort them.

N^$(.+)(¶\1)*$#2


Sort them in descending order of frequency.

L$^(.+)(¶\1)*$.(*$($#2*__


Multiply the mode by its count.

# JavaScript, 96 bytes

s=>~s.map(m=o=(c,i)=>p=c<'0'?[p*s[++i],p-s[i]].map(n=>m=--o[o[n]||=n/1e4,n]>o[m]?m:n):-c)&m*o[m]


f=

s=>~s.map(m=o=(c,i)=>p=c<'0'?[p*s[++i],p-s[i]].map(n=>m=--o[o[n]||=n/1e4,n]>o[m]?m:n):-c)&m*o[m]

testcases = 
178 9 11 => 72
999910 10 10 10 10 10 10 => 6
2746 27362 4815 383 6 21 745 => 40
=> 0
19383 => 0
1 1 1 1 1 1 1 1 1 1 => 18
187373 28377373 2837821 2837 => 12
000 001 010 011 100 101 110 111 => 0
10 11 12 13 14 15 16 17 18 19 20 => 16
28478382847738212848392928626188 => 0
28478382847 7382128483 92928626188 => 49
3 => 0
.match(/^.* => .*$/gm).map(r => r.split(' => ')); testcases.forEach(([i, e]) => { console.log(e == f([...i]), f([...i])); }); # Python 3.8 (pre-release), 135 bytes lambda l:-(m:=max(l:=sorted(-w for x,y,z in zip(l,l[1:],l[2:])if y==' 'for w in eval(f'{x}+{z},{x}*{z}'))or [0],key=(c:=l.count)))*c(m)  Try it online! ## Explanation • for x,y,z in zip(l,l[1:],l[2:]): Iterate over each triplet of 3 consecutive elements • if y==' ': Skip when the middle element is not a space • for -w in eval(f'{x}+{z},{x}*{z}'): Append negative of the sum and product of first and third elements to list. These are the negated landmine number candidates. eval is used to avoid having to convert to int using int • l:=sorted(...): Iterate over the sorted negated landmine candidates. Ensures that bigger magnitudes appear first. Update the list l to this list. • lambda l:-(m:=max(...,key=(c:=l.count)))*c(m): Find the max value of the negated landmine number candidates when sorted by number of occurrences, multiply by the number of occurrences, and then negate the result. Because we sorted before, most negative numbers appear first, and max will take the first occurring element when their is a tie, which is also the largest when negated. Set c:=l.count to save bytes. # JavaScript (Node.js), 97 bytes x=>~x.replace(k=/. (?=(.))/g,(a,b)=>[-a*b,-a-b].map(v=>x=x<(t=k[v]=k[v]-1||v/99)?x:t))&x%1*99*~-x  Try it online! -1 from Arnauld • 97? Commented Jul 13, 2023 at 11:53 • x%1*99 may have some floating point issues which could be annoying: f('3 3 3') // 18, f('6 6 6 6 6 6') // 180, f('7 7 7 7') // 147 – tsh Commented Jul 14, 2023 at 2:27 • @tsh Should I use this instead? – l4m2 Commented Jul 14, 2023 at 3:53 • Maybe. It at least works (with errors) – tsh Commented Jul 14, 2023 at 12:56 # R, 225 bytes s=strsplit(x, '')[[1]];i=s==' ';s=as.double(s);if(any(i)){l=as.data.frame(table(sapply(X=which(i),FUN=function(X)c(s[X-1]+s[X+1],s[X-1]*s[X+1]))),stringsAsFactors=F);y=max(l[,2]);z=max(as.double(l[(l[,2]==y),1]));z*y;}else{0}  Try it online! Readable version: get_lms <- function(x) { chars = strsplit(x, '')[[1]] idx = chars==' ' nums = as.double(chars) if(any(idx)){ lm_cts = as.data.frame( table( sapply(X = which(idx), FUN = function(X) c(nums[X-1] + nums[X+1], nums[X-1] * nums[X+1])) ), stringsAsFactors = F); hi_cnt = max(lm_cts[,2]); hi_num = max(as.double(lm_cts[(lm_cts[,2]==hi_cnt),1])); hi_num*hi_cnt } else { 0 } } all(get_lms("178 9 11") == 72 ,get_lms("12 32 21 32 22") == 20 ,get_lms("999910 10 10 10 10 10 10") == 6 ,get_lms("2746 27362 4815 383 6 21 745") == 40 ,get_lms("") == 0 ,get_lms("19383") == 0 ,get_lms("1 1 1 1 1 1 1 1 1 1") == 18 ,get_lms("187373 28377373 2837821 2837") == 12 ,get_lms("000 001 010 011 100 101 110 111") == 0 ,get_lms("10 11 12 13 14 15 16 17 18 19 20") == 16 ,get_lms("28478382847738212848392928626188") == 0 ,get_lms("28478382847 7382128483 92928626188") == 49)  • Welcome to CGCC, and thanks for the submission! Commented Jul 13, 2023 at 19:30 # Charcoal, 42 bytes ＦＥ⌕Ａθ ✂θ⊖ι⁺²ι⊞⊞ＯυΣιΠι≔⌈Ｅυ№υιηＩ∨⌈×Φυ⁼№υιηη⁰  Try it online! Link is to verbose version of code. Explanation: ＦＥ⌕Ａθ ✂θ⊖ι⁺²ι  Loop over all of the slices of length 3 centred on spaces. ⊞⊞ＯυΣιΠι  Push the sum and product of that slice to the predefined empty list. Note that sum and product of a string splits the string on non-digit characters, casts the pieces to number and then takes the sum or product of the resulting list, although for single digits this is equivalent to the digital sum or product. ≔⌈Ｅυ№υιη  Get the highest frequency of any of the values. Ｉ∨⌈×Φυ⁼№υιηη⁰  Multiply by the highest value with that frequency, or output 0 if there were no spaces in the input. # Husk, 17 bytes Σ►LgOΣẊ~§e+*→←†iw  Try it online! ## Explanation Σ►LgOΣẊ~§e+*→←†iw w Split input on spaces. †i Convert each character to integer. Ẋ For each adjacent pair of numbers: ~ →← take the last digit from the first and the first digit from the second §e+* and build a list with the sum and product of those digits. Σ Concatenate all the lists obtained. O Sort the values smallest to largest g Group equal values together ►L Take the longest group (later groups chosen in case of equal length) Σ Sum all values from this group together  # Jelly, 17 bytes I feel like the last seven bytes may be able to be six somehow ḲṚ+ż×ɗḢɗƝFfⱮÆṃ$ṀS


A monadic Link that accepts a list of integers (the digits) and space characters (the gaps) and yields an integer (the landmine score).

Try it online!
Note: Jelly cannot accept a mixed list of integers and characters when running as a full program as it interprets ' ' as a string
(e.g. an argument of [1, ' ', 2] would give the Main Link [1, [' '], 2]).

### How?

ḲṚ+ż×ɗḢɗƝFfⱮÆṃ$ṀS - Link: list of integers [0-9] & (single) space characters, Field Ḳ - split {Field} at space characters Ɲ - for each neighbouring pair (LeftInts, RightInts): ɗ - last three links as a dyad: Ṛ - reverse {LeftInts} ɗ - last three links as a dyad: + - {ReversedLeftInts} add {RightInts} × - {ReversedLeftInts} multiply {RightInts} ż - {Added} zip {Multiplied} Ḣ - head F - flatten -> PossibleLandmineNumbers$   - last two links as a monad:
Æṃ    -   mode -> MostCommonValues
Ɱ      -   for V in {MostCommonValues}:
f       -    {PossibleLandmineNumbers} filter keep {V}
Ṁ  - maximum
S - sum


# Vyxals, 128 bitsv2, 16 bytes

⌈zƛ÷h$t"⌊₍∑Π;fsṘĠÞG  Try it Online! Takes input as a string. Ports Husk. ## Explained ⌈zƛ÷h$t"⌊₍∑Π;fsṘĠÞG­⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁡⁪‏‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁤⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁣⁡⁪‏‏​⁡⁠⁡‌⁢⁡​‎‎⁪⁡⁪⁠⁪⁣⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁣⁪‏⁠‎⁪⁡⁪⁠⁪⁣⁤⁪‏‏​⁡⁠⁡‌⁢⁢​‎‎⁪⁡⁪⁠⁪⁤⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁤⁣⁪‏‏​⁡⁠⁡‌⁢⁣​‎‎⁪⁡⁪⁠⁪⁤⁤⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁡⁪‏‏​⁡⁠⁡‌⁢⁤​‎‎⁪⁡⁪⁠⁪⁢⁡⁢⁪‏⁠‎⁪⁡⁪⁠⁪⁢⁡⁣⁪‏‏​⁡⁠⁡‌⁣⁡​‎‏​⁢⁠⁡‌­
⌈z                   # ‎⁡Split the input on spaces and get all overlapping pairs
ƛ         ;        # ‎⁢To each pair:
÷h\$t"             # ‎⁣  [pair[0][-1], pair[-1][0]]
⌊            # ‎⁤  Each converted to int
₍∑Π         # ‎⁢⁡  [sum, product]
fs      # ‎⁢⁢Flatten and sort the result
ṘĠ    # ‎⁢⁣Reverse and group by consecutive value
ÞG  # ‎⁢⁤Get the longest by length
# ‎⁣⁡The s flag sums the resulting list
💎


Created with the help of Luminespire.