Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace CapturingMagnetoMan
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- public class StartUp
- {
- public static void Main()
- {
- //pattern for test
- /*[asdasdasd[dasdasd<2M2>asda]s]dMasdds][asdasd<3M3>sadasdad]
- * [asdasdasd[dasda][<M>]]]asda]s]d]asdds]
- Capture MAGNETO!*/
- string pattern = @"\[[^[\]]*?<(\d*)M(\d*)>.*?\]";
- Regex magnetoTrap = new Regex(pattern);
- StringBuilder text = new StringBuilder();
- string inputLine;
- while ((inputLine = Console.ReadLine().Trim() != "Capture MAGNETO!")
- {
- text.Append(inputLine);
- }
- string parsedText = text.ToString();
- int magnetosCaptured = 0;
- int magnetosEscaped = 0;
- MatchCollection allMatches = magnetoTrap.Matches(parsedText);
- foreach (Match match in allMatches)
- {
- string numberAsString = match.Groups[1].Value + match.Groups[2].Value;
- int indexToSearchAt = GetIndexToSearchAt(numberAsString);
- string wholeMatch = match.Groups[0].Value;
- int indexOfMatch = parsedText.IndexOf(wholeMatch);
- int startIndex = indexOfMatch + wholeMatch.Length;
- //ДА СЕ НАПРАВИ ВАЛИДАЦИЯ ЗА СЪБСТРИНГА ЗА ДА НЕ ИЗЛЕЗЕ ИЗВЪН СТИНГА
- string substring = parsedText.Substring(startIndex, indexToSearchAt);
- if (substring.Contains(' ') && substring[substring.Length] == 'M')
- {
- magnetosCaptured++;
- }
- else
- {
- magnetosEscaped++;
- }
- }
- Console.WriteLine(magnetosCaptured);
- Console.WriteLine(magnetosEscaped);
- }
- private static int GetIndexToSearchAt(string numberAsString)
- {
- int result = 0;
- foreach (char digit in numberAsString)
- {
- result += int.Parse(digit.ToString());
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement