Guest User

Untitled

a guest
Mar 30th, 2019
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10.  
  11.     public static void main(String[] args) throws IOException {
  12.         Scanner scanner = new Scanner(System.in);
  13.  
  14.         String numberRegex = "[+-]?\\d+\\.?\\d*";
  15.         String operationRegex = "[*/]+";
  16.         String delimiterRegex = ",\\s*";
  17.         String healthRegex = "[^0-9*/+\\-.]";
  18.  
  19.         Map <String,List<Double>> demonsHash = new TreeMap<>();
  20.         String[] demons = scanner.nextLine().split(delimiterRegex);
  21.  
  22.         for (int i = 0; i < demons.length; i++) {
  23.             double health = 0;
  24.             double damage = 0;
  25.  
  26.             List<Double> healthDamage = new ArrayList<>();
  27.             String currentDemon = demons[i].replaceAll(" ","");
  28.             Pattern healthPattern = Pattern.compile(healthRegex);
  29.             Matcher healthFinder = healthPattern.matcher(currentDemon);
  30.  
  31.             while (healthFinder.find()) {
  32.                 health += healthFinder.group().charAt(0);
  33.             }
  34.             Pattern pattern = Pattern.compile(numberRegex);
  35.             Matcher numbers = pattern.matcher(currentDemon);
  36.  
  37.             while(numbers.find()){
  38.                 damage += Double.parseDouble(numbers.group());
  39.             }
  40.             Pattern pattern1 = Pattern.compile(operationRegex);
  41.             Matcher delimiters = pattern1.matcher(currentDemon);
  42.  
  43.             while(delimiters.find()){
  44.                 String signsSequence = delimiters.group();
  45.                 for (int j = 0; j < signsSequence.length(); j++) {
  46.                     if (signsSequence.charAt(j) == '*'){
  47.                         damage *= 2;
  48.                     }
  49.                     else if (signsSequence.charAt(j) == '/') {
  50.                         damage /= 2;
  51.                     }
  52.                 }
  53.             }
  54.             healthDamage.add(health);
  55.             healthDamage.add(damage);
  56.  
  57.             demonsHash.put(currentDemon, healthDamage);
  58.         }
  59.         demonsHash.forEach((key, value) -> {
  60.             System.out.print(key + " - ");
  61.             System.out.printf("%.0f health, ", value.get(0));
  62.             System.out.printf("%.2f damage%n", value.get(1));
  63.  
  64.         });
  65.     }
  66. }
Add Comment
Please, Sign In to add comment