Advertisement
daxtera

Untitled

Mar 24th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 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. namespace _09._Star_Enigma
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int numberOfMessages = int.Parse(Console.ReadLine());
  13.  
  14. List<char> messageList = new List<char>();
  15. int validLettersCount = 0;
  16.  
  17. List<string> atackedPlanets = new List<string>();
  18. List<string> destroyedPlanets = new List<string>();
  19.  
  20. for (int i = 0; i < numberOfMessages; i++)
  21. {
  22. string currentMessage = Console.ReadLine();
  23. messageList = currentMessage.ToList();
  24. validLettersCount = countOFValidLetters(messageList, validLettersCount, i);
  25.  
  26. for (int j = 0; j < messageList.Count; j++)
  27. {
  28. messageList[j] -= (char)validLettersCount;
  29. //here messageList's elements are all shifted countOfValidLetters down
  30. }
  31.  
  32. string pattern = @"(?<planetName>(?<=@)[A-z]+).*[:](?<planetPopulation>[0-9]+)!(?<atackType>[D|A])!->(?<soldierCount>[0-9]+)";
  33. Regex regex = new Regex(pattern);
  34.  
  35. StringBuilder messageListToStringSB = new StringBuilder();
  36.  
  37. // first we create a SB to hold our messageListValues to string so we can check them with our regex
  38. foreach (var letter in messageList)
  39. {
  40. messageListToStringSB.Append(letter);
  41. }
  42.  
  43. string messageListToString = messageListToStringSB.ToString();
  44.  
  45. Match match = regex.Match(messageListToString);
  46.  
  47. if (match.Success)
  48. {
  49. string planetName = match.Groups[1].Value;
  50. string planetPopulation = match.Groups[2].Value;
  51. string atackType = match.Groups[3].Value;
  52. string soldierCount = match.Groups[4].Value;
  53.  
  54. if (atackType == "A")
  55. {
  56. atackedPlanets.Add("-> " + planetName);
  57. }
  58. if (atackType == "D")
  59. {
  60. destroyedPlanets.Add("-> " + planetName);
  61. }
  62.  
  63. }
  64.  
  65.  
  66. }
  67. Console.WriteLine($"Attacked planets: {atackedPlanets.Count}");
  68.  
  69. foreach (var attackedPlanet in atackedPlanets)
  70. {
  71. Console.WriteLine(attackedPlanet);
  72. }
  73. Console.WriteLine($"Destroyed planets: {destroyedPlanets.Count}");
  74.  
  75. foreach (var destroyedPlanet in destroyedPlanets)
  76. {
  77. Console.WriteLine(destroyedPlanet);
  78. }
  79.  
  80. }
  81.  
  82. private static int countOFValidLetters(List<char> messageList, int validLettersCount, int i)
  83. {
  84. foreach (var message in messageList)
  85. {
  86. switch (message)
  87. {
  88. case 's':
  89. validLettersCount++;
  90. break;
  91. case 'S':
  92. validLettersCount++;
  93. break;
  94. case 't':
  95. validLettersCount++;
  96. break;
  97. case 'T':
  98. validLettersCount++;
  99. break;
  100. case 'a':
  101. validLettersCount++;
  102. break;
  103. case 'A':
  104. validLettersCount++;
  105. break;
  106. case 'r':
  107. validLettersCount++;
  108. break;
  109. case 'R':
  110. validLettersCount++;
  111. break;
  112.  
  113. }
  114. }
  115. return validLettersCount;
  116. }
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement