Guest User

Untitled

a guest
Jun 29th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.Collections;
  6. import java.util.List;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9. import java.util.stream.Collectors;
  10.  
  11. public class Pr02ArcheryTournamentSimple {
  12.  
  13.     private static final Pattern TARGETS_PATTERN = Pattern.compile("\\|");
  14.  
  15.     private static final Matcher SHOOT_MATCHER = Pattern
  16.             .compile("^Shoot (?<direction>Left|Right)@(?<start>\\d+)@(?<length>\\d+)$")
  17.             .matcher("");
  18.  
  19.     public static void main(String[] args) throws IOException {
  20.  
  21.         BufferedReader reader = new BufferedReader(
  22.                 new InputStreamReader(System.in, StandardCharsets.UTF_8));
  23.  
  24.         List<Integer> targets = TARGETS_PATTERN
  25.                 .splitAsStream(reader.readLine())
  26.                 .map(Integer::parseInt)
  27.                 .collect(Collectors.toList());
  28.  
  29.         int points = 0;
  30.  
  31.         String command;
  32.         while (!"Game over".equals(command = reader.readLine())) {
  33.             if ("Reverse".equals(command)) {
  34.                 Collections.reverse(targets);
  35.             } else if (SHOOT_MATCHER.reset(command).matches()) {
  36.                 String direction = SHOOT_MATCHER.group("direction");
  37.                 int startIndex = Integer.parseInt(SHOOT_MATCHER.group("start"));
  38.                 int length = Integer.parseInt(SHOOT_MATCHER.group("length"));
  39.  
  40.                 int newPoints = shootAtTarget(targets, direction, startIndex, length);
  41.                 points += newPoints;
  42.             } else {
  43.                 // throw new IllegalArgumentException("Invalid command: " + command);
  44.             }
  45.         }
  46.  
  47.         System.out.println(targets.stream().map(String::valueOf).collect(Collectors.joining(" - ")));
  48.         System.out.printf("Iskren finished the archery tournament with %d points!", points);
  49.     }
  50.  
  51.     private static int shootAtTarget(List<Integer> targets,
  52.                                      String direction, int startIndex, int length) {
  53.         int pointsScored = 0;
  54.  
  55.         if (startIndex >= 0 && startIndex < targets.size()) {
  56.             length %= targets.size();
  57.  
  58.             int offset = "Right".equals(direction) ? length : (targets.size() - length);
  59.  
  60.             int targetIndex = (startIndex + offset) % targets.size();
  61.  
  62.             Integer targetPoints = targets.get(targetIndex);
  63.             pointsScored = Math.min(5, targetPoints);
  64.             targetPoints -= pointsScored;
  65.             targets.set(targetIndex, targetPoints);
  66.         }
  67.  
  68.         return pointsScored;
  69.     }
  70. }
Add Comment
Please, Sign In to add comment