Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Advance.Exams.JavaAdvancedExam.February2023;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayDeque;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.stream.Collectors;
- public class ApocalypsePreparation {
- private static final int PATCH_REQUIREMENT = 30;
- private static final int BANDAGE_REQUIREMENT = 40;
- private static final int MEDKIT_REQUIREMENT = 100;
- private static final String PATCH = "Patch";
- private static final String BANDAGE = "Bandage";
- private static final String MEDKIT = "MedKit";
- public static void main(String[] args) throws IOException {
- BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
- ArrayDeque<Integer> textiles = new ArrayDeque<>(Arrays.stream(scanner.readLine().split("\\s")).map(Integer::parseInt).collect(Collectors.toList()));
- ArrayDeque<Integer> medicaments = new ArrayDeque<>();
- int[] input = Arrays.stream(scanner.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
- Arrays.stream(input).forEach(medicaments::push);
- HashMap<String, Integer> items = new HashMap<>();
- while (!textiles.isEmpty() && !medicaments.isEmpty()) {
- int textile = textiles.poll();
- int medicament = medicaments.pop();
- int sum = textile + medicament;
- if (sum == PATCH_REQUIREMENT) {
- items.putIfAbsent(PATCH, 0);
- items.put(PATCH, items.get(PATCH) + 1);
- } else if (sum == BANDAGE_REQUIREMENT) {
- items.putIfAbsent(BANDAGE, 0);
- items.put(BANDAGE, items.get(BANDAGE) + 1);
- } else if (sum == MEDKIT_REQUIREMENT) {
- items.putIfAbsent(MEDKIT, 0);
- items.put(MEDKIT, items.get(MEDKIT) + 1);
- } else if (sum > MEDKIT_REQUIREMENT) {
- items.putIfAbsent(MEDKIT, 0);
- items.put(MEDKIT, items.get(MEDKIT) + 1);
- sum -= 100;
- int addReamaining = medicaments.pop() + sum;
- medicaments.push(addReamaining);
- } else {
- medicaments.push(medicament + 10);
- }
- }
- if (textiles.isEmpty() && medicaments.isEmpty()) {
- System.out.println("Textiles and medicaments are both empty.");
- } else if (textiles.isEmpty()){
- System.out.println("Textiles are empty.");
- } else {
- System.out.println("Medicaments are empty.");
- }
- items.entrySet().stream()
- .sorted((name1, name2) -> name1.getKey().compareTo(name2.getKey()))
- .sorted((n1, n2) -> Integer.compare(n2.getValue(), n1.getValue()))
- .forEach(item -> System.out.printf("%s - %d%n", item.getKey(), item.getValue()));
- if (!medicaments.isEmpty()) {
- System.out.println("Medicaments left: " + String.join(", ", medicaments.toString().replaceAll("[\\[\\]]", "")));
- } else if (!textiles.isEmpty()) {
- System.out.println("Textiles left: " + String.join(", ", textiles.toString().replaceAll("[\\[\\]]", "")));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment