SHARE
TWEET

P02_LittleAlchemy

a guest Jun 7th, 2018 117 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayDeque;
  5. import java.util.Arrays;
  6.  
  7. public class P02_LittleAlchemy {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         ArrayDeque<Integer> stones = new ArrayDeque<>();
  12.         int[] s = Arrays.stream(reader.readLine().split(" "))
  13.                 .mapToInt(Integer::parseInt).toArray();
  14.  
  15.         for (int i : s) {
  16.             stones.addLast(i);
  17.         }
  18.         int gold = 0;
  19.  
  20.         String line = reader.readLine();
  21.  
  22.         while (true) {
  23.             String[] commandArgs = line.split(" ");
  24.             switch (commandArgs[0]) {
  25.                 case "Apply":
  26.                     gold = applyAcid(stones, gold, commandArgs[2]);
  27.                     break;
  28.                 case "Air":
  29.                     gold = airLeak(stones, gold, commandArgs[2]);
  30.                     break;
  31.                 default:
  32.                     printResult(stones, gold);
  33.                     return;
  34.             }
  35.             line = reader.readLine();
  36.         }
  37.     }
  38.  
  39.     private static int airLeak(ArrayDeque<Integer> stones, int gold, String commandArg) {
  40.         if(gold <= 0){
  41.             return gold;
  42.         }
  43.         gold--;
  44.         stones.addLast(Integer.parseInt(commandArg));
  45.         return gold;
  46.     }
  47.  
  48.     private static int applyAcid(ArrayDeque<Integer> stones, int gold, String commandArg) {
  49.         int acid = Integer.parseInt(commandArg);
  50.         while (acid-- > 0 && !stones.isEmpty()){
  51.             int element = stones.pop();
  52.             if(--element <= 0){
  53.                 gold++;
  54.             }else{
  55.                 stones.addLast(element);
  56.             }
  57.         }
  58.         return gold;
  59.     }
  60.  
  61.     private static void printResult(ArrayDeque<Integer> stones, int gold) {
  62.         while (!stones.isEmpty()) {
  63.             System.out.print(stones.pop() + " ");
  64.         }
  65.         System.out.println("\n" + gold);
  66.     }
  67. }
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