borovaneca

Robotics

Feb 1st, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. package Advance.StacksAndQueues.Exercise;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.text.DecimalFormat;
  7. import java.util.ArrayDeque;
  8. import java.util.Arrays;
  9.  
  10.  
  11. public class Main {
  12.     public static void main(String[] args) throws IOException {
  13.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  14.         String input = bufferedReader.readLine();
  15.  
  16.         Robot[] robots = createRobots(input);
  17.  
  18.         input = bufferedReader.readLine();
  19.  
  20.         int time = getStarTime(input);
  21.  
  22.         ArrayDeque<String> itemsQueue = createItemsQueue(bufferedReader);
  23.  
  24.  
  25.         while (!itemsQueue.isEmpty()) {
  26.             String item = itemsQueue.removeFirst();
  27.             boolean isItemProcessed = false;
  28.             time++;
  29.             for (int i = 0; i < robots.length; i++) {
  30.  
  31.                 if (!robots[i].isWorking) {
  32.                     robots[i].isWorking = true;
  33.                     System.out.println(String.format("%s - %s [%s]", robots[i].name, item, calculateTime(time)));
  34.                     isItemProcessed = true;
  35.                     break;
  36.                 }
  37.  
  38.             }
  39.  
  40.             if (!isItemProcessed) {
  41.                 itemsQueue.addLast(item);
  42.             }
  43.             allRobotsWork(robots);
  44.  
  45.         }
  46.     }
  47.  
  48.     private static void allRobotsWork(Robot[] robots) {
  49.         for (Robot robot : robots) {
  50.             robot.work();
  51.         }
  52.     }
  53.  
  54.     private static ArrayDeque<String> createItemsQueue(BufferedReader bufferedReader) throws IOException {
  55.         ArrayDeque<String> items = new ArrayDeque<>();
  56.  
  57.         String item = bufferedReader.readLine();
  58.  
  59.         while (!item.equals("End")) {
  60.             items.addLast(item);
  61.             item = bufferedReader.readLine();
  62.         }
  63.  
  64.         return items;
  65.     }
  66.  
  67.  
  68.     private static Long createTime(String input) {
  69.         Integer[] timeInput = Arrays.stream(input.split(":")).map(Integer::parseInt).toArray(Integer[]::new);
  70.         Long totalTime = 0L;
  71.         totalTime += timeInput[0] * 3600;
  72.         totalTime += timeInput[1] * 60;
  73.         totalTime += timeInput[2];
  74.  
  75.         return totalTime;
  76.     }
  77.  
  78.     private static int getStarTime(String line) throws IOException {
  79.         String[] tokens = line.split(":");
  80.         int hours = Integer.parseInt(tokens[0]);
  81.         int minutes = Integer.parseInt(tokens[1]);
  82.         int seconds = Integer.parseInt(tokens[2]);
  83.         seconds += (minutes * 60);
  84.         seconds += (hours * 3600);
  85.         return seconds;
  86.     }
  87.  
  88.  
  89.     private static Robot[] createRobots(String input) {
  90.         String[] robotsInfo = input.split(";");
  91.         Robot[] robots = new Robot[robotsInfo.length];
  92.         for (int i = 0; i < robotsInfo.length; i++) {
  93.             String[] currentRobotInfo = robotsInfo[i].split("-");
  94.             String robotName = currentRobotInfo[0];
  95.             Long robotProcessingTime = Long.parseLong(currentRobotInfo[1]);
  96.             robots[i] = new Robot(robotName, robotProcessingTime);
  97.         }
  98.         return robots;
  99.     }
  100.  
  101.     private static class Robot {
  102.         private String name;
  103.         private Long processingTime;
  104.         private Long currentProcessingTime;
  105.         private boolean isWorking;
  106.  
  107.         public Robot(String name, Long processingTime) {
  108.             this.name = name;
  109.             this.processingTime = processingTime;
  110.             this.currentProcessingTime = 0L;
  111.             this.isWorking = false;
  112.         }
  113.  
  114.         public void work() {
  115.             if (isWorking) {
  116.                 this.currentProcessingTime++;
  117.                 if (currentProcessingTime >= this.processingTime) {
  118.                     this.isWorking = false;
  119.                     this.currentProcessingTime = 0L;
  120.                 }
  121.             }
  122.         }
  123.     }
  124.     private static String calculateTime(int time) {
  125.         int hours = (time / 3600) % 24;
  126.         int timeLeft = time % 3600;
  127.         int minutes = timeLeft / 60;
  128.         int seconds = timeLeft % 60;
  129.         return String.format("%02d:%02d:%02d", hours, minutes, seconds);
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment