desislava_topuzakova

4. Star Enigma - video

Aug 1st, 2021
906
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class StarEnigma_04 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         //1. decrypt message
  9.             //1.1 count of letters [s, t, a, r]
  10.             //1.2 decrypt
  11.         //2. извеличаме данни: planet, population, attack, soldierCount
  12.         int n = Integer.parseInt(scanner.nextLine());
  13.         String regex = "@(?<planet>[A-Za-z]+)[^@\\-!:>]*:(?<population>[0-9]+)[^@\\-!:>]*!(?<attack>[AD])![^@\\-!:>]*->(?<soldierCount>[0-9]+)";
  14.         Pattern pattern = Pattern.compile(regex);
  15.  
  16.         List<String> attackers = new ArrayList<>(); //атакуващи планети
  17.         List<String> destroyed = new ArrayList<>(); //разрушени планети
  18.         for (int i = 1; i <= n; i++) {
  19.             String encryptMessage = scanner.nextLine();
  20.             int countLetters = getSpecialLetters(encryptMessage);
  21.             String decryptedMessage = getDecryptedMessage(encryptMessage, countLetters);
  22.             //PQ@Alderaa1:30000!A!->20000
  23.             Matcher matcher = pattern.matcher(decryptedMessage);
  24.             if(matcher.find()) {
  25.                 String planet = matcher.group("planet");
  26.                 int population = Integer.parseInt(matcher.group("population"));
  27.                 String attackType = matcher.group("attack");
  28.                 //A -> attackers
  29.                 //D -> destroyed
  30.                 if(attackType.equals("A")) {
  31.                     attackers.add(planet);
  32.                 } else if (attackType.equals("D")){
  33.                     destroyed.add(planet);
  34.                 }
  35.                 int soldierCount = Integer.parseInt(matcher.group("soldierCount"));
  36.             }
  37.         }
  38.         //2 списъка с планети: атакуващи + унищожени
  39.         System.out.println("Attacked planets: " + attackers.size());
  40.         Collections.sort(attackers);
  41.         attackers.forEach(planet -> System.out.println("-> " + planet));
  42.  
  43.         System.out.println("Destroyed planets: " + destroyed.size());
  44.         Collections.sort(destroyed);
  45.         destroyed.forEach(planet -> System.out.println("-> " + planet));
  46.     }
  47.  
  48.     private static String getDecryptedMessage(String encryptMessage, int countLetters) {
  49.         StringBuilder decryptMessage = new StringBuilder();
  50.         //encrypt: STCDoghudd4=63333$D$0A53333
  51.         //decrypt: PQ@Alderaa1:30000!A!->20000
  52.         for (int i = 0; i < encryptMessage.length(); i++) {
  53.             char currentSymbol = encryptMessage.charAt(i);
  54.             decryptMessage.append((char)(currentSymbol - countLetters));
  55.         }
  56.         return decryptMessage.toString();
  57.     }
  58.  
  59.     private static int getSpecialLetters(String encryptMessage) {
  60.         //SsTtAaRr -> s t a r
  61.         //STCDoghudd4=63333$D$0A53333 -> stcdoghudd4=63333$d$0a53333
  62.         int counter = 0;
  63.         String lowerCaseMessage = encryptMessage.toLowerCase();
  64.         for (int i = 0; i < lowerCaseMessage.length(); i++) {
  65.             char currentSymbol = lowerCaseMessage.charAt(i);
  66.             if (currentSymbol == 's' ||
  67.                 currentSymbol == 'a' ||
  68.                 currentSymbol == 't' ||
  69.                     currentSymbol == 'r') {
  70.                 counter++;
  71.             }
  72.         }
  73.         return counter;
  74.     }
  75. }
  76.  
RAW Paste Data