Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package exercises;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class StarEnigma {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- //STCDoghudd4=63333$D$0A53333
- //EHfsytsnhf?8555&I&2C9555SR
- //Pattern mypattern = Pattern.compile(MYREGEX, Pattern.CASE_INSENSITIVE);
- int n = Integer.parseInt(scanner.nextLine());
- int attackedPlanets = 0;
- int destroyedPlanets = 0;
- List<String> attacked = new ArrayList<>();
- List<String> destroyed = new ArrayList<>();
- while (n-- > 0) {
- String message = scanner.nextLine();
- String decrypted = "";
- Pattern pattern = Pattern.compile("[star]", Pattern.CASE_INSENSITIVE);
- Matcher star = pattern.matcher(message);
- int count = 0;
- for (int i = 0; i < message.length(); i++) {
- if (star.find()) {
- count++;
- }
- }
- for (int i = 0; i < message.length(); i++) {
- char symbol = message.charAt(i);
- decrypted += (char)(symbol - count);
- }
- // .*?@([a-zA-Z]+)[^@\-!>]*:(\d+)[^@\-!>]*!([AD])![^@\-!>]*->(\d+)
- String regex = ".*?(?<planet>[a-zA-Z]+)(?:[^@\\-!:>]*):(?<population>(\\d)+)" +
- "(?:[^@\\-!:>]*)!(?<attack>[AD])!(?:[^@\\-!:>]*)->(?<soldierCount>\\d+)";
- Pattern pattern1 = Pattern.compile(regex);
- Matcher planets = pattern1.matcher(decrypted);
- String planet = planets.group("planet");
- //int population = Integer.parseInt(planets.group(population));
- String attack = planets.group("attack");
- //int soldierCount = Integer.parseInt(planets.group(soldierCount));
- while (planets.find()){
- if ("A".equals(attack)){
- attacked.add(planet);
- attackedPlanets++;
- System.out.print(String.format("Attacked planets: %d%n", attackedPlanets));
- Collections.sort(attacked);
- for (String s : attacked) {
- System.out.printf("-> %s%n", s);
- }
- }else {
- destroyed.add(planet);
- destroyedPlanets++;
- System.out.printf("Destroyed planets: %d%n", destroyedPlanets);
- Collections.sort(destroyed);
- for (String s : destroyed) {
- System.out.printf("-> %s%n", s);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement