TheBulgarianWolf

Star Enigma Regex

Apr 16th, 2021
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace StarEnigma
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             List<string> attackedPlanets = new List<string>();
  13.             List<string> destroyedPlanets = new List<string>();
  14.             Regex reg = new Regex(@"[STARstar]");
  15.             Regex planetReg = new Regex(@"[^@\-!:>]*\@(?<planet>[A-Za-z]+)[^@\-!:>]*\:[^@\-!:>](?<population>\d+)[^@\-!:>]*\![^@\-!:>]*(?<type>[AD])[^@\-!:>]*\!->[^@\-!:>]*(?<soldiers>\d+)[^@\-!:>]*");
  16.             int numberOfLines = int.Parse(Console.ReadLine());
  17.            
  18.             for(int i = 0; i < numberOfLines; i++)
  19.             {
  20.                 StringBuilder sb = new StringBuilder();
  21.                 string line = Console.ReadLine();
  22.                 MatchCollection matches = reg.Matches(line);
  23.                 int numberOfLetters = matches.Count;
  24.                 char[] arr = line.ToCharArray();
  25.                 foreach(char ch in arr)
  26.                 {
  27.                     int charCode = (int)ch;
  28.                     int newCode = charCode - numberOfLetters;
  29.                     char newChar = (char)newCode;
  30.                     sb.Append(newChar);
  31.                 }
  32.                 string decripted = sb.ToString();
  33.                 Match finalMatches = planetReg.Match(decripted);
  34.                 string planet = finalMatches.Groups["planet"].Value;
  35.                 int population = int.Parse(finalMatches.Groups["population"].Value);
  36.                 string attackType = finalMatches.Groups["type"].Value;
  37.                 int numberOfSoldiers = int.Parse(finalMatches.Groups["soldiers"].Value);
  38.                 if(attackType == "D")
  39.                 {
  40.                     destroyedPlanets.Add(planet);
  41.                 }
  42.                 else if(attackType == "A")
  43.                 {
  44.                     attackedPlanets.Add(planet);
  45.                 }
  46.             }
  47.  
  48.             Console.WriteLine("Attacked planets: " + attackedPlanets.Count);
  49.             foreach(string planet in attackedPlanets)
  50.             {
  51.                 Console.WriteLine("-> " + planet);
  52.             }
  53.             Console.WriteLine("Destroyed planets: " + destroyedPlanets.Count);
  54.             foreach(string planet in destroyedPlanets)
  55.             {
  56.                 Console.WriteLine("-> " + planet);
  57.             }
  58.         }
  59.     }
  60. }
  61.  
Add Comment
Please, Sign In to add comment