petyaminkova91

Untitled

Nov 19th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. namespace _04_StarEnigma
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. int numberOfMessages = int.Parse(Console.ReadLine());
  13.  
  14. SortedDictionary<string, List<string>> planets = new SortedDictionary<string, List<string>>();
  15.  
  16. for (int i = 0; i < numberOfMessages; i++)
  17. {
  18. string message = Console.ReadLine();
  19.  
  20. string lettersPattern = @"[SsTtAaRr]";
  21.  
  22. var lettersMatch = Regex.Matches(message, lettersPattern);
  23.  
  24. int lettersCount = lettersMatch.Count;
  25.  
  26. char[] messageArray = message.ToCharArray();
  27.  
  28. for (int j = 0; j < messageArray.Length; j++)
  29. {
  30. messageArray[j] -= (char)lettersCount;
  31. }
  32.  
  33. message = new string(messageArray);
  34.  
  35. string pattern = @"@(?<planet>[A-Za-z]+)[^@:->!]:(?<populatin>\d+)!(?<attackType>[AD])!->(?<soldierCount>\d+)";
  36. var matches = Regex.Matches(message, pattern);
  37.  
  38. foreach (Match match in matches)
  39. {
  40. string planetName = match.Groups["planet"].Value;
  41. string attackType = match.Groups["attackType"].Value;
  42.  
  43. if (!planets.ContainsKey(attackType))
  44. {
  45. planets.Add(attackType, new List<string>());
  46. }
  47. planets[attackType].Add(planetName);
  48. }
  49. }
  50.  
  51. Console.WriteLine($"Attacked planets: {planets["A"].Count}");
  52.  
  53. Console.WriteLine(String.Join("\n", planets["A"]));
  54.  
  55. Console.WriteLine($"Destroyed planets: {planets["D"].Count}");
  56.  
  57. foreach (var kvp in planets.Where(x => x.Key == "D"))
  58. {
  59. foreach (var item in kvp.Key)
  60. {
  61. Console.WriteLine($"-> {kvp.Value}");
  62. }
  63. }
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment