Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import deque
- from datetime import datetime, timedelta
- class Robot:
- def __init__(self, name, processing_time):
- self.name = name
- self.processing_time = processing_time
- self.available_in = 0
- def start_job(self, product, current_time, assembly_line_start_time):
- """
- Reports when starting a new job.
- """
- self.available_in = current_time + self.processing_time
- report_time = convert_time(assembly_line_start_time, current_time)
- print(f"{self.name} - {product} [{report_time}]")
- class AssemblyLine:
- def __init__(self):
- self.robots = []
- self.products = deque()
- def add_robot(self, robot):
- """
- Adds robot to the robots list
- """
- self.robots.append(robot)
- def add_product(self, product):
- """
- Adds product to the products queue
- """
- self.products.append(product)
- def start(self, start_time):
- """
- Starts the assembly line main loop.
- """
- current_time = 0
- while self.products:
- current_time += 1
- # Check if there is an available robot.
- for robot in self.robots:
- if current_time >= robot.available_in: # If True, the robot is available
- product = self.products.popleft() # Remove the product and assign it as a job to the robot
- robot.start_job(product, current_time, start_time)
- break
- else: # If all robots are busy then rotate the products queue
- self.products.rotate(-1)
- def convert_time(start_time, current_time):
- """
- Adds current time to start time and returns the result in hh:mm:ss format.
- """
- time_delta = timedelta(seconds=current_time)
- converted_time = datetime.strptime(start_time, "%H:%M:%S") + time_delta
- return converted_time.strftime("%H:%M:%S")
- def equip_robots_to_assembly_line(assembly_line, robots_stats):
- for item in robots_stats:
- name = item[0]
- processing_time = int(item[1])
- assembly_line.add_robot(Robot(name, processing_time))
- def load_products_to_assembly_line(assembly_line):
- command = input()
- while command != "End":
- product = command
- assembly_line.add_product(product)
- command = input()
- def main():
- robots_stats = [x.split("-") for x in input().split(";")]
- start_time = input()
- assembly_line = AssemblyLine() # Create assembly line
- equip_robots_to_assembly_line(assembly_line, robots_stats)
- load_products_to_assembly_line(assembly_line)
- assembly_line.start(start_time)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement