Advertisement
damesova

Treasure Finder [Mimi]

Mar 24th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class _03_TreasureFinder {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String regType = "(?:&+)(.+)(?:&+)";
  11.         String regCoordinates = "(?:<+)(.+[^>])(?:>+)";
  12.  
  13.         Pattern pType = Pattern.compile(regType);
  14.         Pattern pCoord = Pattern.compile(regCoordinates);
  15.  
  16.         int[] key = Arrays.stream(scanner.nextLine()
  17.                 .split("\\s+"))
  18.                 .mapToInt(Integer::parseInt)
  19.                 .toArray();
  20.  
  21.         String line = "";
  22.  
  23.         while (!"find".equals(line = scanner.nextLine())) {
  24.             StringBuilder sb = new StringBuilder();
  25.             if (key.length < line.length()) {
  26.                 for (int i = 0, j = 0; i < line.length(); i++, j++) {
  27.                     char curr = (char) ((int) line.charAt(i) - key[j]);
  28.                     sb.append(curr);
  29.                     if (j == key.length - 1) {
  30.                         j = -1;
  31.                     }
  32.                 }
  33.                 String output = sb.toString();
  34.                 Matcher mType = pType.matcher(output);
  35.                 Matcher mCoord = pCoord.matcher(output);
  36.  
  37.                 if (mType.find() && mCoord.find()) {
  38.                     System.out.printf("Found %s at %s%n", mType.group(1), mCoord.group(1));
  39.                 }
  40.             } else {
  41.                 for (int i = 0, j = 0; i < line.length(); i++, j++) {
  42.                     char curr = (char) ((int) line.charAt(i) - key[j]);
  43.                     sb.append(curr);
  44.                 }
  45.                 String output = sb.toString();
  46.                 Matcher mType = pType.matcher(output);
  47.                 Matcher mCoord = pCoord.matcher(output);
  48.  
  49.                 if (mType.find() && mCoord.find()) {
  50.                     System.out.printf("Found %s at %s%n", mType.group(1), mCoord.group(1));
  51.                 }
  52.             }
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement