5kov

10. Robotics

May 14th, 2023
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class Robotics {
  5.     public static void main(String[] args) {
  6.         Scanner Scanner = new Scanner(System.in);
  7.  
  8.         List<String> robots = Arrays.stream(Scanner.nextLine().split(";")).collect(Collectors.toList());
  9.         Map<String, Integer> robotTime = new LinkedHashMap<>();
  10.         ArrayDeque<String> freeRobots = new ArrayDeque<>();
  11.         for (int i = 0; i < robots.size(); i++) {
  12.             String[] temp = robots.get(i).split("-");
  13.             robotTime.put(temp[0], Integer.parseInt(temp[1]));
  14.             freeRobots.add(temp[0]);
  15.         }
  16.  
  17.         String startingTime = Scanner.nextLine();
  18.  
  19.         List<String> products = new ArrayList<>();
  20.         String input = Scanner.nextLine();
  21.         while (!input.equals("End")) {
  22.             products.add(input);
  23.             input = Scanner.nextLine();
  24.         }
  25.         ArrayDeque<String> productsQueue = new ArrayDeque<>(products);
  26.         Map<String, String> robotsBusyUntil = new LinkedHashMap<>();
  27.  
  28.         String currentTime = startingTime;
  29.  
  30.         while (!productsQueue.isEmpty()) {
  31.             currentTime = calcTime(currentTime, 1);
  32.             ArrayList toRemove = new ArrayList();
  33.             if (robotsBusyUntil.containsValue(currentTime)) {
  34.                 for(Map.Entry<String, String> entry : robotsBusyUntil.entrySet()) {
  35.                     if (entry.getValue().equals(currentTime)) {
  36.                         freeRobots.add(entry.getKey());
  37.                         toRemove.add(entry.getKey());
  38.                     }
  39.                 }
  40.             }
  41.  
  42.             for (int i = 0; i < toRemove.size(); i++) {
  43.                 robotsBusyUntil.remove(toRemove.get(i));
  44.             }
  45.             toRemove.clear();
  46.  
  47.             if (!freeRobots.isEmpty()) {
  48.                 String tempProduct = productsQueue.remove();
  49.                 String tempRobot = freeRobots.remove();
  50.                 System.out.println(tempRobot + " - " + tempProduct + " [" + currentTime + "]");
  51.                 robotsBusyUntil.put(tempRobot, calcTime(currentTime, robotTime.get(tempRobot)));
  52.             } else {
  53.                 productsQueue.add(productsQueue.remove());
  54.             }
  55.         }
  56.     }
  57.  
  58.     public static String calcTime(String currentTime, int increaseWithSeconds) {
  59.         int[] time = Arrays.stream(currentTime.split(":")).mapToInt(e -> Integer.parseInt(e)).toArray();
  60.         int timeInSeconds = (time[0] * 3600) + (time[1] * 60) + time[2];
  61.         timeInSeconds = timeInSeconds + increaseWithSeconds;
  62.         int newHours = timeInSeconds / 3600;
  63.         if (newHours > 23) {
  64.             newHours = 0;
  65.         }
  66.         timeInSeconds = timeInSeconds % 3600;
  67.         int newMinutes = timeInSeconds / 60;
  68.         timeInSeconds = timeInSeconds % 60;
  69.         int newSeconds = timeInSeconds;
  70.         return String.format("%02d:%02d:%02d", newHours, newMinutes, newSeconds);
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment