Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. package AssociativeArrays;
  2.  
  3.  
  4. import com.sun.source.tree.Tree;
  5.  
  6. import java.util.*;
  7. import java.util.stream.Collector;
  8. import java.util.stream.Collectors;
  9.  
  10. public class MOBAChallenger {
  11. public static String commonPosition(Map<String, TreeMap<String, Integer>> userPositions, String player1, String player2) {
  12. TreeMap<String, Integer> stringIntegerTreeMap = userPositions.get(player1);
  13. for (String s : stringIntegerTreeMap.keySet()) {
  14. if (userPositions.get(player2).containsKey(s)) {
  15. return "Common";
  16. }
  17. }
  18. return "Notcommon";
  19. }
  20.  
  21. public static String Position(Map<String, TreeMap<String, Integer>> userPositions, String player1, String player2) {
  22. TreeMap<String, Integer> stringIntegerTreeMap = userPositions.get(player1);
  23. for (String s : stringIntegerTreeMap.keySet()) {
  24. if (userPositions.get(player2).containsKey(s)) {
  25. return s;
  26. }
  27. }
  28. return "";
  29. }
  30.  
  31.  
  32. public static void main(String[] args) {
  33. Scanner scanner = new Scanner(System.in);
  34. String input = scanner.nextLine();
  35. Map<String, TreeMap<String, Integer>> userPositions = new LinkedHashMap<>();
  36. while (!"Season end".equals(input)) {
  37. if (input.contains("->")) {
  38. String[] tokens = input.split(" -> ");
  39. String player = tokens[0];
  40. String position = tokens[1];
  41. int points = Integer.parseInt(tokens[2]);
  42. if (!userPositions.containsKey(player)) {
  43. userPositions.put(player, new TreeMap<>());
  44. userPositions.get(player).put(position, points);
  45. } else if (!userPositions.get(player).containsKey(position)) {
  46. userPositions.get(player).put(position, points);
  47. } else {
  48. if (userPositions.get(player).get(position) < points) {
  49. userPositions.get(player).put(position, points);
  50. }
  51. }
  52. } else {
  53. String[] battles = input.split(" vs ");
  54. String player1 = battles[0];
  55. String player2 = battles[1];
  56. if (userPositions.containsKey(player1) && userPositions.containsKey(player2)) {
  57. if (commonPosition(userPositions, player1, player2).equals("Common")) {
  58. String s = Position(userPositions, player1, player2);
  59. if (userPositions.get(player1).get(s) > userPositions.get(player2).get(s)) {
  60. userPositions.remove(player2);
  61. } else if (userPositions.get(player1).get(s) < userPositions.get(player2).get(s)) {
  62. userPositions.remove(player1);
  63. }
  64. }
  65. }
  66. }
  67. input = scanner.nextLine();
  68. }
  69. // Map<String,Integer> sorted=new LinkedHashMap<>();
  70. // for (Map.Entry<String, TreeMap<String, Integer>> stringTreeMapEntry : userPositions.entrySet()) {
  71. // Map<String, Integer> finalSorted = sorted;
  72. // stringTreeMapEntry.getValue().forEach((e1, e2)->{
  73. // int sum=stringTreeMapEntry.getValue().values().stream().mapToInt(x->x).sum();
  74. // finalSorted.putIfAbsent(stringTreeMapEntry.getKey(),sum);
  75. // });
  76. // }
  77. // sorted= sorted.entrySet().stream().sorted((a,b)->b.getValue().compareTo(a.getValue()))
  78. // .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue, (e1,e2)->e1,LinkedHashMap::new));
  79. // sorted.forEach((key,value)-> {
  80. // System.out.printf("%s: %d skill%n",key,value);
  81. // userPositions.get(key).entrySet().stream().sorted((a,b)->{
  82. // int res= b.getValue().compareTo(a.getValue());
  83. // if(res==0)
  84. // res = a.getKey().compareTo(b.getKey());
  85. //
  86. // return res ; })
  87. // .forEach(e-> System.out.printf("- %s <::> %d%n",e.getKey(),e.getValue()));
  88. // });
  89. userPositions.entrySet().stream()
  90. .sorted((e1, e2) -> {
  91. int sum1 = e1.getValue().values().stream().mapToInt(Integer::intValue).sum();
  92. int sum2 = e2.getValue().values().stream().mapToInt(Integer::intValue).sum();
  93.  
  94. if (sum1 == sum2) {
  95. return e1.getKey().compareTo(e2.getKey());
  96. } else {
  97. return sum2 - sum1;
  98. }
  99. })
  100. .forEach((entry -> {
  101. int sum = entry.getValue().values().stream().mapToInt(Integer::intValue).sum();
  102. System.out.printf("%s: %d skill%n", entry.getKey(), sum);
  103. entry.getValue().entrySet().stream()
  104. .sorted((e1, e2) -> {
  105. if (e1.getValue().equals(e2.getValue())) {
  106. return e1.getKey().compareTo(e2.getKey());
  107. } else {
  108. return e2.getValue().compareTo(e1.getValue());
  109. }
  110. })
  111. .forEach(e -> System.out.printf("- %s <::> %d%n", e.getKey(), e.getValue()));
  112. }));
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement