Advertisement
simonradev

CaptureMagneto

Jun 21st, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. namespace CapturingMagnetoMan
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using System.Text.RegularExpressions;
  8.     using System.Threading.Tasks;
  9.    
  10.     public class StartUp
  11.     {
  12.         public static void Main()
  13.         {
  14.             //pattern for test
  15.             /*[asdasdasd[dasdasd<2M2>asda]s]dMasdds][asdasd<3M3>sadasdad]
  16.              * [asdasdasd[dasda][<M>]]]asda]s]d]asdds]
  17.                Capture MAGNETO!*/
  18.             string pattern = @"\[[^[\]]*?<(\d*)M(\d*)>.*?\]";
  19.             Regex magnetoTrap = new Regex(pattern);
  20.  
  21.             StringBuilder text = new StringBuilder();
  22.             string inputLine;
  23.             while ((inputLine = Console.ReadLine().Trim() != "Capture MAGNETO!")
  24.             {
  25.                 text.Append(inputLine);
  26.             }
  27.  
  28.             string parsedText = text.ToString();
  29.  
  30.             int magnetosCaptured = 0;
  31.             int magnetosEscaped = 0;
  32.  
  33.             MatchCollection allMatches = magnetoTrap.Matches(parsedText);
  34.             foreach (Match match in allMatches)
  35.             {
  36.                 string numberAsString = match.Groups[1].Value + match.Groups[2].Value;
  37.                 int indexToSearchAt = GetIndexToSearchAt(numberAsString);
  38.  
  39.                 string wholeMatch = match.Groups[0].Value;
  40.                 int indexOfMatch = parsedText.IndexOf(wholeMatch);
  41.  
  42.                 int startIndex = indexOfMatch + wholeMatch.Length;
  43.  
  44.                 //ДА СЕ НАПРАВИ ВАЛИДАЦИЯ ЗА СЪБСТРИНГА ЗА ДА НЕ ИЗЛЕЗЕ ИЗВЪН СТИНГА
  45.                 string substring = parsedText.Substring(startIndex, indexToSearchAt);
  46.  
  47.                 if (substring.Contains(' ') && substring[substring.Length] == 'M')
  48.                 {
  49.                     magnetosCaptured++;
  50.                 }
  51.                 else
  52.                 {
  53.                     magnetosEscaped++;
  54.                 }
  55.             }
  56.  
  57.             Console.WriteLine(magnetosCaptured);
  58.             Console.WriteLine(magnetosEscaped);
  59.         }
  60.  
  61.         private static int GetIndexToSearchAt(string numberAsString)
  62.         {
  63.             int result = 0;
  64.             foreach (char digit in numberAsString)
  65.             {
  66.                 result += int.Parse(digit.ToString());
  67.             }
  68.  
  69.             return result;
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement