Advertisement
N_Damyanov

05. Star Enigma

Dec 13th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.78 KB | None | 0 0
  1. package RegularExpressions;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class StarEnigma {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         int lines = Integer.parseInt(scanner.nextLine());
  11.         List<String> attacked = new ArrayList<>();
  12.         List<String> destroyed = new ArrayList<>();
  13.  
  14.  
  15.         for (int i = 0; i < lines; i++) {
  16.             String message = scanner.nextLine();
  17.             Pattern pattern = Pattern.compile("[starSTAR]");
  18.             Matcher matcher = pattern.matcher(message);
  19.             int decryptionKey = 0;
  20.             while (matcher.find()) {
  21.                 decryptionKey++;
  22.             }
  23.  
  24.             StringBuilder decryptedMessage = new StringBuilder();
  25.             for (int j = 0; j < message.length(); j++) {
  26.                 decryptedMessage.append((char)(message.charAt(j) - decryptionKey));
  27.             }
  28.             pattern = Pattern.compile("@([A-Za-z]+)[^@\\-!:>]*:(\\d+)[^@\\-!:>]*!(A|D)![^@\\-!:>]*->(\\d+)");
  29.             matcher = pattern.matcher(decryptedMessage);
  30.             if (matcher.find()) {
  31.                 if (matcher.group(3).equals("A")) {
  32.                     attacked.add(matcher.group(1));
  33.                 } else {
  34.                     destroyed.add(matcher.group(1));
  35.                 }
  36.             }
  37.         }
  38.         Collections.sort(attacked);
  39.         Collections.sort(destroyed);
  40.         System.out.printf("Attacked planets: %d%n", attacked.size());
  41.         for (String s : attacked) {
  42.             System.out.printf("-> %s%n", s);
  43.         }
  44.         System.out.printf("Destroyed planets: %d%n", destroyed.size());
  45.         for (String s : destroyed) {
  46.             System.out.printf("-> %s%n", s);
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement