Advertisement
simonradev

CaptureMagneto

Jun 21st, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 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.                 string substring = parsedText.Substring(startIndex, indexToSearchAt);
  45.  
  46.                 if (substring.Contains(' ') && substring[substring.Length] == 'M')
  47.                 {
  48.                     magnetosCaptured++;
  49.                 }
  50.                 else
  51.                 {
  52.                     magnetosEscaped++;
  53.                 }
  54.             }
  55.  
  56.             Console.WriteLine(magnetosCaptured);
  57.             Console.WriteLine(magnetosEscaped);
  58.         }
  59.  
  60.         private static int GetIndexToSearchAt(string numberAsString)
  61.         {
  62.             int result = 0;
  63.             foreach (char digit in numberAsString)
  64.             {
  65.                 result += int.Parse(digit.ToString());
  66.             }
  67.  
  68.             return result;
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement