Advertisement
Lyubohd

Star Enigma

Jul 24th, 2020
1,008
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         int n = Integer.parseInt(scanner.nextLine());
  13.  
  14.         List<String> attacked = new ArrayList<>();
  15.         List<String> destroyed = new ArrayList<>();
  16.  
  17.         for (int i = 0; i < n; i++) {
  18.  
  19.             String message = scanner.nextLine();
  20.             int add = 0;
  21.  
  22.             for (int j = 0; j < message.length(); j++) {
  23.                 char symbol = message.toLowerCase().charAt(j);
  24.  
  25.                 if (symbol == 's' || symbol == 't' || symbol == 'a' || symbol == 'r') {
  26.                     add++;
  27.                 }
  28.             }
  29.             StringBuilder decryptedMesaage = new StringBuilder();
  30.             for (int j = 0; j < message.length(); j++) {
  31.                 char symbol = (char) (message.charAt(j) - add);
  32.                 decryptedMesaage.append(symbol);
  33.             }
  34.  
  35.             Pattern pattern = Pattern.compile(".*?@(?<planet>[a-zA-Z]+)[^@\\-!:>]*:(\\d+)[^@\\-!:>]*!(?<type>[AD])![^@\\-!:>]*->(\\d+)");
  36.             Matcher matcher = pattern.matcher(decryptedMesaage);
  37.  
  38.             if (matcher.find()) {
  39.                 String planet = matcher.group("planet");
  40.                 String type = matcher.group("type");
  41.  
  42.                 if (type.equals("A")) {
  43.                     attacked.add(planet);
  44.                 } else if (type.equals("D")) {
  45.                     destroyed.add(planet);
  46.                 }
  47.             }
  48.         }
  49.         System.out.println("Attacked planets: " + attacked.size());
  50.         if (!attacked.isEmpty()) {
  51.             attacked
  52.                     .stream()
  53.                     .sorted((e1, e2) -> e1.compareTo(e2))
  54.                     .forEach(e -> System.out.println("-> " + e));
  55.         }
  56.  
  57.         System.out.println("Destroyed planets: " + destroyed.size());
  58.         if (!destroyed.isEmpty()) {
  59.             destroyed
  60.                     .stream()
  61.                     .sorted((e1, e2) -> e1.compareTo(e2))
  62.                     .forEach(e -> System.out.println("-> " + e));
  63.         }
  64.     }
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement