SHOW:
|
|
- or go back to the newest paste.
| 1 | import java.util.ArrayList; | |
| 2 | import java.util.Collections; | |
| 3 | import java.util.List; | |
| 4 | import java.util.Scanner; | |
| 5 | import java.util.regex.Matcher; | |
| 6 | import java.util.regex.Pattern; | |
| 7 | import java.util.stream.Collectors; | |
| 8 | ||
| 9 | public class StarEnigma {
| |
| 10 | public static void main(String[] args) {
| |
| 11 | Scanner scanner = new Scanner(System.in); | |
| 12 | int count = Integer.parseInt(scanner.nextLine()); | |
| 13 | List<String> attacked = new ArrayList<>(); | |
| 14 | List<String> destroyed = new ArrayList<>(); | |
| 15 | //всяка една буква от съобщението - 3 от аски код | |
| 16 | for (int i = 0; i < count; i++) {
| |
| 17 | String encryptedMessage = scanner.nextLine(); | |
| 18 | int countSpecialLetter = getSpecialLetters(encryptedMessage); | |
| 19 | String decryptedMessage = getDescryptedMessage(encryptedMessage, countSpecialLetter); | |
| 20 | ||
| 21 | //PQ@Alderaa1:30000!A!->20000 | |
| 22 | String regex = "@(?<planet>[A-Za-z]+)[^@!:>-]*:(?<population>[0-9]+)[^@!:>-]*!(?<attack>[AD])![^@!:>-]*->(?<soldiersCount>[0-9]+)"; | |
| 23 | Pattern pattern = Pattern.compile(regex); | |
| 24 | Matcher matcher = pattern.matcher(decryptedMessage); | |
| 25 | if(matcher.find()) {
| |
| 26 | String planet = matcher.group("planet");
| |
| 27 | String typeAttack = matcher.group("attack");
| |
| 28 | if(typeAttack.equals("A")){
| |
| 29 | attacked.add(planet); | |
| 30 | } else if (typeAttack.equals("D")) {
| |
| 31 | destroyed.add(planet); | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | } | |
| 36 | ||
| 37 | printOutput(attacked, destroyed); | |
| 38 | } | |
| 39 | ||
| 40 | private static void printOutput(List<String> attacked, List<String> destroyed) {
| |
| 41 | Collections.sort(attacked); | |
| 42 | Collections.sort(destroyed); | |
| 43 | ||
| 44 | System.out.println("Attacked planets: " + attacked.size());
| |
| 45 | if(!attacked.isEmpty()) {
| |
| 46 | attacked.forEach(p -> System.out.println("-> " + p));
| |
| 47 | } | |
| 48 | System.out.println("Destroyed planets: " + destroyed.size());
| |
| 49 | if(!destroyed.isEmpty()) {
| |
| 50 | destroyed.forEach(p -> System.out.println("-> " + p));
| |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | private static String getDescryptedMessage(String encryptedMessage, int reducedValue) {
| |
| 55 | StringBuilder decryptedMessage = new StringBuilder(); | |
| 56 | for (int index = 0; index < encryptedMessage.length(); index++) {
| |
| 57 | char currentSymbol = encryptedMessage.charAt(index); | |
| 58 | decryptedMessage.append((char)(currentSymbol - reducedValue)); | |
| 59 | ||
| 60 | } | |
| 61 | ||
| 62 | return decryptedMessage.toString(); | |
| 63 | } | |
| 64 | ||
| 65 | private static int getSpecialLetters(String encryptedMessage) {
| |
| 66 | //SsTtAaRr -> броят на тези букви | |
| 67 | int count = 0; | |
| 68 | String regexSpecialLetter = "[SsTtAaRr]"; | |
| 69 | Pattern pattern = Pattern.compile(regexSpecialLetter); | |
| 70 | Matcher matcher = pattern.matcher(encryptedMessage); | |
| 71 | ||
| 72 | while(matcher.find()){
| |
| 73 | count++; | |
| 74 | } | |
| 75 | ||
| 76 | return count; | |
| 77 | ||
| 78 | } | |
| 79 | } |