11
\$\begingroup\$

Background

You have again, again been given the task of calculating the number of landmines in a field. But this time, the field is foggy.

You must calculate the landmine score given a list/string of numbers, the landmine number, and the land distance (all are numbers).

The landmine number tells you where landmines are.

For each digit in the landscape (first number), if the digits left and right add to the landmine number, add the digit in focus to the landmine score.

Note: The very first and last numbers cannot have landmines because they have no numbers to the left and right of them respectively.

Because the field is foggy however, we need to clear the fog and expand the landscape. This is done by repeatedly adding the last two digits of the given landscape and appending it to the end. If the result of the addition has two digits, append the ones digit. This is done until the length of the landscape is equal to the land distance.

Your Task

  • Sample Input: The landscape, the landmine number, and the land distance. It is given that the land distance is greater than or equal to the length of the number landscape. It is also given that the landscape contains at least two digits.

  • Output: Return the landmine score.

Explained Examples Note: Expansions are grouped in 5 for demonstration.

Input => Output
178 9 11 => 16

When expanded, the landscape is:
17853 81909 9

Of these:
178 => 1+8 = 9, so add 7
099 => 0+9 = 9, so add 9

7+9 = 16
Input => Output
012 7 21 => 16

Expanding: 
01235 83145 94370 77415 6

Of these:
235, 2+5 = 7, add 3
314, 3+4 = 7, add 1
077, 0+7 = 7, add 7
156, 1+6 = 7, add 5

3+1+7+5 = 16
Input => Output
10 10 10 => 0

Expanding:
10112 35831 

Of these, there are no landmines.
Input => Output
090909 0 10 => 18

Expanding:
09090 99875

Of these:
090, 0+0=0, add 9
090, 0+0=0, add 9

9+9=18

Test Cases

Input => Output
178 9 11 => 16
012 7 21 => 16
10 10 10 => 0
090909 0 10 => 18

00 0 99 => 0
00 99 99 => 0
91900 18 42 => 1
123121 10 15 => 18
1111111111 2 10 => 8

This is , so shortest answer wins.

...

Landmine Number Series

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Could we take the first input-number as a list of digits? \$\endgroup\$ Jul 11 at 12:34
  • 2
    \$\begingroup\$ @KevinCruijssen Yes, the input is there just as a sample, but you can take the three numbers in any format within reason. \$\endgroup\$ Jul 11 at 12:36
  • 3
    \$\begingroup\$ I enjoyed parts 1 & 2 and your chain link challenges, but for future challenges, consider that multipart challenges, or challenges with subtasks, are discouraged. \$\endgroup\$
    – Jonah
    Jul 11 at 16:54
  • 1
    \$\begingroup\$ @Jonah Thanks! Is it a problem with this challenge specifically being too close to part 1? If I have more challenges under the same story, should I change the story or change the task to be unique enough? \$\endgroup\$ Jul 11 at 17:00
  • 1
    \$\begingroup\$ @Jonah Thanks, noted! I have LN IV lined up for tomorrow and I think it's sufficiently different, could you have a look at it tomorrow? \$\endgroup\$ Jul 11 at 19:46

14 Answers 14

7
\$\begingroup\$

Python 3.8+, 91 85 84 82 80 78 bytes

f=lambda s,n,d:d-1and f((s:=s+[sum(s[-2:])%10])[1:],n,d-1)+s[1]*(s[0]+s[2]==n)

Try it online!

Takes a list of int digits, the landmine number and then the land number

Explanation

Recursive implementation where f is called d-1 times. In every call, the digit sum(s[-2:])%10 is appended to s. This may add extra digits to the landscape, but the function will terminate before reaching them so this is not a problem. Additionally, the recursive call removes the first digit of the landscape ((...)[1:]) so that extra parameters are avoided, and so that s[1]*(s[0]+s[2]==n) can use single digit numbers to index the list, saving bytes.

\$\endgroup\$
8
  • 3
    \$\begingroup\$ Welcome to Code Golf, and nice first submission! I don't know much Python, but you can remove the space and newline after def f(s,n,d):, and d-1 before and can be ~d (i.e. -d-1 which is equivalent in this case), for 81 bytes: Try it online! \$\endgroup\$
    – noodle man
    Jul 11 at 17:53
  • \$\begingroup\$ -3 bytes thanks to @noodleman \$\endgroup\$ Jul 11 at 18:01
  • 1
    \$\begingroup\$ replaced s+=[stuff] with s+=stuff, and removed an extra space: 80 bytes \$\endgroup\$
    – Ethan C
    Jul 11 at 18:21
  • 1
    \$\begingroup\$ made it a lambda function but only works if walrus expressions are allowed (python 3.8+) 76 bytes \$\endgroup\$
    – Ethan C
    Jul 11 at 18:27
  • 1
    \$\begingroup\$ -2 bytes thanks to @EthanC (f= needs to be prepended or else the recursive call can't be made) \$\endgroup\$ Jul 11 at 18:40
5
\$\begingroup\$

Vyxal d, 96 bitsv1, 12 bytes

‡+tdḞẎ3lƛy$∑⁰=*

Try it Online!

I do love a good opportunity to use a generator. Takes landmine digits as a list of digits, land area number, then landmine number.

Explained

‡+tdḞẎ3lƛy$∑⁰=*­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢‏‏​⁡⁠⁡‌⁤​‎‏​⁢⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁢‏⁠‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‏​⁡⁠⁡‌⁢⁤​‎‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏⁠‏​⁡⁠⁡‌⁣⁡​‎‎⁡⁠⁤⁣‏⁠‎⁡⁠⁤⁤‏‏​⁡⁠⁡‌⁣⁢​‎‏​⁢⁠⁡‌­
‡  dḞ            # ‎⁡A generator where each term:
  t              # ‎⁢  is the tail of
 +               # ‎⁣  the sum of the last 2 terms
# ‎⁤The generator takes the landmine digits as the initial list of terms and works from there.
     Ẏ           # ‎⁢⁡Take the first land-area digits of the generator
      3lƛ        # ‎⁢⁢And to each overlapping window of size 3:
         y$      # ‎⁢⁣  Uninterleave and swap so that the first and last item of the window are on the top of the stack in a list
            ⁰=   # ‎⁢⁤  Check whether the sum of that list is equal to the landmine number
              *  # ‎⁣⁡  And multiply the middle of the window by that. If the sum was equal, this will return the number as-is. Otherwise, it will return 0.
# ‎⁣⁢The d flag flattens the result of the map before summing the list.

💎 Created with the help of Luminespire at https://vyxal.github.io/Luminespire
\$\endgroup\$
3
\$\begingroup\$

Charcoal, 39 bytes

NθNηFη≔⁺ζI﹪Σ…⮌ζ²χζIΣΦ✂ζ¹⊖η¹⁼θΣ⁺§ζκ§ζ⁺²κ

Try it online! Link is to verbose version of code. Takes the landmine number and field length as the first two inputs. Explanation:

NθNη

Input the landmine number and field length.

Fη≔⁺ζI﹪Σ…⮌ζ²χζ

Extend the field so it's at least as long as the required length.

IΣΦ✂ζ¹⊖η¹⁼θΣ⁺§ζκ§ζ⁺²κ

Slice out the part that can have landmines, then filter out those digits whose neighbours have the wrong sum, then take the digital sum of the result.

\$\endgroup\$
3
\$\begingroup\$

Mathematica, 164 bytes

Golfed version. Try it online!

f[l_,m_,d_]:=Module[{a=l,b=0},While[Length[a]<d,AppendTo[a,Mod[Last[a]+If[Length[a]>1,a[[-2]],0],10]]];For[i=2,i<Length@a,i++,If[a[[i-1]]+a[[i+1]]==m,b+=a[[i]]]];b]

Ungolfed version. Try it online!

LandmineScore[landscape_List, mineNum_, dist_] := 
Module[{land = landscape, score = 0},
  While[Length[land] < dist,
    If[Length[land] > 1, 
        AppendTo[land, Mod[Last[land] + land[[-2]], 10]],
        AppendTo[land, land[[-1]]]
    ]
  ];
  For[i = 2, i < Length[land], i++, 
    If[land[[i - 1]] + land[[i + 1]] == mineNum, 
      score += land[[i]]
    ]
  ];
  score
]


Print[LandmineScore[{1, 7, 8}, 9, 11]];  (* 16 *)
Print[LandmineScore[{0, 1, 2}, 7, 21]];  (* 16 *)
Print[LandmineScore[{1, 0}, 10, 10]];  (* 0 *)
Print[LandmineScore[{0, 9, 0, 9, 0, 9}, 0, 10]];  (* 18 *)
Print[LandmineScore[{0, 0}, 0, 99]];  (* 0 *)
Print[LandmineScore[{0, 0}, 99, 99]];  (* 0 *)
Print[LandmineScore[{9, 1, 9, 0, 0}, 18, 42]];  (* 1 *)
Print[LandmineScore[{1, 2, 3, 1, 2, 1}, 10, 15]];  (* 18 *)
Print[LandmineScore[{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 2, 10]];  (* 8 *)
\$\endgroup\$
2
\$\begingroup\$

Excel, 172 bytes

Named variable z defined as:

=LAMBDA(x,n,
    IF(n=LEN(x),x,z(x,n-1)&MOD(SUM(0+MID(z(x,n-1),n-{1,2},1)),10))
)

Formula within the worksheet:

=LET(
    a,z(A1,C1),
    b,SEQUENCE(LEN(a)-2),
    SUM(IF(MMULT(0+MID(a,b+{0,2},1),{1;1})=B1,0+MID(a,b+1,1)))
)

Landscape, landmine number and land distance in A1, B1 and C1 respectively.

Fails in some cases (e.g. 00|99|99), perhaps due to IEEE 754 precision on the return from MOD.

\$\endgroup\$
2
\$\begingroup\$

05AB1E, 16 bytes

λ£₂+θ}Ц¦+IQ0šÏO

Inputs in the order landscape, land distance, landmine number, where landscape is a list of digits.

Try it online or verify all test cases.

Explanation:

λ           # Start a recursive environment,
 £          # to generate the first second (implicit) input amount of items,
            # Starting with a(0),a(1),...,a(l) at the first (implicit) input-list
            # And where every following a(n) is calculated as:
            #  (implicitly push a(n-1))
  ₂         #  Push a(n-2)
   +        #  Add them together
    θ       #  Pop and leave just its last digit
}           # Close the recursive environment,
            # (we now have the expanded landscape as a list of digits)
 Ð          # Triplicate the resulting list
  ¦¦        # Remove the first two items of the top copy
    +       # Add the values at the same positions together (ignoring the last two)
     IQ     # Check which values are equal to the third input
       0š   # Prepend a 0 to these checks
         Ï  # Only keep the values at the truthy indices
          O # Sum them together
            # (after which the result is output implicitly)
\$\endgroup\$
1
\$\begingroup\$

Q, 61 bytes

{while[z>(#:)x;x,:sum[-2#x]mod 10];sum x(&:)y=next[x]+prev x}

ungolfed:

{ [x; y; z]
    while[ z>count x;
        x,: (sum -2#x) mod 10
    ];
    : sum x where y=next[x] + prev[x]
    }
\$\endgroup\$
1
\$\begingroup\$

Pyth, 28 bytes

sm*@d1qQs%2d.:<u+Ges>2GKEEK3

Try it online!

Takes three inputs: landmine number, length, and starting sequence as a list.

Explanation

                       KE       # K = second input
               u         E      # repeat lambda G K times, with third input as the starting value
                +G              #   append to G
                  e             #   the last digit of
                   s            #   the sum of
                    >2G         #   the last two elements of G
              <           K     # first K elements of the last value of G
            .:             3    # all length 3 sublists
 m                              # map this to lambda d
  *@d1                          #   middle element of d times
      qQs%2d                    #   Q == sum of first and last elements of d
s                               # sum all terms
\$\endgroup\$
1
\$\begingroup\$

C (clang), 94 bytes

f(*a,m,n,l,*s){for(;*s=m<n;)a[m++]=(a[m-2]+a[m-1])%10;for(;--m>1;)*s+=a[m-2]+a[m]-l?0:a[m-1];}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python 3.8 (pre-release), 127 bytes

lambda s,n,d:(s:=(g:=lambda w:w if d==len(w)else g(w+[(w[-1]+w[-2])%10]))(s))and sum(z*(x+y==n)for x,y,z in zip(s,s[2:],s[1:]))

Try it online!

So I have Python 3.10.0 installed on my computer (and it works) but it doesn't work on TIO's "Python 3"? IDK at least it works on this version.

So there are two parts to this function, chained by the and operator. The first part expands the landscape to the proper length using a recursive lambda function. The second part uses zip to loop over every pair of elements and checks to see if they sum to the landmine number. And finally, sum is used to add up all the landmine scores found to get the answer.

Defining the second lambda function is really costly so I wouldn't be surprised is someone else found a better way to generate the new landscape.

\$\endgroup\$
1
  • \$\begingroup\$ TIO is no longer updated with new versions of languages, if you want a more modern release of Python try Attempt This Online (ATO) \$\endgroup\$
    – noodle man
    Jul 11 at 17:56
1
\$\begingroup\$

Retina 0.8.2, 127 bytes

\G\d|\d+
$*_,
+`^((_*,)*(_*),(_*,))(¶.*¶(?<-2>_)*(?(2)^)___)
$1$3$4$5
+`^(.*)_{10}
$1
M!`(?<=(_*),)(_*)(?=,(_*))(?=,.+¶\1\3,)
_

Try it online! Takes input on separate lines but link is to test suite that splits on spaces for convenience. Explanation:

\G\d|\d+
$*_,

Convert to unary.

+`^((_*,)*(_*),(_*,))(¶.*¶(?<-2>_)*(?(2)^)___)
$1$3$4$5

Extend the field until it reaches the distance.

+`^(.*)_{10}
$1

Keep only the ones digits of the field.

M!`(?<=(_*),)(_*)(?=,(_*))(?=,.+¶\1\3,)

Match only the landmines.

_

Take their sum.

\$\endgroup\$
1
\$\begingroup\$

Swift 5.9, 113 bytes

while l.count<d{l+=[(l[l.count-2]+l.last!)%10]};return(1...l.count-2).reduce(0){$0+(l[$1-1]+l[$1+1]==n ?l[$1]:0)}

Where the function header is:

func landmineScore(landscape l: inout [Int], mineNumber n: Int, distance d: Int) -> Int

Ungolfed version (plus header):

func landmineScore(landscape: inout [Int], mineNumber: Int, distance: Int) -> Int {
    while landscape.count < distance {
        landscape += [(landscape[landscape.count - 2] + landscape.last!) % 10]
    }
    
    return (1...(landscape.count - 2)).reduce(0) { result, index in
        result + (landscape[index - 1] + landscape[index + 1] == mineNumber ? landscape[index] : 0)
    }
}

I chose to make landscape an inout because that prevents me from having to declare var m=l at the beginning, saving 8 bytes total. I also used Array.reduce instead of a for-in loop or Array.forEach, saving a fair amount of bytes (since it also allowed me to eliminate using a separate score variable).

Using l.count instead of l.endIndex saved 3 bytes per use (both values are always the same).

The space before the ? is needed to prevent Swift from trying to optional-chain.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES13), 74 bytes

Expects (number)(landscape)(distance), where landscape is given as a string.

n=>s=>g=k=>k>2&&g(--k,s+=-(-s.at(-1)-s.at(-2))%10)+s[k-1]*!(n-s[k-2]-s[k])

Attempt This Online!

Commented

n =>              // n = landmine number
s =>              // s = landscape string
g = k =>          // k = land distance
k > 2 &&          // if k is greater than 2:
  g(              //   do a recursive call:
    --k,          //     decrement k
    s += -(       //     append to s:
      -s.at(-1) - //       the sum of the last digit in s
      s.at(-2)    //       and the penultimate digit
    ) % 10        //     modulo 10
  )               //   end of recursive call
                  //   the code below is executed once all recursive
                  //   calls have been initiated, which means that s
                  //   is now guaranteed to be long enough
  + s[k - 1] * !( //   add s[k - 1] to the final result if:
    n             //     n is equal to
    - s[k - 2]    //     the sum of s[k - 2]
    - s[k]        //     and s[k]
  )               //

JavaScript (ES6), 64 bytes

Same input format as above. Due to limited IEEE 754 precision, this version cannot see very far in the fog...

n=>s=>g=k=>--k>1&&g(k,s+=(+s+s/10)%10|0)+s[k-1]*!(n-s[k-2]-s[k])

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Perl 5, 110 bytes

/ (\d+) /;$_=$`;$l=$';$n=$&;/(.)(.)$/,$_.=($1+$2)%10while y///c<$l;/(.)(.)(.)(?{$s+=$2if$1+$3==$n})^/g;$_=$s|0

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.