Advertisement
Lyuben_Andreev

moreThanSevenDwarves

May 31st, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4.  
  5. public class Main {
  6.  
  7.     static class Dwarf {
  8.         private String name;
  9.         private String color;
  10.         private int physics;
  11.  
  12.         public Dwarf(String name, String color, int physics) {
  13.             this.name = name;
  14.             this.color = color;
  15.             this.physics = physics;
  16.         }
  17.  
  18.         public void setPhysics(int physics) {
  19.             this.physics = Math.max(physics, this.getPhysics());
  20.         }
  21.  
  22.         public String getName() {
  23.             return name;
  24.         }
  25.  
  26.         public String getColor() {
  27.             return color;
  28.         }
  29.  
  30.         public int getPhysics() {
  31.             return physics;
  32.         }
  33.     }
  34.  
  35.     public static void main(String[] args) {
  36.         Scanner sc = new Scanner(System.in);
  37.  
  38.         List<Dwarf> dwarves = new ArrayList<>();
  39.         Map<String, Integer> countsByColor = new HashMap<>();
  40.  
  41.         String line = sc.nextLine();
  42.  
  43.         while (!line.equals("Once upon a time")) {
  44.             String[] tokens = line.split(" <:> ");
  45.             String name = tokens[0];
  46.             String color = tokens[1];
  47.             int physics = Integer.parseInt(tokens[2]);
  48.  
  49.             Optional<Dwarf> dwarfCandidate = dwarves.stream().filter(dwarf -> dwarf.getName().equals(name) && dwarf.getColor().equals(color))
  50.                     .findFirst();
  51.  
  52.             if (dwarfCandidate.isPresent()) {
  53.                 Dwarf dwarf = dwarfCandidate.get();
  54.                 dwarf.setPhysics(physics);
  55.             } else {
  56.                 Dwarf dwarf = new Dwarf(name, color, physics);
  57.                 countsByColor.putIfAbsent(color, 0);
  58.                 countsByColor.put(color, countsByColor.get(color) + 1);
  59.                 dwarves.add(dwarf);
  60.             }
  61.  
  62.             line = sc.nextLine();
  63.         }
  64.  
  65.         dwarves.sort((dwarf1, dwarf2) -> {
  66.             int physics2 = dwarf2.getPhysics();
  67.             int physics1 = dwarf1.getPhysics();
  68.             if (physics1 == physics2) {
  69.                 return countsByColor.get(dwarf2.getColor()).compareTo(
  70.                         countsByColor.get(dwarf1.getColor())
  71.                 );
  72.             }
  73.             return Integer.compare(physics2, physics1);
  74.         });
  75.  
  76.         dwarves.forEach(dwarf -> System.out.printf("(%s) %s <-> %d%n", dwarf.getColor(), dwarf.getName(), dwarf.getPhysics()));
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement