SHARE
TWEET

Untitled

shniaga Mar 26th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package TextAndRegexExercise;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class NetherRealms_13 {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         Map<String, List<Double>> demons = new TreeMap<>();
  12.  
  13.  
  14.  
  15.         String[] input = scanner.nextLine().split("\\s+,\\s+");
  16.  
  17.         for (int i = 0; i < input.length; i++) {
  18.             String healthString = "";
  19.             double health = 0;
  20.             double damage = 0;
  21.             String operations = "";
  22.             Pattern pHealth = Pattern.compile("([^\\.\\/\\-+\\*0-9]*)");
  23.             Matcher pMatch = pHealth.matcher(input[i]);
  24.             demons.put(input[i], new ArrayList<>());
  25.  
  26.             while (pMatch.find()) {
  27.                 healthString += pMatch.group();
  28.             }
  29.             for (int j = 0; j < healthString.length(); j++) {
  30.                 health += healthString.charAt(j);
  31.             }
  32.             demons.get(input[i]).add(health);
  33.  
  34.             Pattern pDamage = Pattern.compile("[-+]?\\d+(\\.(\\d+))?");
  35.             Matcher dMatch = pDamage.matcher(input[i]);
  36.  
  37.             while (dMatch.find()) {
  38.                 double x = Double.parseDouble(dMatch.group());
  39.                 damage += x;
  40.             }
  41.             for (int j = 0; j < input[i].length(); j++) {
  42.                 if (input[i].charAt(j) == '/' || (input[i].charAt(j) == '*')) {
  43.                     operations += (input[i].charAt(j));
  44.                 }
  45.             }
  46.             for (int j = 0; j < operations.length(); j++) {
  47.                 if (operations.charAt(j) == '/') {
  48.                     damage /= 2;
  49.                 } else if (operations.charAt(j) == '*') {
  50.                     damage *= 2;
  51.                 }
  52.             }
  53.             demons.get(input[i]).add(damage);
  54.         }
  55.         demons.forEach((key,value) -> {
  56.             System.out.println(String.format("%s - %.0f health, %.2f damage", key,value.get(0),value.get(1)));
  57.         });
  58.  
  59.     }
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top