Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class Robotics {
- public static void main(String[] args) {
- Scanner Scanner = new Scanner(System.in);
- List<String> robots = Arrays.stream(Scanner.nextLine().split(";")).collect(Collectors.toList());
- Map<String, Integer> robotTime = new LinkedHashMap<>();
- ArrayDeque<String> freeRobots = new ArrayDeque<>();
- for (int i = 0; i < robots.size(); i++) {
- String[] temp = robots.get(i).split("-");
- robotTime.put(temp[0], Integer.parseInt(temp[1]));
- freeRobots.add(temp[0]);
- }
- String startingTime = Scanner.nextLine();
- List<String> products = new ArrayList<>();
- String input = Scanner.nextLine();
- while (!input.equals("End")) {
- products.add(input);
- input = Scanner.nextLine();
- }
- ArrayDeque<String> productsQueue = new ArrayDeque<>(products);
- Map<String, String> robotsBusyUntil = new LinkedHashMap<>();
- String currentTime = startingTime;
- while (!productsQueue.isEmpty()) {
- currentTime = calcTime(currentTime, 1);
- ArrayList toRemove = new ArrayList();
- if (robotsBusyUntil.containsValue(currentTime)) {
- for(Map.Entry<String, String> entry : robotsBusyUntil.entrySet()) {
- if (entry.getValue().equals(currentTime)) {
- freeRobots.add(entry.getKey());
- toRemove.add(entry.getKey());
- }
- }
- }
- for (int i = 0; i < toRemove.size(); i++) {
- robotsBusyUntil.remove(toRemove.get(i));
- }
- toRemove.clear();
- if (!freeRobots.isEmpty()) {
- String tempProduct = productsQueue.remove();
- String tempRobot = freeRobots.remove();
- System.out.println(tempRobot + " - " + tempProduct + " [" + currentTime + "]");
- robotsBusyUntil.put(tempRobot, calcTime(currentTime, robotTime.get(tempRobot)));
- } else {
- productsQueue.add(productsQueue.remove());
- }
- }
- }
- public static String calcTime(String currentTime, int increaseWithSeconds) {
- int[] time = Arrays.stream(currentTime.split(":")).mapToInt(e -> Integer.parseInt(e)).toArray();
- int timeInSeconds = (time[0] * 3600) + (time[1] * 60) + time[2];
- timeInSeconds = timeInSeconds + increaseWithSeconds;
- int newHours = timeInSeconds / 3600;
- if (newHours > 23) {
- newHours = 0;
- }
- timeInSeconds = timeInSeconds % 3600;
- int newMinutes = timeInSeconds / 60;
- timeInSeconds = timeInSeconds % 60;
- int newSeconds = timeInSeconds;
- return String.format("%02d:%02d:%02d", newHours, newMinutes, newSeconds);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment