Advertisement
Rayk

Snowwhite

Feb 28th, 2018
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.HashMap;
  5. import java.util.LinkedHashMap;
  6.  
  7. public class Snowwhite {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         LinkedHashMap<String, Integer> dwarfs = new LinkedHashMap<>();
  12.         HashMap<String, Integer> colors = new HashMap<>();
  13.  
  14.         String line;
  15.         while (!"Once upon a time".equals(line = reader.readLine())) {
  16.             String[] tokens = line.split(" <:> ");
  17.  
  18.             String dwarfName = tokens[0];
  19.             String dwarfHatColor = tokens[1];
  20.             int dwarfPhysics = Integer.parseInt(tokens[2]);
  21.  
  22.             String dwarfId = String.format("%s <:> %s", dwarfName, dwarfHatColor);
  23.  
  24.             if (!dwarfs.containsKey(dwarfId)) {
  25.                 dwarfs.put(dwarfId, dwarfPhysics);
  26.  
  27.                 if (!colors.containsKey(dwarfHatColor)) {
  28.                     colors.put(dwarfHatColor, 1);
  29.                 } else {
  30.                     colors.put(dwarfHatColor, colors.get(dwarfHatColor) + 1);
  31.                 }
  32.             } else {
  33.                 int oldPhysics = dwarfs.get(dwarfId);
  34.  
  35.                 if (dwarfPhysics > oldPhysics) {
  36.                     dwarfs.put(dwarfId, dwarfPhysics);
  37.                 }
  38.             }
  39.         }
  40.  
  41.         dwarfs.entrySet()
  42.                 .stream()
  43.                 .sorted((d1, d2) -> {
  44.                     int compareResult = Integer
  45.                             .compare(d2.getValue(), d1.getValue());
  46.  
  47.                     if (compareResult == 0) {
  48.                         String d1HatColor = d1.getKey().split(" <:> ")[1];
  49.                         String d2HatColor = d2.getKey().split(" <:> ")[1];
  50.  
  51.                         compareResult = Integer
  52.                                 .compare(
  53.                                         colors.get(d2HatColor),
  54.                                         colors.get(d1HatColor)
  55.                                 );
  56.                     }
  57.  
  58.                     return compareResult;
  59.                 })
  60.                 .forEach(d -> {
  61.                     String[] dwarfIdTokens = d.getKey().split(" <:> ");
  62.                     String dwarfName = dwarfIdTokens[0];
  63.                     String dwarfHatColor = dwarfIdTokens[1];
  64.  
  65.                     Integer dwarfPhysics = d.getValue();
  66.  
  67.                     System.out.printf("(%s) %s <-> %d%n", dwarfHatColor, dwarfName, dwarfPhysics);
  68.                 });
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement