Fix the acquire step in a bullet pool
Your task
To avoid garbage-collection spikes, your shooter reuses bullet objects from a pool instead of creating new ones. After a few minutes of play, bullets start appearing already damaged. You model the pool with a script of commands and log each bullet's state when it is released.
An assistant wrote simulate_bullet_pool(commands, slot_count). It is in the editor. It has a bug. Find it and fix it.
Input and output:
commands: each[op, slot, amount]. Op 0 gets a bullet into firing slotslot, op 1 addsamountdamage to the bullet in that slot, op 2 releases it back to the pool.slot_count: how many firing slots exist, numbered from 0.- Return one
[bullet_id, damage]pair per release, in order.
Rules the tests check:
- Skip a command with fewer than 3 items, a slot outside
0toslot_count - 1, or an op other than 0, 1 and 2. A negativeslot_countcounts as 0. - Get: skip it if the slot already holds a bullet. Otherwise take the most recently released bullet from the pool (last in, first out). If the pool is empty, create a new bullet whose id is the number of bullets created so far, starting at 0.
- A bullet from a get always starts with 0 damage, whether it is new or reused.
- Hit: skip it if the slot is empty. Otherwise add
amountto that bullet's damage. - Release: skip it if the slot is empty. Otherwise log
[bullet_id, damage], empty the slot and put the bullet back in the pool.
Examples
simulate_bullet_pool(commands: list[list[int]], slot_count: int) -> list[list[int]]Example 1
Input
simulate_bullet_pool([[0, 0, 0], [1, 0, 3], [2, 0, 0]], 2)Output
[[0, 3]]Example 2
Input
simulate_bullet_pool([[0, 0, 0], [1, 0, 3], [2, 0, 0], [0, 0, 0], [1, 0, 1], [2, 0, 0]], 2)Output
[[0, 3], [0, 1]]Example 3
Input
simulate_bullet_pool([[0, 0, 0], [0, 1, 0], [1, 0, 2], [1, 1, 5], [2, 1, 0], [2, 0, 0]], 2)Output
[[1, 5], [0, 2]]