Advertisement
yanass

Star Enigma Solved

Aug 1st, 2019
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 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.             //dont use groups if you don't want to watch the same thing
  24.             string patternMessage = @"@(?<planet>[A-Za-z]+)([^@\-!:>])*:(?<population>[0-9]+)([^@\-!:>])*!(?<attack_type>[AD])!([^@\-!:>])*->(?<soldier_count>[0-9]+)";
  25.  
  26.             for (int i = 0; i < messageNum; i++)
  27.             {
  28.                 string messageEncrypted = Console.ReadLine();
  29.  
  30.                 int key = CountCryptoKey(messageEncrypted);
  31.  
  32.                 string firstStage = DecryptFirstStage(messageEncrypted, key).ToString();
  33.  
  34.                 Match decrypted = Regex.Match(firstStage, patternMessage);
  35.  
  36.                 if (decrypted.Success)
  37.                 {
  38.                     string planetName = decrypted.Groups["planet"].Value;
  39.                     string attackType = decrypted.Groups["attack_type"].Value;
  40.  
  41.                     if (attackType == "A")
  42.                     {
  43.                         planetsActions["Attacked"].Add(planetName);
  44.                     }
  45.                     else if (attackType == "D")
  46.                     {
  47.                         planetsActions["Destroyed"].Add(planetName);
  48.                     }
  49.                 }
  50.             }
  51.  
  52.             foreach (var planet in planetsActions)
  53.             {
  54.                 Console.WriteLine($"{planet.Key} planets: {planet.Value.Count}");
  55.  
  56.                 foreach (var planets in planet.Value.OrderBy(x => x))
  57.                 {
  58.                     Console.WriteLine($"-> {planets}");
  59.                 }
  60.             }
  61.         }
  62.         static StringBuilder DecryptFirstStage(string message, int key)
  63.         {
  64.             StringBuilder firstStageDecrypt = new StringBuilder();
  65.  
  66.             for (int i = 0; i < message.Length; i++)
  67.             {
  68.                 firstStageDecrypt.Append((char)(message[i] - key));
  69.             }
  70.  
  71.             return firstStageDecrypt;
  72.         }
  73.         static int CountCryptoKey(string message)
  74.         {
  75.             string pattern = @"[STARstar]";
  76.  
  77.             MatchCollection lettersKey = Regex.Matches(message, pattern);
  78.  
  79.             return lettersKey.Count();
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement