Advertisement
Ligh7_of_H3av3n

01. Worms And Holes

Jun 14th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. package Uprajnenie;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Deque;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Queue;
  10. import java.util.Scanner;
  11.  
  12. public class WormsAndHoles {
  13.     public static void main(String[] args) {
  14.         Scanner scanner = new Scanner(System.in);
  15.  
  16.  
  17.         // Reading worms and holes
  18.         String[] wormsInput = scanner.nextLine().split(" ");
  19.         String[] holesInput = scanner.nextLine().split(" ");
  20.  
  21.         Deque<Integer> worms = new ArrayDeque<>();
  22.         Queue<Integer> holes = new LinkedList<>();
  23.  
  24.         // Filling the stack with worms
  25.         for (String worm : wormsInput) {
  26.             worms.push(Integer.parseInt(worm));
  27.         }
  28.  
  29.         // Filling the queue with holes
  30.         for (String hole : holesInput) {
  31.             holes.offer(Integer.parseInt(hole));
  32.         }
  33.  
  34.         int matchesCount = 0;
  35.  
  36.         // Process worms and holes
  37.         while (!worms.isEmpty() && !holes.isEmpty()) {
  38.             int worm = worms.pop();
  39.             int hole = holes.poll();
  40.  
  41.             if (worm == hole) {
  42.                 matchesCount++;
  43.             } else if (worm > hole) {
  44.                 worm -= 3;
  45.                 if (worm > 0) {
  46.                     worms.push(worm);
  47.                 }
  48.             }
  49.         }
  50.  
  51.         // Output results
  52.         if (matchesCount > 0) {
  53.             System.out.println("Matches: " + matchesCount);
  54.         } else {
  55.             System.out.println("There are no matches.");
  56.         }
  57.  
  58.         if (worms.isEmpty()) {
  59.             if (matchesCount == wormsInput.length) {
  60.                 System.out.println("Every worm found a suitable hole!");
  61.             } else {
  62.                 System.out.println("Worms left: none");
  63.             }
  64.         } else {
  65.             System.out.print("Worms left: ");
  66.             List<Integer> remainingWorms = new ArrayList<>(worms);
  67.             Collections.reverse(remainingWorms);
  68.             for (int i = 0; i < remainingWorms.size(); i++) {
  69.                 System.out.print(remainingWorms.get(i));
  70.                 if (i < remainingWorms.size() - 1) {
  71.                     System.out.print(", ");
  72.                 }
  73.             }
  74.             System.out.println();
  75.         }
  76.  
  77.         if (holes.isEmpty()) {
  78.             System.out.println("Holes left: none");
  79.         } else {
  80.             System.out.print("Holes left: ");
  81.             List<Integer> remainingHoles = new ArrayList<>(holes);
  82.             for (int i = 0; i < remainingHoles.size(); i++) {
  83.                 System.out.print(remainingHoles.get(i));
  84.                 if (i < remainingHoles.size() - 1) {
  85.                     System.out.print(", ");
  86.                 }
  87.             }
  88.             System.out.println();
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement