Yargi

PokemonDontGo

Jun 28th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class PokemonDontGo {
  7.     public static void main(String[] args) {
  8.         // instantiate scanner class read input and convert to list of integer
  9.         Scanner sc = new Scanner(System.in);
  10.         List<Integer> pokes = Arrays.stream(sc.nextLine().split("\\s+")).map(Integer::parseInt).collect(Collectors.toList());
  11.  
  12.         // declare int variable for summary of catches
  13.         int sumOfPokes = 0;
  14.  
  15.         // loop until no pokes left
  16.         while (!pokes.isEmpty()){
  17.  
  18.             // get int from scanner (console)
  19.             int index = Integer.parseInt(sc.nextLine());
  20.  
  21.             // declare int variable actual pokemon
  22.             int actualPoke = 0;
  23.             // on negative index remove first index and copy last to first index
  24.             if (index < 0){
  25.               actualPoke = pokes.get(0);
  26.               pokes.remove(0);
  27.               pokes.add(0, pokes.get(pokes.size() - 1));
  28.             }
  29.             // on index greater than list size remove last index and copy first to last index
  30.             else if (index > pokes.size() - 1){
  31.                 actualPoke = pokes.get(pokes.size() - 1);
  32.                 pokes.remove(pokes.size() - 1);
  33.                 pokes.add(pokes.get(0));
  34.             }
  35.             // on index in range of list remove index
  36.             else {
  37.                 actualPoke = pokes.get(index);
  38.                 pokes.remove(index);
  39.             }
  40.             // when the list is not empty calculate all remaining values
  41.             if (!pokes.isEmpty()){
  42.                 for (int i = 0; i < pokes.size(); i++){
  43.                     int p = pokes.get(i);
  44.                     if (p <= actualPoke){
  45.                         p += actualPoke;
  46.                     }
  47.                     else {
  48.                         p -= actualPoke;
  49.                     }
  50.                     pokes.remove(i);
  51.                     pokes.add(i, p);
  52.                 }
  53.             }
  54.             // sum the actual removed Pokemon
  55.             sumOfPokes += actualPoke;
  56.         }
  57.         // print the result to console
  58.         System.out.println(sumOfPokes);
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment