Advertisement
yanass

Star Enigma

Aug 1st, 2019
423
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.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace Star_Enigma
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             int messageNum = int.Parse(Console.ReadLine());
  14.  
  15.             Dictionary<string, List<string>> planetsActions = new Dictionary<string, List<string>>
  16.             {
  17.                 ["Attacked"] = new List<string>(),
  18.                 ["Destroyed"] = new List<string>(),
  19.             };
  20.  
  21.             StringBuilder firstStageDecrypt = new StringBuilder();
  22.             //* matches between 0 and many, ? - between 0 and 1
  23.             string patternMessage = @"@(?<planet>[A-Za-z]+)([^@\-!:>])*:(?<population>[0-9]+)\1*!(?<attack_type>[AD])!\1*->(?<soldier_count>[0-9]+)";
  24.  
  25.             for (int i = 0;i<messageNum;i++)
  26.             {
  27.                 string messageEncrypted = Console.ReadLine();
  28.                 int key = CountCryptoKey(messageEncrypted);
  29.  
  30.                 string firstStage = DecryptFirstStage(messageEncrypted, key).ToString();
  31.  
  32.                 Match decrypted = Regex.Match(firstStage, patternMessage);
  33.  
  34.                 if(decrypted.Success)
  35.                 {
  36.                     string planetName = decrypted.Groups["planet"].Value;
  37.                     string attackType = decrypted.Groups["attack_type"].Value;
  38.  
  39.                     if(attackType == "A")
  40.                     {
  41.                         planetsActions["Attacked"].Add(planetName);
  42.                     }
  43.                     else
  44.                     {
  45.                         planetsActions["Destroyed"].Add(planetName);
  46.                     }
  47.                 }
  48.             }
  49.  
  50.             foreach(var planet in planetsActions)
  51.             {
  52.                 Console.WriteLine($"{planet.Key} planets: {planet.Value.Count}");
  53.                
  54.                 foreach(var planets in planet.Value.OrderBy(x=>x))
  55.                 {
  56.                     Console.WriteLine($"-> {planets}");
  57.                 }
  58.             }
  59.         }
  60.         static StringBuilder DecryptFirstStage(string message, int key)
  61.         {
  62.             StringBuilder firstStageDecrypt = new StringBuilder();
  63.  
  64.             for (int i = 0;i<message.Length;i++)
  65.             {
  66.                 firstStageDecrypt.Append((char)(message[i] - key));
  67.             }
  68.  
  69.             return firstStageDecrypt;
  70.         }
  71.         static int CountCryptoKey(string message)
  72.         {
  73.             string pattern = @"[STARstar]";
  74.  
  75.             MatchCollection lettersKey = Regex.Matches(message, pattern);
  76.  
  77.             return lettersKey.Count();
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement