Advertisement
mbratanov

Untitled

Sep 12th, 2024
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. from collections import deque
  2. from datetime import datetime, timedelta
  3.  
  4.  
  5. class Robot:
  6.     def __init__(self, name, processing_time):
  7.         self.name = name
  8.         self.processing_time = processing_time
  9.         self.available_in = 0
  10.  
  11.     def start_job(self, product, current_time, assembly_line_start_time):
  12.         """
  13.        Reports when starting a new job.
  14.        """
  15.         self.available_in = current_time + self.processing_time
  16.         report_time = convert_time(assembly_line_start_time, current_time)
  17.  
  18.         print(f"{self.name} - {product} [{report_time}]")
  19.  
  20.  
  21. class AssemblyLine:
  22.     def __init__(self):
  23.         self.robots = []
  24.         self.products = deque()
  25.  
  26.     def add_robot(self, robot):
  27.         """
  28.        Adds robot to the robots list
  29.        """
  30.         self.robots.append(robot)
  31.  
  32.     def add_product(self, product):
  33.         """
  34.        Adds product to the products queue
  35.        """
  36.         self.products.append(product)
  37.  
  38.     def start(self, start_time):
  39.         """
  40.        Starts the assembly line main loop.
  41.        """
  42.         current_time = 0
  43.         while self.products:
  44.             current_time += 1
  45.  
  46.             # Check if there is an available robot.
  47.             for robot in self.robots:
  48.                 if current_time >= robot.available_in:  # If True, the robot is available
  49.                     product = self.products.popleft()  # Remove the product and assign it as a job to the robot
  50.                     robot.start_job(product, current_time, start_time)
  51.                     break
  52.             else:  # If all robots are busy then rotate the products queue
  53.                 self.products.rotate(-1)
  54.  
  55.  
  56. def convert_time(start_time, current_time):
  57.     """
  58.    Adds current time to start time and returns the result in hh:mm:ss format.
  59.    """
  60.     time_delta = timedelta(seconds=current_time)
  61.     converted_time = datetime.strptime(start_time, "%H:%M:%S") + time_delta
  62.     return converted_time.strftime("%H:%M:%S")
  63.  
  64.  
  65. def equip_robots_to_assembly_line(assembly_line, robots_stats):
  66.     for item in robots_stats:
  67.         name = item[0]
  68.         processing_time = int(item[1])
  69.         assembly_line.add_robot(Robot(name, processing_time))
  70.  
  71.  
  72. def load_products_to_assembly_line(assembly_line):
  73.     command = input()
  74.     while command != "End":
  75.         product = command
  76.         assembly_line.add_product(product)
  77.         command = input()
  78.  
  79.  
  80. def main():
  81.     robots_stats = [x.split("-") for x in input().split(";")]
  82.     start_time = input()
  83.  
  84.     assembly_line = AssemblyLine()  # Create assembly line
  85.  
  86.     equip_robots_to_assembly_line(assembly_line, robots_stats)
  87.     load_products_to_assembly_line(assembly_line)
  88.  
  89.     assembly_line.start(start_time)
  90.  
  91.  
  92. if __name__ == "__main__":
  93.     main()
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement