Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- public class NetherRealms {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- List<String> demons = Arrays.stream(sc.nextLine().split("[, ]+"))
- .sorted(String::compareTo)
- .collect(Collectors.toList());
- String healthRegex = "([^0-9+\\-\\*\\/.])";
- Pattern healthPattern = Pattern.compile(healthRegex);
- String damageRegex = "(-?\\d+\\.?\\d*)";
- Pattern damagePattern = Pattern.compile(damageRegex);
- String operationsRegex = "(\\*|\\/)";
- Pattern operationsPattern = Pattern.compile(operationsRegex);
- for (String demon : demons) {
- Matcher matcher = healthPattern.matcher(demon);
- int health = 0;
- while (matcher.find()) {
- health += matcher.group(1).charAt(0);
- }
- matcher = damagePattern.matcher(demon);
- double damage = 0.0;
- while (matcher.find()) {
- damage += Double.parseDouble(matcher.group(1));
- }
- matcher = operationsPattern.matcher(demon);
- while (matcher.find()) {
- if (matcher.group(1).equals("*")) {
- damage *= 2;
- } else {
- damage /= 2;
- }
- }
- System.out.printf("%s - %d health, %.2f damage%n", demon, health, damage);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement