Last active
May 15, 2023 20:04
-
-
Save maestroviktorin/a2d540c6a9e8dfd04f80c0716fdfae6b to your computer and use it in GitHub Desktop.
Solution to the problem #8432 from kompege.ru
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Problem No. 8432 on https://www.kompege.ru | |
| class Vehicle: | |
| ''' | |
| Kind of struct describing a vehicle. | |
| ''' | |
| def __init__(self, start: int, duration: int, _type: str): | |
| self.start = start | |
| self.end = start + duration | |
| self.type = _type | |
| with open('26_8432.txt') as file: | |
| rows = file.readlines() | |
| N = int(rows[0]) | |
| # All the vehicles are sorted in ascending of their arrival time. | |
| vehicles = sorted((Vehicle(int((r := row.split())[0]), int(r[1]), r[2]) | |
| for row in rows[1:]), key=lambda vhl: vhl.start) | |
| # 70 parking lots for autos and 30 ones for buses according to condition of the problem. | |
| auto_lots = {x: 0 for x in range(1, 71)} | |
| bus_lots = {x: 0 for x in range(1, 31)} | |
| def try_auto(vhl: Vehicle): | |
| ''' | |
| Attempt to park an auto in the auto parking lot. | |
| Returns `True` if the attempt was successful or `False` otherwise. | |
| ''' | |
| for a_lot in range(1, 71): | |
| if vhl.start >= auto_lots[a_lot]: | |
| auto_lots[a_lot] = vhl.end | |
| return True | |
| return False | |
| def try_bus(vhl: Vehicle): | |
| ''' | |
| Attempt to park either an auto or a bus in the bus parking lot. | |
| Returns `True` if the attempt was successful or `False` otherwise. | |
| ''' | |
| for b_lot in range(1, 31): | |
| if vhl.start >= bus_lots[b_lot]: | |
| bus_lots[b_lot] = vhl.end | |
| return True | |
| return False | |
| buses = 0 | |
| total = 0 | |
| for vhl in vehicles: | |
| if vhl.type == 'A': | |
| is_parked = try_auto(vhl) | |
| if not is_parked: | |
| is_parked = try_bus(vhl) | |
| else: | |
| is_parked = try_bus(vhl) | |
| buses += is_parked | |
| total += is_parked | |
| print(buses, N - total) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment