#318 · Simulation · Intermediate · 75 XP · about 16 min
A small parking lot has spaces parking spaces and, at the gate, room for line_size cars to wait in a line. The barrier keeps a log of events, each a string 'in PLATE' (a car arrives) or 'out PLATE' (a car wants to leave). Handle the events in order:
line_size cars are waiting, it joins the end of the line. Otherwise it is turned away and drives off.Write turned_away(spaces, line_size, events) that returns the list of plates that were turned away, in the order it happened. A car that is turned away twice appears twice.
events = ['in A1', 'in B2', 'in C3', 'in D4', 'out A1', 'in E5'] turned_away(2, 1, events) # returns ['D4'] turned_away(2, 0, events) # returns ['C3', 'D4']
With 2 spaces and room for 1 waiting car, A1 and B2 park, C3 waits at the gate and D4 is turned away. When A1 leaves, C3 takes its space, so E5 finds room in the line. With no room to wait, C3 and D4 are both turned away, and E5 parks in the space A1 left.
events has 0 to 30,000 items.First solves only, ranked by stars and then time.