Advertisement
dragonbs

04. Star Enigma

Mar 23rd, 2023
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 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.  
  7. internal class Program
  8. {
  9.     static void Main()
  10.     {
  11.         int numberOfMessages = int.Parse(Console.ReadLine());
  12.         List<string> attackedPlanets = new List<string>();
  13.         List<string> destroyedPlanets = new List<string>();
  14.  
  15.         for (int i = 0; i < numberOfMessages; i++)
  16.         {
  17.             string input = Console.ReadLine();
  18.             string decodedInput = GetDecodedInputMessage(input);
  19.             string regexPattern = @"@(?<planet>[A-Za-z]+)[^@:!\->]*:(?<population>\d+)[^@:!\->]*!(?<action>[A|D])![^@:!\->]*->(?<soldiers>\d+)";
  20.             MatchCollection matchMessage = Regex.Matches(decodedInput, regexPattern);
  21.             if (matchMessage.Count > 0)
  22.             {
  23.                 if (matchMessage[0].Groups["action"].Value == "A")
  24.                     attackedPlanets.Add(matchMessage[0].Groups["planet"].Value);
  25.                 else
  26.                     destroyedPlanets.Add(matchMessage[0].Groups["planet"].Value);
  27.             }
  28.         }
  29.  
  30.         Console.WriteLine($"Attacked planets: {attackedPlanets.Count}");
  31.         if (attackedPlanets.Count > 0)
  32.             Console.WriteLine(String.Join(Environment.NewLine, attackedPlanets.OrderBy(x => x).Select(x => $"-> {x}")));
  33.  
  34.         Console.WriteLine($"Destroyed planets: {destroyedPlanets.Count}");
  35.         if (destroyedPlanets.Count > 0)
  36.             Console.WriteLine(String.Join(Environment.NewLine, destroyedPlanets.OrderBy(x => x).Select(x => $"-> {x}")));
  37.     }
  38.  
  39.     static string GetDecodedInputMessage(string input)
  40.     {
  41.         MatchCollection starLetters = Regex.Matches(input, @"[starSTAR]");
  42.         StringBuilder sb = new StringBuilder();
  43.         foreach (char c in input)
  44.             sb.Append((char)(c - starLetters.Count));
  45.  
  46.         return sb.ToString();
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement