Advertisement
WindFell

Star Enigma

Mar 17th, 2018
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 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. class StarEnigma
  8. {
  9.     static void Main(string[] args)
  10.     {
  11.         int messagesCount = int.Parse(Console.ReadLine());
  12.  
  13.         string star = @"[sStTaArR]";
  14.         string messagePattern = @"@(?<planet>[a-zA-Z]+)[^@\-!:>]*:(?<population>\d+)[^@\-!:>]*!(?<attackType>[AD])![^@\-!:>]*->(?<soldiers>\d+)";
  15.         var planets = new SortedDictionary<string, List<string>>();
  16.  
  17.         planets.Add("A", new List<string>());
  18.         planets.Add("D", new List<string>());
  19.  
  20.         for (int currentMessage = 0; currentMessage < messagesCount; currentMessage++)
  21.         {
  22.             string message = Console.ReadLine();
  23.  
  24.             int key = 0;
  25.             StringBuilder newMessage = new StringBuilder();
  26.  
  27.             foreach (Match starMatch in Regex.Matches(message, star))
  28.             {
  29.                 key++;
  30.             }
  31.  
  32.             foreach (char symbol in message)
  33.             {
  34.                 newMessage.Append(Convert.ToChar(symbol - key));
  35.             }
  36.  
  37.             string decodedMessage = newMessage.ToString();
  38.             Match validMessage = Regex.Match(decodedMessage, messagePattern);
  39.  
  40.             if (validMessage.Success)
  41.             {
  42.                 string attackType = validMessage.Groups["attackType"].Value;
  43.                 string planet = validMessage.Groups["planet"].Value;
  44.  
  45.                 planets[attackType].Add(planet);
  46.             }
  47.         }
  48.  
  49.         foreach (var pair in planets)
  50.         {
  51.             if (pair.Key == "A")
  52.             {
  53.                 Console.WriteLine($"Attacked planets: {pair.Value.Count}");
  54.             }
  55.             else
  56.             {
  57.                 Console.WriteLine($"Destroyed planets: {pair.Value.Count}");
  58.             }
  59.  
  60.             foreach (string planet in pair.Value.OrderBy(p => p))
  61.             {
  62.                 Console.WriteLine($"-> {planet}");
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement