Untitled

mail@pastecode.io avatar
unknown
python
5 months ago
2.7 kB
11
Indexable
class ConcertTicketBuyer:
    def __init__(self):
        # Store the requirements
        self.requirements = []
        # Store incoming ticket data
        self.ticket_data = {}

    def on_new_requirement(self, artist_id: int, ticket_price: int, available_seats: int):
        # Add the requirement as a tuple to the list of requirements
        self.requirements.append((artist_id, ticket_price, available_seats))

    def process_data(self, message_id: int, data: int):
        if message_id == 0:
            # No data to process, return 0
            return 0

        if message_id not in self.ticket_data:
            # Initialize an entry for the message ID
            self.ticket_data[message_id] = [None, None, None]

        # Determine which part of the packet this is (artist ID, ticket price, available seats)
        current_packet = self.ticket_data[message_id]
        if current_packet[0] is None:
            current_packet[0] = data  # artist_id
        elif current_packet[1] is None:
            current_packet[1] = data  # ticket_price
        else:
            current_packet[2] = data  # available_seats

        # Check if we have all parts of the ticket information
        if all(part is not None for part in current_packet):
            artist_id, ticket_price, available_seats = current_packet
            for req_artist_id, req_ticket_price, req_available_seats in self.requirements:
                if (artist_id == req_artist_id and
                        ticket_price <= req_ticket_price and
                        available_seats >= req_available_seats):
                    # Requirement matches, send an order
                    self.requirements.remove((req_artist_id, req_ticket_price, req_available_seats))
                    return message_id  # First part of the order (original message ID)

        return 0

if __name__ == "__main__":
    import sys

    read_line = lambda: sys.stdin.readline().split()

    first_line = read_line()
    num_queries = int(first_line[0])

    engine = ConcertTicketBuyer()

    for _ in range(num_queries):
        line = read_line()
        if line[0] == 'REQUIREMENT':
            artist_id = int(line[1])
            ticket_price = int(line[2])
            available_seats = int(line[3])
            engine.on_new_requirement(artist_id, ticket_price, available_seats)
        elif line[0] == "DATA":
            message_id = int(line[1])
            data = int(line[2])
            received = engine.process_data(message_id, data)
            print(received)
        else:
            print("Malformed input!")
            exit(-1)
Leave a Comment