Advertisement
desislava_topuzakova

05. Nether Realms

Jul 31st, 2021
1,323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<String> demons = Arrays.stream(scanner.nextLine().split("[, ]+"))
  11.                 .sorted(String::compareTo)
  12.                 .collect(Collectors.toList());
  13.  
  14.         String healthRegex = "([^\\d+\\-*.\\/])";
  15.  
  16.         String damageRegex = "(-?\\d+\\.?\\d*)";
  17.  
  18.         String operationRegex = "([*/])";
  19.  
  20.         Pattern healthPattern = Pattern.compile(healthRegex);
  21.  
  22.         Pattern damagePattern = Pattern.compile(damageRegex);
  23.  
  24.         Pattern operationPattern = Pattern.compile(operationRegex);
  25.  
  26.         for (String demon : demons) {
  27.             Matcher matcher = healthPattern.matcher(demon);
  28.  
  29.             int health = 0;
  30.  
  31.             while (matcher.find()) {
  32.                 health += matcher.group(1).charAt(0);
  33.             }
  34.  
  35.             matcher = damagePattern.matcher(demon);
  36.  
  37.             double damage = 0;
  38.  
  39.             while (matcher.find()) {
  40.                 damage += Double.parseDouble(matcher.group(1));
  41.             }
  42.  
  43.             matcher = operationPattern.matcher(demon);
  44.  
  45.             while (matcher.find()) {
  46.                 if(matcher.group(1).equals("*")) {
  47.                     damage *= 2;
  48.                 } else {
  49.                     damage /= 2;
  50.                 }
  51.             }
  52.  
  53.             System.out.printf("%s - %d health, %.2f damage%n",
  54.                     demon, health, damage);
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement