Advertisement
cnencheva

SpaceshipCrafting

Oct 11th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. package spaceStationRecruitment;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Main {
  7.     private final static int GLASS = 25;
  8.     private final static int ALUMINIUM = 50;
  9.     private final static int LITHIUM = 75;
  10.     private final static int CARBON_FIBER = 100;
  11.  
  12.     public static void main(String[] args) {
  13.         Scanner scanner = new Scanner(System.in);
  14.         List<Integer> input = getInput(scanner);
  15.         Deque<Integer> chemicalLiquids = new ArrayDeque<>(input);
  16.         input = getInput(scanner);
  17.         Collections.reverse(input);
  18.         Deque<Integer> physicalItems = new ArrayDeque<>(input);
  19.  
  20.  
  21.         Map<String, Integer> advancedMaterialReached = new LinkedHashMap<>();
  22.         advancedMaterialReached.put("Aluminium", 0);
  23.         advancedMaterialReached.put("Carbon fiber", 0);
  24.         advancedMaterialReached.put("Glass", 0);
  25.         advancedMaterialReached.put("Lithium", 0);
  26.  
  27.         boolean glassReached = false;
  28.         boolean aluminiumReached = false;
  29.         boolean lithiumReached = false;
  30.         boolean carbonFiberReached = false;
  31.  
  32.         while (!chemicalLiquids.isEmpty() && !physicalItems.isEmpty()) {
  33.             int currentLiquid = chemicalLiquids.pollFirst();
  34.             int currentItem = physicalItems.pop();
  35.             int sumOfReaction = currentLiquid + currentItem;
  36.  
  37.             if (sumOfReaction == GLASS) {
  38.                 advancedMaterialReached.put("Glass", advancedMaterialReached.get("Glass") + 1);
  39.                 glassReached = true;
  40.             } else if (sumOfReaction == ALUMINIUM) {
  41.                 advancedMaterialReached.put("Aluminium", advancedMaterialReached.get("Aluminium") + 1);
  42.                 aluminiumReached = true;
  43.             } else if (sumOfReaction == LITHIUM) {
  44.                 advancedMaterialReached.put("Lithium", advancedMaterialReached.get("Lithium") + 1);
  45.                 lithiumReached = true;
  46.             } else if (sumOfReaction == CARBON_FIBER) {
  47.                 advancedMaterialReached.put("Carbon fiber", advancedMaterialReached.get("Carbon fiber") + 1);
  48.                 carbonFiberReached = true;
  49.             } else {
  50.                 currentItem += 3;
  51.                 physicalItems.push(currentItem);
  52.             }
  53.         }
  54.         StringBuilder output = new StringBuilder();
  55.  
  56.         if (glassReached && aluminiumReached && lithiumReached && carbonFiberReached) {
  57.             output.append("Wohoo! You succeeded in building the spaceship!")
  58.                     .append(System.lineSeparator());
  59.         } else {
  60.             output.append("Ugh, what a pity! You didn't have enough materials to build the spaceship.")
  61.                     .append(System.lineSeparator());
  62.         }
  63.  
  64.         if (chemicalLiquids.isEmpty()) {
  65.             output.append("Liquids left: none")
  66.                     .append(System.lineSeparator());
  67.         } else {
  68.             output.append("Liquids left: ");
  69.             while (!chemicalLiquids.isEmpty()) {
  70.                 output.append(chemicalLiquids.poll());
  71.                 output.append(", ");
  72.             }
  73.             output.delete(output.length() - 2, output.length() - 1).append(System.lineSeparator());
  74.         }
  75.  
  76.         if (physicalItems.isEmpty()) {
  77.             output.append("Physical items left: none").append(System.lineSeparator());
  78.         } else {
  79.             output.append("Physical items left: ");
  80.             while (!physicalItems.isEmpty()) {
  81.                 output.append(physicalItems.pop());
  82.                 output.append(", ");
  83.             }
  84.             output.delete(output.length() - 2, output.length() - 1).append(System.lineSeparator());
  85.         }
  86.  
  87.         for (Map.Entry<String, Integer> entry : advancedMaterialReached.entrySet()) {
  88.             output.append(entry.getKey()).append(": ").append(entry.getValue()).append(System.lineSeparator());
  89.         }
  90.         System.out.println(output.toString());
  91.     }
  92.  
  93.     private static List<Integer> getInput(Scanner scanner) {
  94.         return Arrays.stream(scanner.nextLine().split("\\s")).map(Integer::parseInt).collect(Collectors.toList());
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement