Advertisement
Guest User

03. Star Enigma

a guest
May 17th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace _03StarEnigma
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int numOfMessages = int.Parse(Console.ReadLine());
  12.             string pattern = @"([AaSsTtRr])";
  13.             string pattern2 = @"@(?<planet>[A-Za-z]+)[^@\-\\!:>]*:(?<population>\d+)[^@\-\\!:>]*!(?<attack>[A-Z])!->(?<soldiers>\d+)";
  14.             List<string> attackedPlanets = new List<string>();
  15.             List<string> destroyedPlanets = new List<string>();
  16.             for (int i = 1; i <= numOfMessages; i++)
  17.             {
  18.                 string planetName = "";
  19.                 string population = "";
  20.                 string attackType = "";
  21.                 string soldiers = "";
  22.                 string message = Console.ReadLine();
  23.                 var matches = Regex.Matches(message, pattern);
  24.                 int counter = 0;
  25.                 foreach (Match symbol in matches)
  26.                 {
  27.                     counter++;
  28.                 }
  29.                 string decryptedMessage = "";
  30.                 foreach (var symbol in message)
  31.                 {
  32.                     char temp = (char)(symbol - counter);
  33.                     decryptedMessage += temp;
  34.                 }
  35.                 counter = 0;
  36.                 var matches2 = Regex.Matches(decryptedMessage, pattern2);
  37.                 foreach (Match item in matches2)
  38.                 {
  39.                      planetName = item.Groups["planet"].Value;
  40.                      population = item.Groups["population"].Value;
  41.                      attackType = item.Groups["attack"].Value;
  42.                      soldiers = item.Groups["soldiers"].Value;
  43.                 }
  44.                 if (!string.IsNullOrEmpty(soldiers))
  45.                 {
  46.                     if (attackType == "A")
  47.                     {
  48.                         attackedPlanets.Add(planetName);
  49.                     }
  50.                     else
  51.                     {
  52.                         destroyedPlanets.Add(planetName);
  53.                     }
  54.                 }
  55.             }
  56.             attackedPlanets.Sort();
  57.             destroyedPlanets.Sort();
  58.             Console.WriteLine($"Attacked planets: {attackedPlanets.Count}");
  59.             foreach (var item in attackedPlanets)
  60.             {
  61.                 Console.WriteLine($"-> {item}");
  62.             }
  63.             Console.WriteLine($"Destroyed planets: {destroyedPlanets.Count}");
  64.             foreach (var item in destroyedPlanets)
  65.             {
  66.                 Console.WriteLine($"-> {item}");
  67.             }
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement