Guest User

Untitled

a guest
Mar 26th, 2019
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1. package exercises;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class StarEnigma {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.         int n = Integer.parseInt(scanner.nextLine());
  14.         List<String> attacked = new ArrayList<>();
  15.         List<String> destroyed = new ArrayList<>();
  16.  
  17.         while (n-- > 0) {
  18.  
  19.             String message = scanner.nextLine();
  20.             String decrypted = "";
  21.  
  22.             Pattern pattern = Pattern.compile("[star]", Pattern.CASE_INSENSITIVE);
  23.             Matcher star = pattern.matcher(message);
  24.  
  25.             int count = 0;
  26.             for (int i = 0; i < message.length(); i++) {
  27.                 if (star.find()) {
  28.                     count++;
  29.                 }
  30.             }
  31.             for (int i = 0; i < message.length(); i++) {
  32.                 char symbol = message.charAt(i);
  33.                 decrypted += (char)(symbol - count);
  34.             }
  35.             String regex = ".*?(?<planet>[A-Z][a-z]+)(?:[^@\\-!:>]*):(?<population>(\\d)+)" +
  36.                     "(?:[^@\\-!:>]*)!(?<attack>[AD])!(?:[^@\\-!:>]*)->(?<soldierCount>\\d+)";
  37.  
  38.             Pattern pattern1 = Pattern.compile(regex);
  39.             Matcher planets = pattern1.matcher(decrypted);
  40.  
  41.  
  42.  
  43.             while (planets.find()){
  44.                 String planet = planets.group("planet");
  45.                 String attack = planets.group("attack");
  46.  
  47.                 if ("A".equals(attack)){
  48.                     attacked.add(planet);
  49.  
  50.                 }else {
  51.                     destroyed.add(planet);
  52.  
  53.                 }
  54.             }
  55.         }
  56.         System.out.println(String.format("Attacked planets: %d", attacked.size()));
  57.         attacked.stream().sorted().forEach(planet -> System.out.println("-> " + planet));
  58.  
  59.         System.out.println(String.format("Destroyed planets: %d", destroyed.size()));
  60.         destroyed.stream().sorted().forEach(planet -> System.out.println("-> " + planet));
  61.     }
  62. }
Add Comment
Please, Sign In to add comment