Advertisement
Rayk

Snowwhite

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