Advertisement
Kathy2905

StarEnigma

Mar 26th, 2019
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. package exercises;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9.  
  10. public class StarEnigma {
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13. //STCDoghudd4=63333$D$0A53333
  14. //EHfsytsnhf?8555&I&2C9555SR
  15. //Pattern mypattern = Pattern.compile(MYREGEX, Pattern.CASE_INSENSITIVE);
  16.         int n = Integer.parseInt(scanner.nextLine());
  17.  
  18.         int attackedPlanets = 0;
  19.         int destroyedPlanets = 0;
  20.  
  21.         List<String> attacked = new ArrayList<>();
  22.         List<String> destroyed = new ArrayList<>();
  23.  
  24.  
  25.         while (n-- > 0) {
  26.  
  27.             String message = scanner.nextLine();
  28.             String decrypted = "";
  29.  
  30.             Pattern pattern = Pattern.compile("[star]", Pattern.CASE_INSENSITIVE);
  31.             Matcher star = pattern.matcher(message);
  32.  
  33.             int count = 0;
  34.             for (int i = 0; i < message.length(); i++) {
  35.                 if (star.find()) {
  36.                     count++;
  37.                 }
  38.             }
  39.             for (int i = 0; i < message.length(); i++) {
  40.                 char symbol = message.charAt(i);
  41.                 decrypted += (char)(symbol - count);
  42.             }
  43.             // .*?@([a-zA-Z]+)[^@\-!>]*:(\d+)[^@\-!>]*!([AD])![^@\-!>]*->(\d+)
  44.             String regex = ".*?(?<planet>[a-zA-Z]+)(?:[^@\\-!:>]*):(?<population>(\\d)+)" +
  45.                     "(?:[^@\\-!:>]*)!(?<attack>[AD])!(?:[^@\\-!:>]*)->(?<soldierCount>\\d+)";
  46.  
  47.             Pattern pattern1 = Pattern.compile(regex);
  48.             Matcher planets = pattern1.matcher(decrypted);
  49.  
  50.             String planet = planets.group("planet");
  51.             //int population = Integer.parseInt(planets.group(population));
  52.             String attack = planets.group("attack");
  53.             //int soldierCount = Integer.parseInt(planets.group(soldierCount));
  54.  
  55.  
  56.             while (planets.find()){
  57.  
  58.                 if ("A".equals(attack)){
  59.  
  60.                     attacked.add(planet);
  61.                     attackedPlanets++;
  62.  
  63.                     System.out.print(String.format("Attacked planets: %d%n", attackedPlanets));
  64.                     Collections.sort(attacked);
  65.                     for (String s : attacked) {
  66.                         System.out.printf("-> %s%n", s);
  67.                     }
  68.                 }else {
  69.  
  70.                     destroyed.add(planet);
  71.                     destroyedPlanets++;
  72.  
  73.                     System.out.printf("Destroyed planets: %d%n", destroyedPlanets);
  74.                     Collections.sort(destroyed);
  75.                     for (String s : destroyed) {
  76.                         System.out.printf("-> %s%n", s);
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement