Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2022
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.Console;
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6.  
  7. public class P1 {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         ArrayDeque<Integer> ingredients = Arrays
  12.                 .stream(scanner.nextLine().split("\\s+"))
  13.                 .map(Integer::parseInt)
  14.                 .collect(Collectors.toCollection(ArrayDeque::new));
  15.  
  16.         ArrayDeque<Integer> freshnessLevels = Arrays
  17.                 .stream(scanner.nextLine().split("\\s+"))
  18.                 .map(Integer::parseInt)
  19.                 .collect(Collectors.toCollection(ArrayDeque::new));
  20.  
  21.         Map<String, Integer> cocktailsLevels = Map.of(
  22.                 "Pear Sour", 150,
  23.                 "The Harvest", 250,
  24.                 "Apple Hinny", 300,
  25.                 "High Fashion", 400
  26.         );
  27.  
  28.         Map<String, Integer> cocktailsQuantity = new TreeMap<>();
  29.  
  30.         while (!ingredients.isEmpty() && !freshnessLevels.isEmpty()) {
  31.             int ingredient = ingredients.removeFirst();
  32.             if (ingredient == 0) {
  33.                 continue;
  34.             }
  35.  
  36.             int freshness = freshnessLevels.removeLast();
  37.             int totalFreshness = ingredient * freshness;
  38.  
  39.             Optional<String> cocktailOpt = cocktailsLevels.entrySet().stream()
  40.                     .filter(kvp -> kvp.getValue() == totalFreshness)
  41.                     .map(Map.Entry::getKey)
  42.                     .findFirst();
  43.  
  44.             if (cocktailOpt.isPresent()) {
  45.                 cocktailsQuantity.merge(cocktailOpt.get(), 1, (k, ignored) -> k + 1);
  46.             } else {
  47.                 ingredient = ingredient + 5;
  48.                 ingredients.addLast(ingredient);
  49.             }
  50.         }
  51.  
  52.         if (cocktailsQuantity.size() == 4) {
  53.             System.out.println("It's party time! The cocktails are ready!");
  54.         } else {
  55.             System.out.println("What a pity! You didn't manage to prepare all cocktails.");
  56.         }
  57.  
  58.  
  59.         int remainingIngredientsSum = ingredients.stream().mapToInt(Integer::intValue).sum();
  60.  
  61.         if (remainingIngredientsSum > 0) {
  62.             System.out.printf("Ingredients left: %d%n", remainingIngredientsSum);
  63.         }
  64.  
  65.         cocktailsQuantity.entrySet().stream()
  66.                 .map(kvp -> String.format(" # %s --> %d", kvp.getKey(), kvp.getValue()))
  67.                 .forEach(System.out::println);
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement