JulianJulianov

07.RegularExpressionsExercise-Star Enigma

May 7th, 2020
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.28 KB | None | 0 0
  1. 04. *Star Enigma
  2. The war is in its peak, but you, young Padawan, can turn the tides with your programming skills. You are tasked to create a program to decrypt the messages of The Order and prevent the death of hundreds of lives.
  3. You will receive several messages, which are encrypted using the legendary star enigma. You should decrypt the messages, following these rules:
  4. To properly decrypt a message, you should count all the letters [s, t, a, r]case insensitive and remove the count from the current ASCII value of each symbol of the encrypted message.
  5. After decryption:
  6. Each message should have a planet name, population, attack type ('A', as attack or 'D', as destruction) and soldier count.
  7. The planet name starts after '@' and contains only letters from the Latin alphabet.
  8. The planet population starts after ':' and is an Integer;
  9. The attack type may be "A"(attack) or "D"(destruction) and must be surrounded by "!" (exclamation mark).
  10. The soldier count starts after "->" and should be an Integer.
  11. The order in the message should be: planet name -> planet population -> attack type -> soldier count. Each part can be separated from the others by any character except: '@', '-', '!', ':' and '>'.
  12. Input / Constraints
  13. • The first line holds n – the number of messages– integer in range [1100];
  14. • On the next n lines, you will be receiving encrypted messages.
  15. Output
  16. After decrypting all messages, you should print the decrypted information in the following format:
  17. First print the attacked planets, then the destroyed planets.
  18. "Attacked planets: {attackedPlanetsCount}"
  19. "-> {planetName}"
  20. "Destroyed planets: {destroyedPlanetsCount}"
  21. "-> {planetName}"
  22. The planets should be ordered by name alphabetically.
  23. Examples
  24. Input                         Output                Comments
  25. 2                             Attacked planets: 1   We receive two messages, to decrypt them we calculate the key:
  26. STCDoghudd4=63333$D$0A53333   -> Alderaa            First message has decryption key 3. So we substract from each characters code 3.
  27. EHfsytsnhf?8555&I&2C9555SR    Destroyed planets: 1  PQ@Alderaa1:30000!A!->20000 The second message has key 5.@Cantonica:3000!D!->4000NM
  28.                               -> Cantonica          Both messages are valid and they contain planet,
  29.                                                     population, attack type and soldiers count.
  30.                                                     After decrypting all messages we print each planet according the format given.
  31.  
  32. 3                                    Attacked planets: 0       We receive three messages. Message one is decrypted with key 4:
  33. tt(''DGsvywgerx>6444444444%H%1B9444  Destroyed planets: 2      pp$##@Coruscant:2000000000!D!->5000
  34. GQhrr|A977777(H(TTTT                 -> Cantonica              Message two is decrypted with key 7: @Jakku:200000!A!MMMM
  35. EHfsytsnhf?8555&I&2C9555SR           -> Coruscant              This is invalid message, missing soldier count, so we continue.
  36.                                                                The third message has key 5. @Cantonica:3000!D!->4000NM
  37. using System;
  38. using System.Text.RegularExpressions;
  39. using System.Collections.Generic;
  40. using System.Linq;
  41.  
  42. public class Program
  43. {
  44.     public static void Main()
  45.     {
  46.          var countMessages = int.Parse(Console.ReadLine());
  47.            
  48.             var counterA = 0;
  49.             var counterD = 0;
  50.             var listAttackedPlanets = new List<string>();
  51.             var listDestroyedPlanets = new List<string>();
  52.             for (int i = 1; i <= countMessages; i++)
  53.             {
  54.                 var encryptedMessage = Console.ReadLine();
  55.                 var patternKey = @"[starSTAR]";
  56.                 var matchKeyLetters = Regex.Matches(encryptedMessage, patternKey);
  57.                 var decryptionKey = int.Parse(matchKeyLetters.Count.ToString());
  58.                 var decryptedMessage = "";
  59.                 foreach (var encryptedSymbol in encryptedMessage)
  60.                 {
  61.                     decryptedMessage += (char)(encryptedSymbol - decryptionKey);
  62.                 }
  63.                 var pattern = @"@(?<Planet>[A-za-z]+)\d*[^@\-!:>]*:(?<Population>\d+)[^@\-!:>]*!(?<Action>[AD])![^@\-!:>]*->(?<Soldier>\d+)";
  64.                 var matchMessage = Regex.Match(decryptedMessage, pattern);
  65.  
  66.                 if (matchMessage.Success)
  67.                 {
  68.                     if (matchMessage.Groups["Action"].Value == "A")
  69.                     {
  70.                         counterA++;
  71.                         var attackedPlanets = $"-> {matchMessage.Groups["Planet"].Value}";
  72.                         listAttackedPlanets.Add(attackedPlanets);
  73.                     }
  74.                     else
  75.                     {
  76.                         counterD++;
  77.                         var destroyedPlanets = $"-> {matchMessage.Groups["Planet"].Value}";
  78.                         listDestroyedPlanets.Add(destroyedPlanets);
  79.                     }
  80.                 }
  81.  
  82.             }
  83.             Console.WriteLine($"Attacked planets: {counterA}");
  84.             if (counterA != 0)
  85.             {
  86.                 Console.WriteLine(string.Join("\n", listAttackedPlanets.OrderBy(x => x)));
  87.             }
  88.             Console.WriteLine($"Destroyed planets: {counterD}");
  89.             Console.WriteLine(string.Join("\n", listDestroyedPlanets.OrderBy(x => x)));
  90.     }
  91. }
Add Comment
Please, Sign In to add comment