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