Advertisement
Didart

Robotics

Dec 29th, 2022
949
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package StacksAndQueues1;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Deque;
  5. import java.util.Scanner;
  6.  
  7. public class Robotics {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         String[] robotsTokens = scanner.nextLine().split(";");
  12.  
  13.         String[] robots = new String[robotsTokens.length];
  14.         int[] processTime = new int[robotsTokens.length];
  15.         int[] robotsCounter = new int[robotsTokens.length];
  16.  
  17.         for (int i = 0; i < robotsTokens.length; i++) {
  18.             String[] robotArgs = robotsTokens[i].split("-");
  19.             robots[i] = robotArgs[0];
  20.             processTime[i] = Integer.valueOf(robotArgs[1]);
  21.         }
  22.  
  23.         String[] timeTokens = scanner.nextLine().split(":");
  24.         long hh = Integer.valueOf(timeTokens[0]) * 60 * 60;
  25.         long mm = Integer.valueOf(timeTokens[1]) * 60;
  26.         long ss = Integer.valueOf(timeTokens[2]);
  27.         long time = hh + mm + ss;
  28.  
  29.         Deque<String> products = new ArrayDeque<>();
  30.  
  31.         String product = "";
  32.         while (!("End".equals(product = scanner.nextLine()))) {
  33.             products.add(product);
  34.         }
  35.  
  36.         while (!products.isEmpty()) {
  37.             for (int i = 0; i < robots.length; i++) {
  38.                 if (robotsCounter[i] > 0) {
  39.                     robotsCounter[i]--;
  40.                 }
  41.             }
  42.             time++;
  43.             String currentProduct = products.poll();
  44.             boolean isProductTaken = false;
  45.  
  46.             for (int i = 0; i < robots.length; i++) {
  47.                 if (robotsCounter[i] == 0) {
  48.                     robotsCounter[i] = processTime[i];
  49.                     System.out.printf("%s - %s [%s]%n", robots[i], currentProduct, convertSecondsToHMmSs(time));
  50.                     isProductTaken = true;
  51.                     break;
  52.                 }
  53.             }
  54.             if (!isProductTaken) {
  55.                 products.offer(currentProduct);
  56.             }
  57.         }
  58.     }
  59.  
  60.     private static String convertSecondsToHMmSs(long seconds) {
  61.         long s = seconds % 60;
  62.         long m = (seconds / 60) % 60;
  63.         long h = (seconds / (60 * 60)) % 24;
  64.         return String.format("%02d:%02d:%02d", h, m, s);
  65.  
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement