Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.text.DecimalFormat;
  5. import java.util.ArrayDeque;
  6. import java.util.Deque;
  7.  
  8. public class Ex11Robotics01 {
  9.     private static BufferedReader reader;
  10.     private static long startTime;
  11.     private static String[] robots;
  12.     private static int[] processTime;
  13.     private static int[] processing;
  14.     private static Deque<String> products;
  15.  
  16.     static {
  17.         reader = new BufferedReader(new InputStreamReader(System.in));
  18.         products = new ArrayDeque<>();
  19.     }
  20.  
  21.     public static void main(String[] args) throws IOException {
  22.         readInput();
  23.  
  24.         while (! products.isEmpty()) {
  25.             String product = products.removeFirst();
  26.             startTime += 1;
  27.             process();
  28.             boolean allBusy = true;
  29.  
  30.             for (int i = 0; i < processing.length; i++) {
  31.                 if (isIdle(i)) {
  32.                     allBusy = false;
  33.                     processing[i] = processTime[i];
  34.                     System.out.println(String.format("%s - %s [%s]", robots[i], product, calculateTime(startTime)));
  35.                     break;
  36.                 }
  37.             }
  38.  
  39.             if (allBusy) {
  40.                 products.addLast(product);
  41.             }
  42.         }
  43.     }
  44.  
  45.     private static boolean isIdle(int i) {
  46.         return processing[i] == 0;
  47.     }
  48.  
  49.     private static void process() {
  50.         for (int i = 0; i < processing.length; i++) {
  51.             processing[i] = processing[i] == 0 ? 0 : processing[i] - 1;
  52.         }
  53.     }
  54.  
  55.     private static String calculateTime(long seconds) {
  56.         long sec = seconds % 60;
  57.         long minutes = seconds % 3600 / 60;
  58.         long hours = seconds % 86400 / 3600;
  59.         DecimalFormat df = new DecimalFormat("00");
  60.         return String.format("%s:%s:%s", df.format(hours), df.format(minutes), df.format(sec));
  61.     }
  62.  
  63.     private static void readInput() throws IOException {
  64.         getRobots();
  65.         getStarTime();
  66.         getProducts();
  67.     }
  68.  
  69.     private static void getRobots() throws IOException {
  70.         String[] tokens = reader.readLine().split(";");
  71.         robots =  new String[tokens.length];
  72.         processing = new int[tokens.length];
  73.         processTime = new int[tokens.length];
  74.  
  75.         for (int i = 0; i < tokens.length; i++) {
  76.             String name = tokens[i].split("-")[0];
  77.             int seconds = Integer.parseInt(tokens[i].split("-")[1]);
  78.             robots[i] = name;
  79.             processTime[i] = seconds;
  80.         }
  81.  
  82.     }
  83.  
  84.     private static void getStarTime() throws IOException {
  85.         String[] tokens = reader.readLine().split(":");
  86.         int hours = Integer.parseInt(tokens[0]);
  87.         int minutes = Integer.parseInt(tokens[1]);
  88.         int seconds = Integer.parseInt(tokens[2]);
  89.         seconds += (minutes * 60);
  90.         seconds += (hours * 3600);
  91.         startTime = seconds;
  92.     }
  93.  
  94.     private static void getProducts() throws IOException {
  95.         String input;
  96.         while (! "End".equals(input = reader.readLine())) {
  97.             products.addLast(input);
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement