Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.nio.file.OpenOption;
  5. import java.util.*;
  6.  
  7. public class test {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         LinkedHashMap<String, List<OpponentInfo>> gameData = new LinkedHashMap<>();
  12.  
  13.         while (true) {
  14.             String line = bf.readLine();
  15.             if ("osu!".equals(line)) {
  16.                 break;
  17.             }
  18.             String[] tokens = line.split("[\\s<\\->]+");
  19.  
  20.             // Change to object Long?!
  21.             Long playerOneScore = Long.valueOf(tokens[0]);
  22.             String playerOneName = tokens[1];
  23.             // Deleted all variables for forming a object Opponent info
  24.             String playerTwoName = tokens[2];
  25.             Long playerTwoScore = Long.valueOf(tokens[3]);
  26.  
  27.             // fill the map without finding the winner and looser
  28.             if(!gameData.containsKey(playerOneName)){
  29.                 gameData.put(playerOneName, new ArrayList<>());
  30.             }
  31.             gameData.get(playerOneName)
  32.                     .add(new OpponentInfo(playerTwoName, playerOneScore - playerTwoScore));
  33.             if(!gameData.containsKey(playerTwoName)){
  34.                 gameData.put(playerTwoName, new ArrayList<>());
  35.             }
  36.             gameData.get(playerTwoName)
  37.                     .add(new OpponentInfo(playerOneName, playerTwoScore - playerOneScore));
  38.         }
  39.  
  40.         gameData.entrySet().stream()
  41.                 .sorted((a, b) -> sortByTotalScore(a.getValue(), b.getValue()))
  42.                 .forEach(p -> printAll(p));
  43.  
  44.     }
  45.  
  46.     private static void printAll(Map.Entry<String, List<OpponentInfo>> p) {
  47.         System.out.printf("%s - (%d)\n", p.getKey(), p.getValue().stream().mapToLong(OpponentInfo::getMatchScore).sum());
  48.         for (OpponentInfo op : p.getValue()) {
  49.             System.out.printf("*   %s <-> %d\n", op.getOpponentName(), op.getMatchScore());
  50.         }
  51.     }
  52.  
  53.     private static int sortByTotalScore(List<OpponentInfo> a, List<OpponentInfo> b) {
  54.         long playerOneScore = a.stream().mapToLong(OpponentInfo::getMatchScore).sum();
  55.         long playerTwoScore = b.stream().mapToLong(OpponentInfo::getMatchScore).sum();
  56.  
  57.         return Long.compare(playerTwoScore, playerOneScore);
  58.     }
  59.  
  60.     public static class OpponentInfo {
  61.         public String opponentName;
  62.         public long matchScore;
  63.  
  64.         public OpponentInfo() {
  65.         }
  66.  
  67.         public OpponentInfo(String opponentName, long matchScore) {
  68.             this.opponentName = opponentName;
  69.             this.matchScore = matchScore;
  70.         }
  71.  
  72.         public String getOpponentName() {
  73.             return opponentName;
  74.         }
  75.  
  76.         public void setOpponentName(String opponentName) {
  77.             this.opponentName = opponentName;
  78.         }
  79.  
  80.         public long getMatchScore() {
  81.             return matchScore;
  82.         }
  83.  
  84.         public void setMatchScore(long matchScore) {
  85.             this.matchScore = matchScore;
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement