Advertisement
Guest User

12. Bomb Numbers

a guest
Aug 4th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _12.Bomb_Numbers
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. List<int> nums = Console.ReadLine().Split().Select(int.Parse).ToList();
  14. int[] bombNumber = Console.ReadLine().Split().Select(int.Parse).ToArray();
  15.  
  16. List<int> afterExplosions = detonateTheBomb(nums, bombNumber);
  17.  
  18. //Console.WriteLine(string.Join(" ", afterExplosions));
  19.  
  20. long sum = afterExplosions.Sum();
  21. Console.WriteLine(sum);
  22. }
  23.  
  24. private static List<int> detonateTheBomb(List<int> nums, int[] bombNumber)
  25. {
  26. int bomb = bombNumber[0];
  27. int power = bombNumber[1];
  28. int bombIndex = int.MinValue;
  29. bool isThereABombOrNot = true;
  30.  
  31. while (isThereABombOrNot)
  32. {
  33.  
  34. for (int item = 0; item < nums.Count; item++)
  35. {
  36. //finding the index of the bomb
  37. if (nums[item] == bomb)
  38. {
  39. bombIndex = item;
  40. }
  41.  
  42. }//end of for
  43.  
  44. int length = nums.Count;
  45. int left = power;
  46. int right = power;
  47.  
  48. //detonating indexes
  49. if (bombIndex + 1 - left > 0&& bombIndex + 1 + right < length)
  50. {
  51. nums.RemoveRange(bombIndex - left, left + right + 1);
  52. }
  53. else if (bombIndex + 1 - left <= 0)
  54. {
  55. while (bombIndex + 1 - left <= 0)
  56. {
  57. left--;
  58. }
  59. nums.RemoveRange(bombIndex - left, left + right + 1);
  60. }
  61. else if (bombIndex+1+right>length)
  62. {
  63. while (bombIndex + 1 + right > length)
  64. {
  65. right--;
  66. }
  67. nums.RemoveRange(bombIndex - left, left + right + 1);
  68. }
  69.  
  70.  
  71.  
  72.  
  73. //checking for another bomb
  74. for (int item = 0; item < nums.Count; item++)
  75. {
  76. //finding the index of the bomb
  77. if (nums[item] == bomb)
  78. {
  79. isThereABombOrNot = true;
  80. bombIndex = item;
  81. break;
  82. }
  83. else
  84. {
  85. isThereABombOrNot = false;
  86. }
  87.  
  88. }//end of for
  89.  
  90. }//end of while
  91. return nums;
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement