desislava_topuzakova

04. Star Enigma

Nov 19th, 2021
1,295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. package Regex_Exercise;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class StarEnigma_04 {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         int countMessages = Integer.parseInt(scanner.nextLine());
  11.         String regex = "@(?<planetName>[A-Za-z]+)[^@!:>-]*:(?<population>[0-9]+)[^@!:>-]*!(?<attack>[AD])![^@!:>-]*->(?<soldierCount>[0-9]+)";
  12.         Pattern pattern = Pattern.compile(regex);
  13.  
  14.         List<String> attackersPlanets = new ArrayList<>(); //списък с планетите с тип на атака A
  15.         List<String> destroyedPlanets = new ArrayList<>(); //списък с планетите с тип на атака D
  16.         for (int messageCount = 1; messageCount <= countMessages; messageCount++) {
  17.             String encryptedMessage = scanner.nextLine(); //криптирано съобщение
  18.             String decryptedMessage = getDecryptedMessage(encryptedMessage); //декриптирано
  19.             //декриптирано: PQ@Alderaa1:30000!A!->20000
  20.             Matcher matcher = pattern.matcher(decryptedMessage);
  21.             if (matcher.find()) {
  22.                 String planetName = matcher.group("planetName");
  23.                 //int population = Integer.parseInt(matcher.group("population"));
  24.                 String attackType = matcher.group("attack");  ; //A -> attacker; D - > destroyed
  25.                 //int soldierCount = Integer.parseInt(matcher.group("soldierCount"));
  26.                 if (attackType.equals("A")) {
  27.                     attackersPlanets.add(planetName);
  28.                 } else if (attackType.equals("D")) {
  29.                     destroyedPlanets.add(planetName);
  30.                 }
  31.             }
  32.         }
  33.  
  34.         System.out.println("Attacked planets: " + attackersPlanets.size());
  35.         Collections.sort(attackersPlanets);
  36.         attackersPlanets.forEach(planet -> System.out.println("-> " + planet));
  37.  
  38.         System.out.println("Destroyed planets: " + destroyedPlanets.size());
  39.         Collections.sort(destroyedPlanets);
  40.         destroyedPlanets.forEach(planet -> System.out.println("-> " + planet));
  41.     }
  42.  
  43.     private static String getDecryptedMessage(String encryptedMessage) {
  44.         //криптирано съобщение: STCDoghudd4=63333$D$0A53333
  45.         //брой на спец. букви: 3
  46.         //декриптирано съобщение:
  47.         //1. броя на [s, S, T, t, A, a, R, r] в криптираното съобщение
  48.         int countLetters = getSpecialLettersCount(encryptedMessage);
  49.         StringBuilder decryptedMessage = new StringBuilder();
  50.         //взимаме всяка буква от криптираното съобщение -> ascii
  51.         //ascii - countLetters -> ascii кода на нова буква
  52.         //нова буква -> добавяме към декриптираното съобщение
  53.         for (char symbol : encryptedMessage.toCharArray()) {
  54.             char newSymbol = (char)(symbol - countLetters);
  55.             decryptedMessage.append(newSymbol);
  56.         }
  57.         return decryptedMessage.toString();
  58.     }
  59.  
  60.     private static int getSpecialLettersCount(String encryptedMessage) {
  61.         int counter = 0;
  62.         for (char symbol : encryptedMessage.toLowerCase().toCharArray()) {
  63.             if (symbol == 's' || symbol == 'a' || symbol == 't' || symbol == 'r') {
  64.                 counter++;
  65.             }
  66.         }
  67.         return counter;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment