daily pastebin goal
79%
SHARE
TWEET

TreasureFinder

a guest Nov 19th, 2018 83 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. package Lection8;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class TreasureFinder {
  10.     public static void main(String[] args) {
  11.         Scanner scan = new Scanner(System.in);
  12.         String[] key = scan.nextLine().split("\\s+");
  13.         String line = scan.nextLine();
  14.         List<String> messages = new ArrayList<>();
  15.  
  16.         while (!line.equals("find")){
  17.             messages.add(line);
  18.             line = scan.nextLine();
  19.         }
  20.  
  21.         for (int k = 0; k < messages.size(); k++) {
  22.             String encryptedStr = "";
  23.             int j = 0;
  24.             for (int i = 0; i < messages.get(k).length(); i++) {
  25.                 char symbol = messages.get(k).charAt(i);
  26.                 symbol -= key[j].charAt(0) - '0';
  27.                 encryptedStr += symbol;
  28.                 j++;
  29.                 if (j == key.length){
  30.                     j = 0;
  31.                 }
  32.             }
  33.             messages.set(k, encryptedStr);
  34.         }
  35.  
  36.         Pattern treasure = Pattern.compile("&(?<treasure>[A-Za-z]+)&");
  37.         Pattern coordinates = Pattern.compile("<(?<coordinates>[A-Z0-9]+)>");
  38.  
  39.         for (String message : messages) {
  40.             Matcher matchTreasure = treasure.matcher(message);
  41.             Matcher matchCoord = coordinates.matcher(message);
  42.             if (matchTreasure.find() && matchCoord.find()){
  43.                 System.out.println(String.format("Found %s at %s",
  44.                         matchTreasure.group("treasure"),
  45.                         matchCoord.group("coordinates")));
  46.             }
  47.         }
  48.  
  49.  
  50.     }
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top