Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class _03_TreasureFinder {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String regType = "(?:&+)(.+)(?:&+)";
- String regCoordinates = "(?:<+)(.+[^>])(?:>+)";
- Pattern pType = Pattern.compile(regType);
- Pattern pCoord = Pattern.compile(regCoordinates);
- int[] key = Arrays.stream(scanner.nextLine()
- .split("\\s+"))
- .mapToInt(Integer::parseInt)
- .toArray();
- String line = "";
- while (!"find".equals(line = scanner.nextLine())) {
- StringBuilder sb = new StringBuilder();
- if (key.length < line.length()) {
- for (int i = 0, j = 0; i < line.length(); i++, j++) {
- char curr = (char) ((int) line.charAt(i) - key[j]);
- sb.append(curr);
- if (j == key.length - 1) {
- j = -1;
- }
- }
- String output = sb.toString();
- Matcher mType = pType.matcher(output);
- Matcher mCoord = pCoord.matcher(output);
- if (mType.find() && mCoord.find()) {
- System.out.printf("Found %s at %s%n", mType.group(1), mCoord.group(1));
- }
- } else {
- for (int i = 0, j = 0; i < line.length(); i++, j++) {
- char curr = (char) ((int) line.charAt(i) - key[j]);
- sb.append(curr);
- }
- String output = sb.toString();
- Matcher mType = pType.matcher(output);
- Matcher mCoord = pCoord.matcher(output);
- if (mType.find() && mCoord.find()) {
- System.out.printf("Found %s at %s%n", mType.group(1), mCoord.group(1));
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement