desislava_topuzakova

04. Star Enigma

Nov 20th, 2022
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class StarEnigma_04 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. int countMessages = Integer.parseInt(scanner.nextLine());
  9. String regex = "@(?<planetName>[A-Za-z]+)[^@!:>-]*:(?<population>[0-9]+)[^@!:>-]*!(?<attackType>[AD])![^@!:>-]*->(?<soldiersCount>[0-9]+)";
  10. Pattern pattern = Pattern.compile(regex);
  11.  
  12. List<String> attackersPlanets = new ArrayList<>(); //атакуващи планети (attackType е "А")
  13. List<String> destroyedPlanets = new ArrayList<>(); //унищожени планети (attackType е "D")
  14.  
  15. for (int messageCount = 1; messageCount <= countMessages; messageCount++) {
  16. String encryptedMessage = scanner.nextLine(); //криптираното съобщение
  17. String decryptedMessage = getDecryptedMessage(encryptedMessage); //декриптирано съобщение
  18. //декриптирано: "PQ@Alderaa1:30000!A!->20000"
  19. Matcher matcher = pattern.matcher(decryptedMessage);
  20. //matcher: "@(?<planetName>Alderaa)[^@!:>-]*:(?<population>30000)[^@!:>-]*!(?<attackType>A)![^@!:>-]*->(?<soldiersCount>20000)";
  21. if (matcher.find()) {
  22. String planetName = matcher.group("planetName");
  23. //int population = Integer.parseInt(matcher.group("population"));
  24. String attackType = matcher.group("attackType");
  25. //int soldiersCount = Integer.parseInt(matcher.group("soldiersCount"));
  26.  
  27. if (attackType.equals("A")) {
  28. //атакуваща планета
  29. attackersPlanets.add(planetName);
  30. } else if (attackType.equals("D")) {
  31. //унищожена планета
  32. destroyedPlanets.add(planetName);
  33. }
  34. }
  35. }
  36.  
  37. System.out.println("Attacked planets: " + attackersPlanets.size());
  38. Collections.sort(attackersPlanets); //сортирам планетите по име
  39. attackersPlanets.forEach(planet -> System.out.println("-> " + planet));
  40.  
  41. System.out.println("Destroyed planets: " + destroyedPlanets.size());
  42. Collections.sort(destroyedPlanets); //сортирам планетите по име
  43. destroyedPlanets.forEach(planet -> System.out.println("-> " + planet));
  44. }
  45.  
  46. //върне декриптираното съобщение
  47. private static String getDecryptedMessage(String encryptedMessage) {
  48. //1. брой на символите [s, t, a, r, S, T, A, R]
  49. //криптирано съобщение: STCDoghudd4=63333$D$0A53333 -> 3 специални букви
  50. int countLetters = getSpecialLettersCount(encryptedMessage);
  51.  
  52. //декриптиране -> контруираме ново съобщение
  53. StringBuilder decryptedMessage = new StringBuilder();
  54. //1. всеки символ от критпираното съобщение
  55. //2. нов символ -> ascii на нов символ = ascii на текущия символ - countLetters
  56. //3. добавяме нов символ
  57. for (char symbol : encryptedMessage.toCharArray()) {
  58. char newSymbol = (char)(symbol - countLetters);
  59. decryptedMessage.append(newSymbol);
  60. }
  61.  
  62. return decryptedMessage.toString();
  63. }
  64.  
  65. //върне общия брой на буквите: [s, t, a, r, S, T, A, R]
  66. private static int getSpecialLettersCount(String encryptedMessage) {
  67. //криптирано съобщение: STCDoghudd4=63333$D$0A53333 -> 3 специални букви
  68. int count = 0;
  69. for (char symbol : encryptedMessage.toCharArray()) {
  70. switch (symbol) {
  71. case 's':
  72. case 't':
  73. case 'a':
  74. case 'r':
  75. case 'S':
  76. case 'T':
  77. case 'A':
  78. case 'R':
  79. count++;
  80. break;
  81. }
  82. }
  83.  
  84. return count;
  85. }
  86. }
  87.  
Add Comment
Please, Sign In to add comment