Advertisement
Guest User

Untitled

a guest
Nov 25th, 2020
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 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.Arrays;
  6. import java.util.NoSuchElementException;
  7. import java.util.Optional;
  8. import java.util.PrimitiveIterator;
  9. import java.util.function.Function;
  10. import java.util.function.UnaryOperator;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. import java.util.stream.Collectors;
  14.  
  15. public class Pr03TreasureFinder {
  16.  
  17.     private static final Pattern MESSAGE_PATTERN = Pattern.compile("^.*&(?<type>.+)&.*<(?<coordinates>.+)>$");
  18.  
  19.     public static void main(String[] args) throws IOException {
  20.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
  21.  
  22.         int[] keys = Arrays.stream(reader.readLine().split("\\s+"))
  23.                 .mapToInt(Integer::parseInt)
  24.                 .toArray();
  25.  
  26.         KeySupplier keySupplier = new KeySupplier(keys);
  27.  
  28.         UnaryOperator<String> decryptMessage = encryptedMessage -> {
  29.             keySupplier.reset();
  30.             return encryptedMessage.chars()
  31.                     .map(charValue -> charValue - keySupplier.nextInt())
  32.                     .mapToObj(charValue -> (char) charValue)
  33.                     .collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
  34.                     .toString();
  35.         };
  36.  
  37.         Function<String, Optional<String>> treasureOptional = decryptedMessage -> {
  38.             Matcher matcher = MESSAGE_PATTERN.matcher(decryptedMessage);
  39.             if (!matcher.matches()) {
  40.                 return Optional.empty();
  41.             }
  42.             String type = matcher.group("type");
  43.             String coordinates = matcher.group("coordinates");
  44.             return Optional.of(String.format("Found %s at %s", type, coordinates));
  45.         };
  46.  
  47.         System.out.println(reader
  48.                 .lines()
  49.                 .takeWhile(message -> !"find".equals(message))
  50.                 .map(decryptMessage)
  51.                 .map(treasureOptional)
  52.                 .filter(Optional::isPresent)
  53.                 .map(Optional::get)
  54.                 .collect(Collectors.joining(System.lineSeparator()))
  55.         );
  56.     }
  57.  
  58.     private static final class KeySupplier implements PrimitiveIterator.OfInt {
  59.  
  60.         private final int[] keys;
  61.         private int index;
  62.  
  63.         private KeySupplier(int[] keys) {
  64.             this.keys = keys;
  65.             index = 0;
  66.         }
  67.  
  68.         public void reset() {
  69.             index = 0;
  70.         }
  71.  
  72.         @Override
  73.         public boolean hasNext() {
  74.             if (keys.length == 0) {
  75.                 return false;
  76.             }
  77.  
  78.             if (index >= keys.length) {
  79.                 index = 0;
  80.             }
  81.  
  82.             return true;
  83.         }
  84.  
  85.         @Override
  86.         public int nextInt() {
  87.             if (!hasNext()) {
  88.                 throw new NoSuchElementException();
  89.             }
  90.  
  91.             return keys[index++];
  92.         }
  93.  
  94.         @Override
  95.         public Integer next() {
  96.             return nextInt();
  97.         }
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement