Baggage Carousel Laps
HardOnline Assessment
Simulate an airport baggage carousel for a very long time and report how many bags were claimed, bounced and left unclaimed.
The carousel is a ring of length cells, numbered 0 to length - 1; each cell holds at most one bag. The loading chute feeds cell 0 and the claim area is at cell pickup. Time runs in ticks t = 0, 1, 2, .... Each tick does the following, in this order:
- Move. Every bag moves from its cell
ito cell(i + 1) % length(the bag in the last cell wraps around to cell 0), and its age goes up by 1. A bag's age is 0 on the tick it is loaded. - Load. If
loads[t % len(loads)]is'1', a new bag arrives at the chute. If cell 0 is empty the bag is put there with age 0; if cell 0 is occupied the new bag is bounced (it never goes on the belt). - Claim. If
claims[t % len(claims)]is'1', a passenger is waiting: if cellpickupholds a bag, that bag is claimed and removed (the new bag from step 2 can be claimed immediately whenpickupis 0). Otherwise nothing happens. - Clean-up. Every bag whose age is at least
limitis taken off by staff and counted as unclaimed.
The carousel starts empty. Run ticks 0 to ticks - 1.
Input: length, pickup, loads, claims, limit and ticks.
Output: [claimed, bounced, unclaimed, onBelt]: the three totals above and the number of bags still on the belt after the last tick.
Example 1
- Input:
- [4,2,"1","10",3,8]
- Output:
- [3,0,2,3]
- Explanation
- A bag is loaded every tick; a passenger waits on even ticks (
claims = "10"); the pickup cell is 2 andlimitis 3. The bag loaded at tick 0 is in cell 2 at tick 2 and is claimed. At tick 4 the bag loaded at tick 2 is claimed, and the bag from tick 1 reaches age 3 and is taken off by staff; the same happens at tick 6 (the tick-6 claim takes the tick-4 bag and the tick-3 bag expires). Over 8 ticks that gives 3 claimed, 0 bounced, 2 unclaimed, and the bags loaded at ticks 5, 6 and 7 are still on the belt.
Example 2
- Input:
- [3,1,"11","0",5,7]
- Output:
- [0,3,2,2]
- Explanation
- A bag is loaded on every tick and nobody ever claims (
claims = "0"), with limit 5. Ticks 0 to 2 fill the three cells. At ticks 3, 4 and 5 the first bags have wrapped around to cell 0 (bags move every tick), so each new bag bounces (3 bounces). The bag loaded at tick 0 reaches age 5 at tick 5 and the one from tick 1 at tick 6, so 2 are unclaimed. The tick-6 bag finds cell 0 free and is loaded. After tick 6 the bags loaded at ticks 2 and 6 remain on the belt.
Example 3
- Input:
- [6,4,"1101","10",9,1000000000000]
- Output:
- [249999999999,249999999999,249999999999,3]
- Explanation
- Ten to the twelfth ticks cannot be simulated one by one. The carousel's cell contents together with the position inside the repeating
loadsandclaimspatterns (t % 4andt % 2, sot % 4overall) eventually repeat, so after a short start the totals grow by a fixed amount per cycle. After 1012 ticks the totals are 249999999999 claimed, 249999999999 bounced, 249999999999 unclaimed and 3 bags remain on the belt.
Constraints
- 2 ≤ length ≤ 12; 0 ≤ pickup < length; 1 ≤ limit ≤ 60
- loads and claims are non-empty strings of '0' and '1', each of length at most 40
- 0 ≤ ticks ≤ 1012