Advertisement
iWantAMcLaren

Star Enigma

Mar 31st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import java.util.;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. int n = Integer.parseInt(scanner.nextLine());
  12.  
  13. MapString, ListString result = new LinkedHashMap();
  14. result.put(Attacked planets, new ArrayList());
  15. result.put(Destroyed planets, new ArrayList());
  16.  
  17. Pattern pattern = Pattern.compile(([^@-!]) +
  18. (planet@[A-Za-z]+)([^@-!]) +
  19. (populationd+)([^@-!]) +
  20. (typeOfAttack![AD]!)([^@-!]) +
  21. (soldierCount-d+)([^@-!]));
  22.  
  23. for (int i = 0; i n; i++) {
  24. String input = scanner.nextLine();
  25. int key = 0;
  26. key = getKey(input, key);
  27. input = decryptMsg(input, key);
  28.  
  29. Matcher matcher = pattern.matcher(input);
  30.  
  31. if (matcher.find()) {
  32.  
  33. String planet = matcher.group(planet).substring(1);
  34. int population = Integer.parseInt(matcher.group(population).substring(1));
  35. String typeOfAttack = matcher.group(typeOfAttack).substring(1, matcher.group(typeOfAttack).length() - 1);
  36. int soldierCount = Integer.parseInt(matcher.group(soldierCount).substring(2));
  37.  
  38. switch (typeOfAttack) {
  39. case A
  40. String mapKey = Attacked planets;
  41. result.get(mapKey).add(planet);
  42. break;
  43. case D
  44. mapKey = Destroyed planets;
  45. result.get(mapKey).add(planet);
  46. break;
  47. }
  48. }
  49. }
  50. for (Map.EntryString,ListString planets result.entrySet()){
  51. System.out.println(String.format(%s %d,planets.getKey(),planets.getValue().size()));
  52.  
  53. planets.getValue().sort((e1,e2)-e1.compareTo(e2));
  54.  
  55. for (int i = 0; i planets.getValue().size(); i++) {
  56. System.out.println(String.format(- %s,planets.getValue().get(i).toString().replaceAll([],)));
  57. }
  58.  
  59. }
  60. }
  61.  
  62. private static int getKey(String input, int key) {
  63. for (int i = 0; i input.length(); i++) {
  64. if (input.toLowerCase().charAt(i) == 's'
  65. input.toLowerCase().charAt(i) == 't'
  66. input.toLowerCase().charAt(i) == 'a'
  67. input.toLowerCase().charAt(i) == 'r') {
  68. key++;
  69. }
  70. }
  71. return key;
  72. }
  73.  
  74. private static String decryptMsg(String input, int key) {
  75. StringBuilder result = new StringBuilder();
  76. for (int i = 0; i input.length(); i++) {
  77. result.append((char) (input.charAt(i) - key));
  78. }
  79. return result.toString();
  80. }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement