Atomic increment interleaving simulator
Your task
A shared counter starts at 0. N threads each perform one non-atomic increment, implemented as three steps in order: LOAD (read counter into the thread's local register), ADD (local register += 1), STORE (write the local register back to the counter). You are given a schedule: a list of [thread, step] events that is a valid interleaving of these steps (each thread's LOAD precedes its ADD precedes its STORE). Simulate the interleaving and return the FINAL value of the shared counter. Because of lost updates this can be anywhere from 1 to N.
Examples
interleaved_counter(n: int, schedule: list[list]) -> intExample 1
Input
interleaved_counter(2, [[0, "LOAD"], [1, "LOAD"], [0, "ADD"], [0, "STORE"], [1, "ADD"], [1, "STORE"]])Output
1