BanishMemory

Сръбско Unleashed

Jul 15th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. /*
  2. * Сръбско Unleashed
  3. */
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.util.Arrays;
  9. import java.util.HashMap;
  10. import java.util.LinkedList;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.regex.Matcher;
  14. import java.util.regex.Pattern;
  15. import static java.util.Comparator.comparing;
  16.  
  17. public class SrpskoUnleashed {
  18.     private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  19.     private final static Pattern validInputPattern = Pattern
  20.             .compile("(?:(?:(?:\\w+\\s\\w+)|(?:\\w+))\\s\\@(?:(?:\\w+\\s\\w+)|(?:\\w+))\\s\\d+\\s\\d+)");
  21.     private final static Pattern singerNamePattern = Pattern
  22.             .compile("(?:(?:\\w+\\s\\w+)|(?:\\w+))(?=\\s\\@)");
  23.     private final static Pattern locationMPattern = Pattern
  24.             .compile("(?<=\\@)(?:((?:[a-zA-Z])+\\s)){1,2}");
  25.     private final static Pattern moneyPattern = Pattern
  26.             .compile("\\d+\\s\\d+");
  27.     private static List<String> validInput;
  28.     private static Map<String, Map<String, Singer>> singersPerLocation;
  29.  
  30.     public static void main(String[] args) {
  31.         singersPerLocation = new HashMap<>();
  32.         List<String> inputList = new LinkedList<>();
  33.        
  34.         String[] input = null;
  35.  
  36.         while(true) {
  37.             String line = null;
  38.            
  39.             try {
  40.                 line = reader.readLine().trim();
  41.             } catch (IOException e) {
  42.                 e.printStackTrace();
  43.             }
  44.            
  45.             if(line.equals("End"))
  46.                 break;
  47.            
  48.             inputList.add(line);
  49.         }
  50.        
  51.         input = inputList.toArray(new String[0]);
  52.  
  53.         validInput = new LinkedList<>();
  54.         for (String iString : input) {
  55.             validateInputAndCollectValidData(iString);
  56.         }
  57.         int counter = 0;
  58.  
  59.         for (String inputItem : validInput) {
  60.             Map<String, Singer> singersOfCurrentLocation;
  61.             String singerName = extractSingerName(inputItem);
  62.             int singerMoney = extractMoney(inputItem);
  63.             String location = extractLocation(inputItem);
  64.             Singer singer = null;
  65.  
  66.             if(singersPerLocation.containsKey(location)) {
  67.                 singersOfCurrentLocation = singersPerLocation.get(location);
  68.             } else {
  69.                 singersOfCurrentLocation = new HashMap<>();
  70.             }
  71.            
  72.            
  73.            
  74.             if (singersOfCurrentLocation.containsKey(singerName)) {
  75.                 singer = singersOfCurrentLocation.get(singerName);
  76.  
  77.             } else {
  78.                 singer = new Singer(singerName, counter);
  79.                 counter++;
  80.             }
  81.  
  82.             singer.addMoney(singerMoney);
  83.            
  84.             singersOfCurrentLocation.put(singerName, singer);
  85.             singersPerLocation.put(location, singersOfCurrentLocation);
  86.         }
  87.  
  88.  
  89.         singersPerLocation.entrySet().stream().map(x -> {
  90.             return x.getKey();
  91.         })
  92.         .sorted(comparing(String::toString).reversed())
  93.         .forEach(loc -> {
  94.             System.out.println(loc);
  95.             singersPerLocation.get(loc)
  96.             .entrySet()
  97.             .stream()
  98.             .map(singerEntry -> {
  99.                 return singerEntry.getValue();
  100.             })
  101.             .sorted()
  102.             .map(singer -> {
  103.                 return String.format("#  %s -> %d", singer.getName(), singer.getTotalMoney());
  104.             })
  105.             .forEach(System.out::println);
  106.         });
  107.     }
  108.    
  109.     private static String extractSingerName(String input) {
  110.         String name = null;
  111.         Matcher m = singerNamePattern.matcher(input);
  112.  
  113.         while (m.find() && name == null) {
  114.             name = m.group();
  115.         }
  116.  
  117.         return name;
  118.     }
  119.  
  120.     private static int extractMoney(String input) {
  121.         String money = null;
  122.         Matcher m = moneyPattern.matcher(input);
  123.  
  124.         while (m.find() && money == null)
  125.             money = m.group();
  126.  
  127.         Integer[] moneyAsNumbers =
  128.                 Arrays.asList(money.split("\\s+"))
  129.                 .stream()
  130.                 .map(x -> {
  131.                     return Integer.valueOf(x);
  132.                 })
  133.                 .toArray(Integer[]::new);
  134.  
  135.         int output = 1;
  136.         for (Integer i : moneyAsNumbers)
  137.             output *= i;
  138.  
  139.         return output;
  140.     }
  141.  
  142.     private static String extractLocation(String input) {
  143.         String location = null;
  144.         Matcher m = locationMPattern.matcher(input);
  145.  
  146.         while (m.find() && location == null)
  147.             location = m.group().trim();
  148.  
  149.         return location;
  150.     }
  151.  
  152.     private static void validateInputAndCollectValidData(String input) {
  153.         String validData = null;
  154.         Matcher m = validInputPattern.matcher(input);
  155.  
  156.         while (m.find()) {
  157.             validData = m.group();
  158.         }
  159.  
  160.         if (validData != null)
  161.             validInput.add(validData);
  162.     }
  163.  
  164. }
  165.  
  166. class Singer implements Comparable<Singer> {
  167.     private String name;
  168.     private int money;
  169.     private int inputOrder;
  170.  
  171.     public Singer(String name, int counter) {
  172.         this.name = name;
  173.         this.money = 0;
  174.         this.inputOrder = counter;
  175.     }
  176.  
  177.     public String getName() {
  178.         return this.name;
  179.     }
  180.  
  181.     public int getTotalMoney() {
  182.         return this.money;
  183.     }
  184.  
  185.     public void addMoney(int money) {
  186.         this.money += money;
  187.     }
  188.  
  189.     public int getInputOrder() {
  190.         return this.inputOrder;
  191.     }
  192.  
  193.     @Override
  194.     public int compareTo(Singer other) {
  195.         int comparisson = -(this.getTotalMoney() - other.getTotalMoney());
  196.         if (comparisson == 0) {
  197.             return this.getInputOrder() - other.getInputOrder();
  198.         } else
  199.             return comparisson;
  200.     }
  201.  
  202.     @Override
  203.     public String toString() {
  204.         return this.getName() + ": " + this.getTotalMoney();
  205.     }
  206.    
  207.    
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment