Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. public class BoomingCannon
  5. {
  6.     static int[] parameters;
  7.     static int startIndex;
  8.     static int count;
  9.  
  10.     static bool isFirst = true;
  11.     static string targetsShotedDown = string.Empty;
  12.  
  13.     public static void Main()
  14.     {
  15.         parameters = ConvertToIntegerArray(Console.ReadLine());
  16.         startIndex = parameters[0];
  17.         count = parameters[1];
  18.  
  19.         string target = Console.ReadLine();
  20.         MatchAndShootOf(target);
  21.  
  22.         Console.WriteLine(targetsShotedDown);
  23.     }
  24.  
  25.     static int[] ConvertToIntegerArray(string input)
  26.     {
  27.         string[] splited = input.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  28.         int[] integerArray = new int[splited.Length];
  29.         for (int index = 0; index < splited.Length; index++)
  30.         {
  31.             integerArray[index] = Convert.ToInt32(splited[index]);
  32.         }
  33.         return integerArray;
  34.     }
  35.  
  36.     static void MatchAndShootOf(string target)
  37.     {
  38.         string pattern = @"((?<=\\<<<)(?<target>.+?))(?=\\<<<|$)";
  39.  
  40.         if (Regex.IsMatch(target, pattern))
  41.         {
  42.             MatchCollection currentTargets = Regex.Matches(target, pattern);
  43.             foreach (Match match in currentTargets)
  44.             {
  45.                 ShootOf(match.Groups["target"].Value);
  46.             }
  47.         }
  48.         else return;
  49.     }
  50.  
  51.     static void ShootOf(string currentTarget)
  52.     {
  53.         string down = string.Empty;
  54.         if (startIndex >= 0 && startIndex < currentTarget.Length)
  55.         {
  56.             down = SubstringFrom(currentTarget.Replace("\\<<<", string.Empty));
  57.             AddToResult(down);
  58.         }
  59.         else return;
  60.     }
  61.  
  62.     static string SubstringFrom(string currentTarget)
  63.     {
  64.         if (count >= 0 && startIndex + count < currentTarget.Length)
  65.         {
  66.             return currentTarget.Substring(startIndex, count);
  67.         }
  68.         else if (count >= 0 && startIndex + count >= currentTarget.Length)
  69.         {
  70.             return currentTarget.Substring(startIndex);
  71.         }
  72.         return string.Empty;
  73.     }
  74.  
  75.     static void AddToResult(string down)
  76.     {
  77.         if (isFirst)
  78.         {
  79.             targetsShotedDown += down;
  80.             isFirst = false;
  81.             return;
  82.         }
  83.         targetsShotedDown += $"/\\{down}";
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement