Archive backlog replay

Write codeMedium24 min

Your task

An archive writer keeps a backlog of blocks waiting to go to tape, and you replay one night of its operation log. Each entry in ops is either "stage|1200|vol-7", which adds a run of that many blocks belonging to that volume to the back of the backlog, or "flush|500", which writes blocks from the front of the backlog until that many have been written or the backlog runs dry. Blocks are written in the order they were staged, and one run can be split across two flushes. A stage of zero blocks adds nothing at all. The same volume may be staged several times, and a flush that writes blocks from two runs of the same volume counts that volume once. A single run can hold up to a billion blocks. Return, for each flush in order, how many distinct volumes it wrote at least one block of.

Examples

volumes_per_flush(ops: list[str]) -> list[int]
Example 1
Inputvolumes_per_flush(["stage|3|vol-a", "stage|2|vol-b", "flush|4", "flush|10"])
Output[2, 1]
Example 2
Inputvolumes_per_flush(["stage|5|vol-a", "stage|4|vol-a", "flush|7", "flush|0", "flush|9"])
Output[1, 0, 1]
Example 3
Inputvolumes_per_flush(["flush|5", "stage|0|vol-c", "flush|2", "stage|1|vol-c", "flush|2"])
Output[0, 0, 1]