LardaX

InfernoIII

Feb 17th, 2017
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5. import java.util.function.BiPredicate;
  6. import java.util.stream.Stream;
  7.  
  8. public class InfernoIII {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.         Integer[] gems = Stream.of(reader.readLine().split(" ")).map(Integer::parseInt).toArray(Integer[]::new);
  12.         Map<String, BiPredicate<Integer, Integer[]>> predicates = new HashMap<>();
  13.  
  14.         String command;
  15.         while (!"Forge".equals(command = reader.readLine())) {
  16.             String[] commandArgs = command.split(";");
  17.             String commandType = commandArgs[0];
  18.             String sumType = commandArgs[1];
  19.             int exclusionSum = Integer.parseInt(commandArgs[2]);
  20.  
  21.             switch (commandType) {
  22.                 case "Exclude":
  23.                     BiPredicate<Integer, Integer[]> filter = createFilter(sumType, exclusionSum);
  24.                     predicates.put(sumType + exclusionSum, filter);
  25.                     break;
  26.  
  27.                 case "Reverse":
  28.                     predicates.remove(sumType + exclusionSum);
  29.             }
  30.         }
  31.         List<String> gemsResults = new ArrayList<>();
  32.  
  33.         BiPredicate<Integer, Integer[]> filter = predicates.values().stream()
  34.                 .reduce(BiPredicate::and)
  35.                 .orElse((x, y) -> true);
  36.  
  37.         for (int i = 0; i < gems.length; i++) {
  38.             if (filter.test(i, gems)) {
  39.                 gemsResults.add(String.valueOf(gems[i]));
  40.             }
  41.         }
  42.  
  43.         System.out.println(String.join(" ", gemsResults));
  44.     }
  45.  
  46.     private static BiPredicate<Integer, Integer[]> createFilter(String sumType, int exclusionSum) {
  47.         switch (sumType) {
  48.             case "Sum Left":
  49.                 return (i, a) -> {
  50.                     int currentNum = a[i];
  51.                     int leftNum;
  52.                     if (i - 1 < 0) {
  53.                         leftNum = 0;
  54.                     } else {
  55.                         leftNum = a[i - 1];
  56.                     }
  57.  
  58.                     return currentNum + leftNum != exclusionSum;
  59.                 };
  60.             case "Sum Right":
  61.                 return (i, a) -> {
  62.                     int currentNum = a[i];
  63.                     int rightNum;
  64.                     if (i + 1 >= a.length) {
  65.                         rightNum = 0;
  66.                     } else {
  67.                         rightNum = a[i + 1];
  68.                     }
  69.                     return currentNum + rightNum != exclusionSum;
  70.                 };
  71.  
  72.             case "Sum Left Right":
  73.                 return (i, a) -> {
  74.                     int currentNum = a[i];
  75.                     int leftNum;
  76.                     if (i - 1 < 0) {
  77.                         leftNum = 0;
  78.                     } else {
  79.                         leftNum = a[i - 1];
  80.                     }
  81.                     int rightNum;
  82.                     if (i + 1 >= a.length) {
  83.                         rightNum = 0;
  84.                     } else {
  85.                         rightNum = a[i + 1];
  86.                     }
  87.                     return currentNum + leftNum + rightNum != exclusionSum;
  88.                 };
  89.  
  90.             default:
  91.                 return null;
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment