Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text.RegularExpressions;
- using System.Linq;
- using System.Collections.Generic;
- namespace _04_StarEnigma
- {
- class Program
- {
- static void Main(string[] args)
- {
- int numberOfMessages = int.Parse(Console.ReadLine());
- SortedDictionary<string, List<string>> planets = new SortedDictionary<string, List<string>>();
- for (int i = 0; i < numberOfMessages; i++)
- {
- string message = Console.ReadLine();
- string lettersPattern = @"[SsTtAaRr]";
- var lettersMatch = Regex.Matches(message, lettersPattern);
- int lettersCount = lettersMatch.Count;
- char[] messageArray = message.ToCharArray();
- for (int j = 0; j < messageArray.Length; j++)
- {
- messageArray[j] -= (char)lettersCount;
- }
- message = new string(messageArray);
- string pattern = @"@(?<planet>[A-Za-z]+)[^@:->!]:(?<populatin>\d+)!(?<attackType>[AD])!->(?<soldierCount>\d+)";
- var matches = Regex.Matches(message, pattern);
- foreach (Match match in matches)
- {
- string planetName = match.Groups["planet"].Value;
- string attackType = match.Groups["attackType"].Value;
- if (!planets.ContainsKey(attackType))
- {
- planets.Add(attackType, new List<string>());
- }
- planets[attackType].Add(planetName);
- }
- }
- Console.WriteLine($"Attacked planets: {planets["A"].Count}");
- Console.WriteLine(String.Join("\n", planets["A"]));
- Console.WriteLine($"Destroyed planets: {planets["D"].Count}");
- foreach (var kvp in planets.Where(x => x.Key == "D"))
- {
- foreach (var item in kvp.Key)
- {
- Console.WriteLine($"-> {kvp.Value}");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment