Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Collections;
- import java.util.LinkedHashMap;
- import java.util.Map;
- public class CubicAssault {
- public static final int maxMeteors = 1_000_000;
- public static void main(String[] args) {
- Map<String, Region> list = new LinkedHashMap<>();
- try (BufferedReader reader = new BufferedReader(new InputStreamReader(
- System.in));) {
- String input = reader.readLine();
- while (true) {
- if (input.equals("Count em all")) {
- break;
- }
- String[] splitedInput = input.split(" -> ");
- String place = splitedInput[0];
- String meteorType = splitedInput[1];
- int meteors = Integer.parseInt(splitedInput[2]);
- if (!list.containsKey(place)) {
- Region region = new Region(place);
- list.put(place, region);
- }
- switch (meteorType) {
- case "Black":
- list.get(place).blackMeteor += meteors;
- break;
- case "Red":
- list.get(place).redMeteor += meteors;
- break;
- case "Green":
- list.get(place).greenMeteor += meteors;
- break;
- }
- refactorMeteors(list, place, meteors);
- input = reader.readLine();
- }
- list.entrySet()
- .stream()
- .sorted((s1, s2) -> {
- int index = Long.compare(s2.getValue().blackMeteor,
- s1.getValue().blackMeteor);
- if (index == 0) {
- index = Integer.compare(s1.getValue()
- .getRegionName().length(), s2.getValue()
- .getRegionName().length());
- }
- if (index == 0) {
- index = s1.getKey().compareTo(s2.getKey());
- }
- return index;
- })
- .forEach(
- pair -> {
- String name = pair.getKey();
- System.out.println(name);
- Map<String, Long> tempMap = new LinkedHashMap<>();
- for (Region string : list.values()) {
- if (string.getRegionName().equals(name)) {
- tempMap.put("Black", string.blackMeteor);
- tempMap.put("Red", string.redMeteor);
- tempMap.put("Green", string.greenMeteor);
- }
- }
- tempMap.entrySet()
- .stream()
- .sorted((s1, s2) -> {
- int index = Long.compare(
- s2.getValue(), s1.getValue());
- if (index == 0) {
- index = Long.compare(s2.getKey().length(),s1.getKey().length());
- }
- if (index== 0) {
- index = s1.getKey().compareTo(s2.getKey());
- }
- return index;
- })
- .forEach(
- s -> System.out.printf(
- "-> %s : %d\n",
- s.getKey(),
- s.getValue()));
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private static void refactorMeteors(Map<String, Region> list, String place,
- int meteors) {
- while (list.get(place).greenMeteor >= maxMeteors) {
- list.get(place).greenMeteor -= maxMeteors;
- list.get(place).redMeteor += 1;
- }
- while (list.get(place).redMeteor >= maxMeteors) {
- list.get(place).redMeteor -= maxMeteors;
- list.get(place).blackMeteor += 1;
- }
- }
- }
- class Region implements Comparable<Region> {
- public long greenMeteor;
- public long redMeteor;
- public long blackMeteor;
- private String regionName;
- public Region(String regionName) {
- this.greenMeteor = 0;
- this.redMeteor = 0;
- this.blackMeteor = 0;
- this.regionName = regionName;
- }
- public String getRegionName() {
- return regionName;
- }
- public void setRegionName(String regionName) {
- this.regionName = regionName;
- }
- @Override
- public int compareTo(Region other) {
- return this.regionName.compareTo(other.regionName);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement