kalitarix

Star enigma

Apr 3rd, 2019
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace StarEnigma_03_18
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int numberOfMessages = int.Parse(Console.ReadLine());
  12.  
  13.             List<string> attackedPlanets = new List<string>();
  14.             List<string> destroyedPlanets = new List<string>();
  15.  
  16.             for (int currentMessage = 1; currentMessage <= numberOfMessages; currentMessage++)
  17.             {
  18.                 string message = Console.ReadLine();
  19.  
  20.                 string temp = message.ToLower();
  21.                 int count = 0;
  22.  
  23.                 for (int index = 0; index < temp.Length; index++)
  24.                 {
  25.                     char symbol = temp[index];
  26.  
  27.                     if (symbol == 's' || symbol == 't' || symbol == 'a' || symbol == 'r')
  28.                     {
  29.                         count++;
  30.                     }
  31.                 }
  32.  
  33.                 string decoded = string.Empty;
  34.  
  35.                 for (int index = 0; index < message.Length; index++)
  36.                 {
  37.                     decoded += (char)(message[index] - count);
  38.                 }
  39.  
  40.                 int indexOfVerbatim = decoded.IndexOf('@');
  41.                 int indexOfPoints = decoded.IndexOf(':');
  42.                 int indexOfExclamation = decoded.IndexOf('!');
  43.                 int indexOfBigger = decoded.IndexOf('>');
  44.  
  45.                 if (indexOfVerbatim < 0 || indexOfPoints < 0 || indexOfExclamation < 0 || indexOfBigger < 0)
  46.                 {
  47.                     continue;
  48.                 }
  49.  
  50.                 if (indexOfVerbatim > indexOfPoints || indexOfPoints > indexOfExclamation || indexOfExclamation + 2 > indexOfBigger)
  51.                 {
  52.                     continue;
  53.                 }
  54.                
  55.                 if (Char.IsDigit(decoded[indexOfPoints + 1]) == false
  56.                     || Char.IsDigit(decoded[indexOfBigger + 1]) == false)
  57.                 {
  58.                     continue;
  59.                 }
  60.  
  61.                 string planetName = null;
  62.  
  63.                 for (int i = indexOfVerbatim + 1; i < indexOfPoints; i++)
  64.                 {
  65.                     char symbolOfPlanet = decoded[i];
  66.  
  67.                     if ((symbolOfPlanet >= 'A' && symbolOfPlanet <= 'Z')
  68.                         || (symbolOfPlanet >= 'a' && symbolOfPlanet <= 'z'))
  69.                     {
  70.                         planetName += symbolOfPlanet;
  71.                     }
  72.                     else
  73.                     {
  74.                         break;
  75.                     }
  76.                 }
  77.  
  78.                 char attackType = decoded[indexOfExclamation + 1];
  79.  
  80.                 if (planetName != null && decoded[indexOfExclamation + 2] == '!')
  81.                 {
  82.                     if (attackType == 'A')
  83.                     {
  84.                         attackedPlanets.Add(planetName);
  85.                     }
  86.                     else if (attackType == 'D')
  87.                     {
  88.                         destroyedPlanets.Add(planetName);
  89.                     }
  90.                 }
  91.             }
  92.  
  93.             attackedPlanets = attackedPlanets.OrderBy(p => p).ToList();
  94.             destroyedPlanets = destroyedPlanets.OrderBy(p => p).ToList();
  95.  
  96.             Console.WriteLine($"Attacked planets: {attackedPlanets.Count}");
  97.  
  98.             foreach (string planet in attackedPlanets)
  99.             {
  100.                 Console.WriteLine($"-> {planet}");
  101.             }
  102.  
  103.             Console.WriteLine($"Destroyed planets: {destroyedPlanets.Count}");
  104.  
  105.             foreach (string planet in destroyedPlanets)
  106.             {
  107.                 Console.WriteLine($"-> {planet}");
  108.             }
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment