Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace _09._Star_Enigma
- {
- class Program
- {
- static void Main(string[] args)
- {
- int numberOfMessages = int.Parse(Console.ReadLine());
- List<char> messageList = new List<char>();
- int validLettersCount = 0;
- List<string> atackedPlanets = new List<string>();
- List<string> destroyedPlanets = new List<string>();
- for (int i = 0; i < numberOfMessages; i++)
- {
- string currentMessage = Console.ReadLine();
- messageList = currentMessage.ToList();
- validLettersCount = countOFValidLetters(messageList, validLettersCount, i);
- for (int j = 0; j < messageList.Count; j++)
- {
- messageList[j] -= (char)validLettersCount;
- //here messageList's elements are all shifted countOfValidLetters down
- }
- string pattern = @"(?<planetName>(?<=@)[A-z]+).*[:](?<planetPopulation>[0-9]+)!(?<atackType>[D|A])!->(?<soldierCount>[0-9]+)";
- Regex regex = new Regex(pattern);
- StringBuilder messageListToStringSB = new StringBuilder();
- // first we create a SB to hold our messageListValues to string so we can check them with our regex
- foreach (var letter in messageList)
- {
- messageListToStringSB.Append(letter);
- }
- string messageListToString = messageListToStringSB.ToString();
- Match match = regex.Match(messageListToString);
- if (match.Success)
- {
- string planetName = match.Groups[1].Value;
- string planetPopulation = match.Groups[2].Value;
- string atackType = match.Groups[3].Value;
- string soldierCount = match.Groups[4].Value;
- if (atackType == "A")
- {
- atackedPlanets.Add("-> " + planetName);
- }
- if (atackType == "D")
- {
- destroyedPlanets.Add("-> " + planetName);
- }
- }
- }
- Console.WriteLine($"Attacked planets: {atackedPlanets.Count}");
- foreach (var attackedPlanet in atackedPlanets)
- {
- Console.WriteLine(attackedPlanet);
- }
- Console.WriteLine($"Destroyed planets: {destroyedPlanets.Count}");
- foreach (var destroyedPlanet in destroyedPlanets)
- {
- Console.WriteLine(destroyedPlanet);
- }
- }
- private static int countOFValidLetters(List<char> messageList, int validLettersCount, int i)
- {
- foreach (var message in messageList)
- {
- switch (message)
- {
- case 's':
- validLettersCount++;
- break;
- case 'S':
- validLettersCount++;
- break;
- case 't':
- validLettersCount++;
- break;
- case 'T':
- validLettersCount++;
- break;
- case 'a':
- validLettersCount++;
- break;
- case 'A':
- validLettersCount++;
- break;
- case 'r':
- validLettersCount++;
- break;
- case 'R':
- validLettersCount++;
- break;
- }
- }
- return validLettersCount;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement