Advertisement
damesova

Star Enigma [Mimi]

Mar 25th, 2019
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class _12_StarEnigma {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int n = Integer.parseInt(scanner.nextLine());
  9.         String reg =
  10.                 "@(?<name>[A-Za-z]+)(?>[^@:!\\->]*):(?<population>\\d+)(?>[^@:!\\->]*)!(?<attack>[AD])!(?>[^@:!\\->]*)->(?<soldiers>\\d+)";
  11.  
  12.         Pattern p = Pattern.compile(reg);
  13.  
  14.         List<String> infoA = new ArrayList<>();
  15.         List<String> infoD = new ArrayList<>();
  16.  
  17.         String line = "";
  18.         for (int i = 0; i < n; i++) {   //for each line
  19.             line = scanner.nextLine();
  20.             int key = 0;
  21.             StringBuilder updatedLine = new StringBuilder();
  22.             for (int j = 0; j < line.length(); j++) { //along the line length counting
  23.                 if (line.charAt(j) == 's' || line.charAt(j) == 't'
  24.                         || line.charAt(j) == 'a' || line.charAt(j) == 'r'
  25.                         || line.charAt(j) == 'S' || line.charAt(j) == 'T'
  26.                         || line.charAt(j) == 'A' || line.charAt(j) == 'R') {
  27.                     key++;
  28.                 }
  29.             }
  30.  
  31.             //Update characters:
  32.             for (int k = 0; k < line.length(); k++) {
  33.                 char charche = (char) ((int) line.charAt(k) - (key));
  34.                 updatedLine.append(charche);
  35.             }
  36.  
  37.  
  38.             Matcher m = p.matcher(updatedLine);
  39.  
  40.             if (m.find()) {
  41.                 String name = m.group("name");
  42.                 String attack = m.group("attack");
  43.  
  44.                 if (attack.equals("A")) {
  45.                     infoA.add(name);
  46.  
  47.                 } else {
  48.                     infoD.add(name);
  49.                 }
  50.             }
  51.         }
  52.  
  53.  
  54.         System.out.println("Attacked planets: " + infoA.size());
  55.         Collections.sort(infoA);
  56.         for (String s : infoA) {
  57.             System.out.println("-> " + s);
  58.         }
  59.  
  60.         System.out.println("Destroyed planets: " + infoD.size());
  61.         Collections.sort(infoD);
  62.         for (String s : infoD) {
  63.             System.out.println("-> " + s);
  64.         }
  65.  
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement