Advertisement
deleriumbg

Star Enigma

Jun 20th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace Star_Enigma
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int messagesCount = int.Parse(Console.ReadLine());
  14.             List<string> attackedPlanets = new List<string>();
  15.             List<string> destroyedPlanets = new List<string>();
  16.  
  17.             for (int currentMessage = 0; currentMessage < messagesCount; currentMessage++)
  18.             {
  19.                 string input = Console.ReadLine();
  20.                 int count = 0;
  21.                 for (int i = 0; i < input.Length; i++)
  22.                 {
  23.                     if (input[i] == 's' || input[i] == 'S' || input[i] == 't' || input[i] == 'T' ||
  24.                         input[i] == 'a' || input[i] == 'A' || input[i] == 'r' || input[i] == 'R')
  25.                     {
  26.                         count++;
  27.                     }
  28.                 }
  29.  
  30.                 StringBuilder decryptedMessage = new StringBuilder();
  31.                 for (int i = 0; i < input.Length; i++)
  32.                 {
  33.                     int charAscii = input[i] - count;
  34.                     decryptedMessage.Append((char)charAscii);
  35.                 }
  36.                 string decryptedString = decryptedMessage.ToString();
  37.  
  38.                 // The planet name starts after '@' and contains only letters from the Latin alphabet.
  39.                 // The planet population starts after ':' and is an Integer;
  40.                 // The attack type may be "A"(attack) or "D"(destruction) and must be surrounded by "!"(exclamation mark).
  41.                 // The soldier count starts after "->" and should be an Integer.
  42.  
  43.                 string pattern = @"@([A-Za-z]+).*:([\d]+).*!([AD])!.*->([\d]+)";
  44.  
  45.                 MatchCollection matches = Regex.Matches(decryptedString, pattern);
  46.                 foreach (Match match in matches)
  47.                 {
  48.                     if (match.Groups[3].Value == "A")
  49.                     {
  50.                         attackedPlanets.Add(match.Groups[1].Value);
  51.                     }
  52.                     else if (match.Groups[3].Value == "D")
  53.                     {
  54.                         destroyedPlanets.Add(match.Groups[1].Value);
  55.                     }
  56.                 }
  57.             }
  58.  
  59.             Console.WriteLine($"Attacked planets: {attackedPlanets.Count}");
  60.             if (attackedPlanets.Count > 0)
  61.             {
  62.                 foreach (var planet in attackedPlanets.OrderBy(x => x))
  63.                 {
  64.                     Console.WriteLine($"-> {planet}");
  65.                 }
  66.             }
  67.             Console.WriteLine($"Destroyed planets: {destroyedPlanets.Count}");
  68.             if (destroyedPlanets.Count > 0)
  69.             {
  70.                 foreach (var planet in destroyedPlanets.OrderBy(x => x))
  71.                 {
  72.                     Console.WriteLine($"-> {planet}");
  73.                 }
  74.             }
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement