Advertisement
gospod1978

Regular Expression/Star Enigma

Oct 11th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Text.RegularExpressions;
  8.  
  9.  
  10. namespace Orders
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             int countMessages = int.Parse(Console.ReadLine());
  17.  
  18.             string firstPattern = @"[starSTAR]";
  19.             List<string> attackPlanets = new List<string>();
  20.             List<string> destroiedPlanets = new List<string>();
  21.  
  22.  
  23.  
  24.             for (int i = 0; i < countMessages; i++)
  25.             {
  26.                 string messages = Console.ReadLine();
  27.  
  28.                 MatchCollection letterMatches = Regex.Matches(messages, firstPattern);
  29.  
  30.                 int countOfLetters = letterMatches.Count;
  31.  
  32.                 string newMessage = string.Empty;
  33.  
  34.                
  35.                 foreach (var letter in messages)
  36.                 {
  37.                     newMessage += (char)(letter - countOfLetters);
  38.  
  39.                 }
  40.                
  41.                 string planetPattern = @"@(?<name>[A-Za-z]+)[^@\-!:>]*:(?<population>[0-9]+)[^@\-!:>]*!(?<type>A|D*)![^@\-!:>]*->(?<soldier>[0-9])+";
  42.  
  43.                 Match planetArgs = Regex.Match(newMessage, planetPattern);
  44.  
  45.                 if (planetArgs.Success)
  46.                 {
  47.                     string planetName = planetArgs.Groups["name"].Value;
  48.                     string type = planetArgs.Groups["type"].Value;
  49.  
  50.                     if (type == "A")
  51.                     {
  52.                         attackPlanets.Add(planetName);
  53.                     }
  54.                     else if (type == "D")
  55.                     {
  56.                         destroiedPlanets.Add(planetName);
  57.                     }
  58.                 }
  59.             }
  60.  
  61.             Console.WriteLine($"Attacked planets: {attackPlanets.Count}");
  62.  
  63.             foreach (var attackPl in attackPlanets.OrderBy(x => x))
  64.             {
  65.                 Console.WriteLine($"-> {attackPl}");
  66.             }
  67.  
  68.             Console.WriteLine($"Destroyed planets: {destroiedPlanets.Count}");
  69.  
  70.             foreach (var destoyedPl in destroiedPlanets.OrderBy(x => x))
  71.             {
  72.                 Console.WriteLine($"-> {destoyedPl}");
  73.             }
  74.  
  75.         }
  76.  
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement