Advertisement
bullit3189

Star Enigma - Regex

Feb 24th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace _09StarEnigma
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int n = int.Parse(Console.ReadLine());
  13.  
  14. List<string> attacked = new List<string>();
  15. List<string> destroyed = new List<string>();
  16.  
  17. for (int i = 0; i < n; i++)
  18. {
  19. string encrypted = Console.ReadLine();
  20. int key = 0;
  21.  
  22. for (int j = 0; j < encrypted.Length; j++)
  23. {
  24. char currSymbol = encrypted[j];
  25.  
  26. if (currSymbol == 'S' || currSymbol =='s' || currSymbol == 'T' || currSymbol == 't' || currSymbol == 'A' || currSymbol == 'a' || currSymbol == 'R' || currSymbol == 'r')
  27. {
  28. key++;
  29. }
  30. }
  31.  
  32. string decrypted = string.Empty;
  33.  
  34. for (int k = 0; k < encrypted.Length; k++)
  35. {
  36. char currSymbol = encrypted[k];
  37.  
  38. int revised = (int)currSymbol - key;
  39.  
  40. decrypted +=(char) revised;
  41. }
  42.  
  43. string regex = @"[^@,\-!:>]*@(?<planet>[A-Za-z]+)[^@,\-!:>]*:(?<population>\d+)[^@,\-!:>]*!(?<attack>[A-Z])![^@,\-!:>]*->(?<soldiers>\d+)";
  44.  
  45. if (Regex.IsMatch(decrypted,regex))
  46. {
  47. Match match = Regex.Match(decrypted, regex);
  48.  
  49. string planet = match.Groups["planet"].Value;
  50. int population = int.Parse(match.Groups["population"].Value);
  51. string attackType = match.Groups["attack"].Value;
  52. int soldiers = int.Parse(match.Groups["soldiers"].Value);
  53.  
  54. if (attackType == "A")
  55. {
  56. attacked.Add(planet);
  57. }
  58. else if (attackType == "D")
  59. {
  60. destroyed.Add(planet);
  61. }
  62. }
  63. }
  64.  
  65. Console.WriteLine($"Attacked planets: {attacked.Count}");
  66.  
  67. if (attacked.Count>0)
  68. {
  69. var filtered=attacked.OrderBy(x => x);
  70.  
  71. foreach (var item in filtered)
  72. {
  73. Console.WriteLine("-> " + item);
  74. }
  75. }
  76.  
  77. Console.WriteLine($"Destroyed planets: {destroyed.Count}");
  78.  
  79. if (destroyed.Count>0)
  80. {
  81. var filtered=destroyed.OrderBy(x => x);
  82.  
  83. foreach (var item in filtered)
  84. {
  85. Console.WriteLine("-> " + item);
  86. }
  87. }
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement