Advertisement
mellowdeep

cubic assault

Jun 21st, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.Collections;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6.  
  7. public class CubicAssault {
  8.  
  9.     public static final int maxMeteors = 1_000_000;
  10.  
  11.     public static void main(String[] args) {
  12.  
  13.         Map<String, Region> list = new LinkedHashMap<>();
  14.         try (BufferedReader reader = new BufferedReader(new InputStreamReader(
  15.                 System.in));) {
  16.             String input = reader.readLine();
  17.             while (true) {
  18.                 if (input.equals("Count em all")) {
  19.                     break;
  20.                 }
  21.                 String[] splitedInput = input.split(" -> ");
  22.                 String place = splitedInput[0];
  23.                 String meteorType = splitedInput[1];
  24.                 int meteors = Integer.parseInt(splitedInput[2]);
  25.                 if (!list.containsKey(place)) {
  26.                     Region region = new Region(place);
  27.                     list.put(place, region);
  28.                 }
  29.                 switch (meteorType) {
  30.                 case "Black":
  31.                     list.get(place).blackMeteor += meteors;
  32.                     break;
  33.                 case "Red":
  34.                     list.get(place).redMeteor += meteors;
  35.                     break;
  36.                 case "Green":
  37.                     list.get(place).greenMeteor += meteors;
  38.                     break;
  39.                 }
  40.  
  41.                 refactorMeteors(list, place, meteors);
  42.                 input = reader.readLine();
  43.             }
  44.  
  45.             list.entrySet()
  46.                     .stream()
  47.                     .sorted((s1, s2) -> {
  48.                         int index = Long.compare(s2.getValue().blackMeteor,
  49.                                 s1.getValue().blackMeteor);
  50.                         if (index == 0) {
  51.                             index = Integer.compare(s1.getValue()
  52.                                     .getRegionName().length(), s2.getValue()
  53.                                     .getRegionName().length());
  54.                         }
  55.                         if (index == 0) {
  56.                             index = s1.getKey().compareTo(s2.getKey());
  57.                         }
  58.                         return index;
  59.                     })
  60.                     .forEach(
  61.                             pair -> {
  62.                                 String name = pair.getKey();
  63.                                 System.out.println(name);
  64.                                 Map<String, Long> tempMap = new LinkedHashMap<>();
  65.                                 for (Region string : list.values()) {
  66.                                     if (string.getRegionName().equals(name)) {
  67.                                         tempMap.put("Black", string.blackMeteor);
  68.                                         tempMap.put("Red", string.redMeteor);
  69.                                         tempMap.put("Green", string.greenMeteor);
  70.  
  71.                                     }
  72.                                 }
  73.                                 tempMap.entrySet()
  74.                                         .stream()
  75.                                         .sorted((s1, s2) -> {
  76.                                             int index = Long.compare(
  77.                                                 s2.getValue(), s1.getValue());
  78.                                             if (index == 0) {
  79.                                                 index = Long.compare(s2.getKey().length(),s1.getKey().length());
  80.                                             }
  81.                                             if (index== 0) {
  82.                                                 index = s1.getKey().compareTo(s2.getKey());
  83.                                             }
  84.                                             return index;
  85.                                         })
  86.                                         .forEach(
  87.                                                 s -> System.out.printf(
  88.                                                         "-> %s : %d\n",
  89.                                                         s.getKey(),
  90.                                                         s.getValue()));
  91.                             });
  92.  
  93.         } catch (Exception e) {
  94.             e.printStackTrace();
  95.         }
  96.     }
  97.  
  98.     private static void refactorMeteors(Map<String, Region> list, String place,
  99.             int meteors) {
  100.         while (list.get(place).greenMeteor >= maxMeteors) {
  101.             list.get(place).greenMeteor -= maxMeteors;
  102.             list.get(place).redMeteor += 1;
  103.         }
  104.  
  105.         while (list.get(place).redMeteor >= maxMeteors) {
  106.             list.get(place).redMeteor -= maxMeteors;
  107.             list.get(place).blackMeteor += 1;
  108.         }
  109.  
  110.     }
  111.  
  112. }
  113.  
  114. class Region implements Comparable<Region> {
  115.  
  116.     public long greenMeteor;
  117.     public long redMeteor;
  118.     public long blackMeteor;
  119.     private String regionName;
  120.  
  121.     public Region(String regionName) {
  122.         this.greenMeteor = 0;
  123.         this.redMeteor = 0;
  124.         this.blackMeteor = 0;
  125.         this.regionName = regionName;
  126.     }
  127.  
  128.     public String getRegionName() {
  129.         return regionName;
  130.     }
  131.  
  132.     public void setRegionName(String regionName) {
  133.         this.regionName = regionName;
  134.     }
  135.  
  136.     @Override
  137.     public int compareTo(Region other) {
  138.  
  139.         return this.regionName.compareTo(other.regionName);
  140.     }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement