Advertisement
daxtera

Untitled

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