Advertisement
Lyubohd

Little Alchemy

Jun 6th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayDeque;
  5. import java.util.Stack;
  6.  
  7. public class P01_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.         Stack<Integer> storage = new Stack<>();
  13.         String[] inputLine = reader.readLine().split("\\s+");
  14.  
  15.         for (String stone : inputLine) {
  16.             stones.add(Integer.parseInt(stone));
  17.         }
  18.  
  19.         while (true) {
  20.             String input = reader.readLine();
  21.             if ("Revision".equals(input)) {
  22.                 break;
  23.             }
  24.             String[] tokens = input.split("\\s+");
  25.             String command = tokens[0] + " " + tokens[1];
  26.             int doses = Integer.parseInt(tokens[2]);
  27.             if ("Apply acid".equals(command) && !stones.isEmpty()) {
  28.                 for (int i = 0; i < doses; i++) {
  29.                     int currentStone = stones.remove();
  30.                     currentStone--;
  31.                     if (currentStone == 0) {
  32.                         storage.push(currentStone);
  33.                     } else {
  34.                         stones.add(currentStone);
  35.                     }
  36.                     if (stones.isEmpty()) {
  37.                         break;
  38.                     }
  39.                 }
  40.             } else if ("Air leak".equals(command) && !storage.isEmpty()) {
  41.                 int gold = storage.pop();
  42.                 gold += doses;
  43.                 stones.add(gold);
  44.             }
  45.         }
  46.  
  47.         if (!stones.isEmpty()) {
  48.             for (Integer stone : stones) {
  49.                 System.out.print(stone + " ");
  50.             }
  51.         }
  52.         System.out.println(System.lineSeparator() + storage.size());
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement