Advertisement
YORDAN2347

Bombs

Feb 2nd, 2021
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _05._BombNumbers
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<int> sequence = Console.ReadLine()
  12.                 .Split()
  13.                 .Select(int.Parse)
  14.                 .ToList();
  15.  
  16.             string[] input = Console.ReadLine().Split();
  17.             int bombNumber = int.Parse(input[0]);
  18.             int bombPower = int.Parse(input[1]);
  19.  
  20.             /// 1. find a bomb
  21.             while (sequence.Contains(bombNumber))
  22.             {
  23.                 int index = sequence.IndexOf(bombNumber);
  24.                
  25.                 int rightMostIndex = bombPower;
  26.                 if (rightMostIndex + index >= sequence.Count)
  27.                 {
  28.                     rightMostIndex = sequence.Count - 1 - index;
  29.                 }
  30.  
  31.                 int leftMostIndex = bombPower;
  32.                 if (index - leftMostIndex < 0)
  33.                 {
  34.                     leftMostIndex = index;
  35.                 }
  36.  
  37.                 // 2. detonate bombPower neighbrs.
  38.                 sequence.RemoveRange(index - leftMostIndex, leftMostIndex + rightMostIndex + 1);
  39.  
  40.             }
  41.             // 3 print sum of left Elements
  42.             int sum = sequence.Sum();
  43.             Console.WriteLine(sum);
  44.         }
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement