broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    lim = x['initial'] * 1000
    lo, hi = x['min'] * 1000, x['max'] * 1000
    cool = 0
    out = []
    for ev in x['events']:
        if ev == 'ok':
            lim = min(hi, lim + 1000000 // lim)
            pass
        elif cool == 0:
            lim = max(lo, lim // 2)
            cool = x['cooldown']
        else:
            cool -= 1
        out.append(lim // 1000)
    return [out, lim]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5], 5861]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2], 2500]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4], 4595]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4], 4250]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'timeout',
               'ok',
               'timeout',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'timeout',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], 2500]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6], 6031]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2], 2900]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4], 4812]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4], 4485]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'ok',
               'ok',
               'ok',
               'timeout',
               'drop',
               'timeout',
               'drop',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'drop',
               'ok',
               'ok'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 3], 3244]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4], 4110]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6], 6196]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3], 3244]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5], 5019]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4], 4707]],
  ['random signals',
   {'cooldown': 2,
    'events': ['ok',
               'drop',
               'ok',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[6, 3, 3, 3, 3, 4, 2, 2, 2, 3, 3, 3, 4, 4, 4, 2], 2305]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6], 6357]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3, 3], 3552]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5, 5], 5218]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 4], 4919]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 2], 2496]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4], 4110]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6], 6514]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3, 3, 3], 3833]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5, 5, 5], 5409]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 4, 5], 5122]],
  ['random signals',
   {'cooldown': 2,
    'events': ['ok',
               'timeout',
               'timeout',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[6, 3, 3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 2, 2, 2, 2], 2900]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]]]
for label, args, expected in cases[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    lim = x['initial'] * 1000
    lo, hi = x['min'] * 1000, x['max'] * 1000
    cool = 0
    out = []
    for ev in x['events']:
        if ev == 'ok':
            lim = min(hi, lim + 1000000 // lim)
            cool = 0
        elif cool == 0:
            lim = max(lo, lim // 2)
            cool = x['cooldown']
        else:
            cool -= 1
        out.append(lim // 1000)
    return [out, lim]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5], 5861]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2], 2500]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4], 4595]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4], 4250]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'timeout',
               'ok',
               'timeout',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'timeout',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], 2500]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6], 6031]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2], 2900]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4], 4812]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4], 4485]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'ok',
               'ok',
               'ok',
               'timeout',
               'drop',
               'timeout',
               'drop',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'drop',
               'ok',
               'ok'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 3], 3244]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4], 4110]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6], 6196]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3], 3244]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5], 5019]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4], 4707]],
  ['random signals',
   {'cooldown': 2,
    'events': ['ok',
               'drop',
               'ok',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[6, 3, 3, 3, 3, 4, 2, 2, 2, 3, 3, 3, 4, 4, 4, 2], 2305]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6], 6357]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3, 3], 3552]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5, 5], 5218]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 4], 4919]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 2], 2496]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4], 4110]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6], 6514]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3, 3, 3], 3833]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5, 5, 5], 5409]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 4, 5], 5122]],
  ['random signals',
   {'cooldown': 2,
    'events': ['ok',
               'timeout',
               'timeout',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[6, 3, 3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 2, 2, 2, 2], 2900]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]]]
for label, args, expected in cases[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
After one loss the limiter ignores later losses until more losses arrive, reacting too slowly.

Input {initial, min, max, cooldown, events}. The limit is kept in fixed-point thousandths (integer). "ok" adds floor(10^6 / limit_thousandths), i.e. 1/limit, capped at max, and counts down an active cooldown. "drop" or "timeout" halves the limit (floor), floored at min, and starts a cooldown of the given number of events when no cooldown is active; during a cooldown a loss only counts it down. After each event report floor(limit). Return [reported whole limits, final limit in thousandths].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    lim = x['initial'] * 1000
    lo, hi = x['min'] * 1000, x['max'] * 1000
    cool = 0
    out = []
    for ev in x['events']:
        if ev == 'ok':
            lim = min(hi, lim + 1000000 // lim)
            if cool:
                cool -= 1
        elif cool == 0:
            lim = max(lo, lim // 2)
            cool = x['cooldown']
        else:
            pass
        out.append(lim // 1000)
    return [out, lim]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5], 5861]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2], 2500]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4], 4595]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4], 4250]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'timeout',
               'ok',
               'timeout',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'timeout',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], 2500]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6], 6031]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2], 2900]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4], 4812]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4], 4485]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'ok',
               'ok',
               'ok',
               'timeout',
               'drop',
               'timeout',
               'drop',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'drop',
               'ok',
               'ok'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 3], 3244]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4], 4110]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6], 6196]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3], 3244]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5], 5019]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4], 4707]],
  ['random signals',
   {'cooldown': 2,
    'events': ['ok',
               'drop',
               'ok',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[6, 3, 3, 3, 3, 4, 2, 2, 2, 3, 3, 3, 4, 4, 4, 2], 2305]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6], 6357]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3, 3], 3552]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5, 5], 5218]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 4], 4919]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 2], 2496]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4], 4110]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6], 6514]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3, 3, 3], 3833]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5, 5, 5], 5409]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 4, 5], 5122]],
  ['random signals',
   {'cooldown': 2,
    'events': ['ok',
               'timeout',
               'timeout',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[6, 3, 3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 2, 2, 2, 2], 2900]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]]]
for label, args, expected in cases[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    lim = x['initial'] * 1000
    lo, hi = x['min'] * 1000, x['max'] * 1000
    cool = 0
    out = []
    for ev in x['events']:
        if ev == 'ok':
            lim = min(hi, lim + 1000000 // lim)
            if cool:
                cool -= 1
        elif cool == 0:
            lim = max(lo, lim // 2)
            cool = x['cooldown']
        else:
            cool = x['cooldown']
        out.append(lim // 1000)
    return [out, lim]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5], 5861]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2], 2500]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4], 4595]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4], 4250]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'timeout',
               'ok',
               'timeout',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'timeout',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], 2500]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6], 6031]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2], 2900]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4], 4812]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4], 4485]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'ok',
               'ok',
               'ok',
               'timeout',
               'drop',
               'timeout',
               'drop',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'drop',
               'ok',
               'ok'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 3], 3244]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4], 4110]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6], 6196]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3], 3244]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5], 5019]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4], 4707]],
  ['random signals',
   {'cooldown': 2,
    'events': ['ok',
               'drop',
               'ok',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[6, 3, 3, 3, 3, 4, 2, 2, 2, 3, 3, 3, 4, 4, 4, 2], 2305]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6], 6357]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3, 3], 3552]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5, 5], 5218]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 4], 4919]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 2], 2496]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4], 4110]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6], 6514]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3, 3, 3], 3833]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5, 5, 5], 5409]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 4, 5], 5122]],
  ['random signals',
   {'cooldown': 2,
    'events': ['ok',
               'timeout',
               'timeout',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[6, 3, 3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 2, 2, 2, 2], 2900]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]]]
for label, args, expected in cases[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A sustained burst of losses keeps the limiter in cooldown and prevents any further decrease.

Input {initial, min, max, cooldown, events}. The limit is kept in fixed-point thousandths (integer). "ok" adds floor(10^6 / limit_thousandths), i.e. 1/limit, capped at max, and counts down an active cooldown. "drop" or "timeout" halves the limit (floor), floored at min, and starts a cooldown of the given number of events when no cooldown is active; during a cooldown a loss only counts it down. After each event report floor(limit). Return [reported whole limits, final limit in thousandths].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    lim = x['initial'] * 1000
    lo, hi = x['min'] * 1000, x['max'] * 1000
    cool = 0
    out = []
    for ev in x['events']:
        if ev == 'ok':
            lim = min(hi, lim + 1000000 // lim)
            if cool:
                cool -= 1
        elif cool == 0:
            lim = max(lo, lim // 2)
            cool = x['cooldown']
        else:
            cool -= 1
        out.append(round(lim / 1000))
    return [out, lim]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5], 5861]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2], 2500]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4], 4595]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4], 4250]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'timeout',
               'ok',
               'timeout',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'timeout',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], 2500]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6], 6031]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2], 2900]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4], 4812]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4], 4485]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'ok',
               'ok',
               'ok',
               'timeout',
               'drop',
               'timeout',
               'drop',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'drop',
               'ok',
               'ok'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 3], 3244]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4], 4110]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6], 6196]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3], 3244]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5], 5019]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4], 4707]],
  ['random signals',
   {'cooldown': 2,
    'events': ['ok',
               'drop',
               'ok',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[6, 3, 3, 3, 3, 4, 2, 2, 2, 3, 3, 3, 4, 4, 4, 2], 2305]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6], 6357]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3, 3], 3552]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5, 5], 5218]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 4], 4919]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 2], 2496]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4], 4110]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6], 6514]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3, 3, 3], 3833]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5, 5, 5], 5409]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 4, 5], 5122]],
  ['random signals',
   {'cooldown': 2,
    'events': ['ok',
               'timeout',
               'timeout',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[6, 3, 3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 2, 2, 2, 2], 2900]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]]]
for label, args, expected in cases[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    lim = x['initial'] * 1000
    lo, hi = x['min'] * 1000, x['max'] * 1000
    cool = 0
    out = []
    for ev in x['events']:
        if ev == 'ok':
            lim = min(hi, lim + 1000000 // lim)
            if cool:
                cool -= 1
        elif cool == 0:
            lim = max(lo, lim // 2)
            cool = x['cooldown']
        else:
            cool -= 1
        out.append(-(-lim // 1000))
    return [out, lim]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5], 5861]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2], 2500]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4], 4595]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4], 4250]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'timeout',
               'ok',
               'timeout',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'timeout',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], 2500]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6], 6031]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2], 2900]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4], 4812]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4], 4485]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'ok',
               'ok',
               'ok',
               'timeout',
               'drop',
               'timeout',
               'drop',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'drop',
               'ok',
               'ok'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 3], 3244]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4], 4110]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6], 6196]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3], 3244]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5], 5019]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4], 4707]],
  ['random signals',
   {'cooldown': 2,
    'events': ['ok',
               'drop',
               'ok',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[6, 3, 3, 3, 3, 4, 2, 2, 2, 3, 3, 3, 4, 4, 4, 2], 2305]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6], 6357]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3, 3], 3552]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5, 5], 5218]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 4], 4919]],
  ['random signals',
   {'cooldown': 2,
    'events': ['timeout',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 2], 2496]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4], 4110]]],
 [['additive growth',
   {'cooldown': 2,
    'events': ['ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 4,
    'max': 20,
    'min': 1},
   [[4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6], 6514]],
  ['halving to floor',
   {'cooldown': 0,
    'events': ['drop', 'drop', 'timeout', 'drop', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 5,
    'max': 20,
    'min': 2},
   [[2, 2, 2, 2, 2, 2, 3, 3, 3], 3833]],
  ['ceiling',
   {'cooldown': 1,
    'events': ['ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok'],
    'initial': 9,
    'max': 10,
    'min': 1},
   [[9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10], 10000]],
  ['cooldown counts successes',
   {'cooldown': 2,
    'events': ['drop', 'ok', 'ok', 'drop', 'ok', 'drop', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 5, 5, 5], 5409]],
  ['cooldown counts losses',
   {'cooldown': 2,
    'events': ['drop', 'drop', 'drop', 'drop', 'timeout', 'ok', 'ok', 'ok', 'ok', 'ok'],
    'initial': 16,
    'max': 32,
    'min': 1},
   [[8, 8, 8, 4, 4, 4, 4, 4, 4, 5], 5122]],
  ['random signals',
   {'cooldown': 2,
    'events': ['ok',
               'timeout',
               'timeout',
               'ok',
               'ok',
               'ok',
               'timeout',
               'ok',
               'ok',
               'ok',
               'ok',
               'ok',
               'drop',
               'ok',
               'timeout',
               'ok'],
    'initial': 6,
    'max': 12,
    'min': 2},
   [[6, 3, 3, 3, 3, 3, 2, 2, 2, 3, 3, 3, 2, 2, 2, 2], 2900]],
  ['odd limit halves',
   {'cooldown': 0, 'events': ['ok', 'drop', 'ok', 'ok', 'drop'], 'initial': 7, 'max': 10, 'min': 1},
   [[7, 3, 3, 4, 2], 2055]]]]
for label, args, expected in cases[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The advertised concurrency exceeds the fractional limit.

Input {initial, min, max, cooldown, events}. The limit is kept in fixed-point thousandths (integer). "ok" adds floor(10^6 / limit_thousandths), i.e. 1/limit, capped at max, and counts down an active cooldown. "drop" or "timeout" halves the limit (floor), floored at min, and starts a cooldown of the given number of events when no cooldown is active; during a cooldown a loss only counts it down. After each event report floor(limit). Return [reported whole limits, final limit in thousandths].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    cap = x['max_concurrent']
    D = x['lease_ms']
    held = {}
    out = []
    for t, op, ident in x['events']:
        for k in [k for k, s in held.items() if s + D < t]:
            del held[k]
        if op == 'acquire':
            if ident in held:
                out.append('duplicate')
            elif len(held) < cap:
                held[ident] = t
                out.append('granted')
            else:
                out.append('rejected')
        else:
            if ident in held:
                del held[ident]
                out.append('released')
            else:
                out.append('unknown')
    return [out, sorted(held)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[['lease expires exactly',
   {'events': [[0, 'acquire', 'a'],
               [999, 'acquire', 'b'],
               [1000, 'acquire', 'b'],
               [1501, 'release', 'b']],
    'lease_ms': 1000,
    'max_concurrent': 1},
   [['granted', 'rejected', 'granted', 'released'], []]],
  ['unknown release',
   {'events': [[0, 'acquire', 'a'],
               [10, 'acquire', 'b'],
               [20, 'release', 'zz'],
               [30, 'acquire', 'c'],
               [41, 'release', 'a']],
    'lease_ms': 5000,
    'max_concurrent': 2},
   [['granted', 'granted', 'unknown', 'rejected', 'released'], ['b']]],
  ['duplicate acquire',
   {'events': [[0, 'acquire', 'a'],
               [800, 'acquire', 'a'],
               [1001, 'acquire', 'b'],
               [1100, 'acquire', 'c']],
    'lease_ms': 1000,
    'max_concurrent': 2},
   [['granted', 'duplicate', 'granted', 'granted'], ['b', 'c']]],
  ['release after expiry',
   {'events': [[0, 'acquire', 'a'], [301, 'release', 'a'], [302, 'acquire', 'b']],
    'lease_ms': 300,
    'max_concurrent': 1},
   [['granted', 'unknown', 'granted'], ['b']]],
  ['random leases',
   {'events': [[8, 'acquire', 'b'],
               [172, 'acquire', 'c'],
               [317, 'acquire', 'd'],
               [462, 'acquire', 'a'],
               [606, 'acquire', 'c'],
               [769, 'acquire', 'd'],
               [900, 'acquire', 'a'],
               [1064, 'release', 'a'],
               [1238, 'release', 'b'],
               [1372, 'acquire', 'c'],
               [1516, 'acquire', 'c'],
               [1668, 'acquire', 'd']],
    'lease_ms': 600,
    'max_concurrent': 2},
   [['granted',
     'granted',
     'rejected',
     'rejected',
     'duplicate',
     'granted',
     'granted',
     'released',
     'unknown',
     'granted',
     'duplicate',
     'granted'],
    ['c', 'd']]],
  ['full pool',
   {'events': [[0, 'acquire', 'a'],
               [1, 'acquire', 'b'],
               [2, 'acquire', 'c'],
               [3, 'release', 'a'],
               [5, 'acquire', 'c']],
    'lease_ms': 10000,
    'max_concurrent': 2},
   [['granted', 'granted', 'rejected', 'released', 'granted'], ['b', 'c']]]],
 [['lease expires exactly',
   {'events': [[0, 'acquire', 'a'],
               [999, 'acquire', 'b'],
               [1000, 'acquire', 'b'],
               [1502, 'release', 'b']],
    'lease_ms': 1000,
    'max_concurrent': 1},
   [['granted', 'rejected', 'granted', 'released'], []]],
  ['unknown release',
   {'events': [[0, 'acquire', 'a'],
               [10, 'acquire', 'b'],
               [20, 'release', 'zz'],
               [30, 'acquire', 'c'],
               [42, 'release', 'a']],
    'lease_ms': 5000,
    'max_concurrent': 2},
   [['granted', 'granted', 'unknown', 'rejected', 'released'], ['b']]],
  ['duplicate acquire',
   {'events': [[0, 'acquire', 'a'],
               [800, 'acquire', 'a'],
               [1002, 'acquire', 'b'],
               [1100, 'acquire', 'c']],
    'lease_ms': 1000,
    'max_concurrent': 2},
   [['granted', 'duplicate', 'granted', 'granted'], ['b', 'c']]],
  ['release after expiry',
   {'events': [[0, 'acquire', 'a'], [302, 'release', 'a'], [303, 'acquire', 'b']],
    'lease_ms': 300,
    'max_concurrent': 1},
   [['granted', 'unknown', 'granted'], ['b']]],
  ['random leases',
   {'events': [[20, 'acquire', 'a'],
               [161, 'acquire', 'a'],
               [304, 'release', 'b'],
               [482, 'acquire', 'c'],
               [635, 'acquire', 'b'],
               [780, 'acquire', 'd'],
               [922, 'release', 'c'],
               [1055, 'acquire', 'a'],
               [1209, 'release', 'b'],
               [1366, 'release', 'a'],
               [1534, 'acquire', 'a'],
               [1689, 'acquire', 'b']],
    'lease_ms': 600,
    'max_concurrent': 2},
   [['granted',
     'duplicate',
     'unknown',
     'granted',
     'granted',
     'rejected',
     'released',
     'granted',
     'released',
     'released',
     'granted',
     'granted'],
    ['a', 'b']]],
  ['full pool',
   {'events': [[0, 'acquire', 'a'],
               [1, 'acquire', 'b'],
               [2, 'acquire', 'c'],
               [3, 'release', 'a'],
               [6, 'acquire', 'c']],
    'lease_ms': 10000,
    'max_concurrent': 2},
   [['granted', 'granted', 'rejected', 'released', 'granted'], ['b', 'c']]]],
 [['lease expires exactly',
   {'events': [[0, 'acquire', 'a'],
               [999, 'acquire', 'b'],
               [1000, 'acquire', 'b'],
               [1503, 'release', 'b']],
    'lease_ms': 1000,
    'max_concurrent': 1},
   [['granted', 'rejected', 'granted', 'released'], []]],
  ['unknown release',
   {'events': [[0, 'acquire', 'a'],
               [10, 'acquire', 'b'],
               [20, 'release', 'zz'],
               [30, 'acquire', 'c'],
               [43, 'release', 'a']],
    'lease_ms': 5000,
    'max_concurrent': 2},
   [['granted', 'granted', 'unknown', 'rejected', 'released'], ['b']]],
  ['duplicate acquire',
   {'events': [[0, 'acquire', 'a'],
               [800, 'acquire', 'a'],
               [1003, 'acquire', 'b'],
               [1100, 'acquire', 'c']],
    'lease_ms': 1000,
    'max_concurrent': 2},
   [['granted', 'duplicate', 'granted', 'granted'], ['b', 'c']]],
  ['release after expiry',
   {'events': [[0, 'acquire', 'a'], [303, 'release', 'a'], [304, 'acquire', 'b']],
    'lease_ms': 300,
    'max_concurrent': 1},
   [['granted', 'unknown', 'granted'], ['b']]],
  ['random leases',
   {'events': [[7, 'acquire', 'a'],
               [186, 'release', 'a'],
               [319, 'acquire', 'a'],
               [456, 'acquire', 'c'],
               [610, 'acquire', 'a'],
               [778, 'acquire', 'd'],
               [935, 'acquire', 'c'],
               [1055, 'acquire', 'a'],
               [1219, 'release', 'a'],
               [1368, 'acquire', 'b'],
               [1500, 'acquire', 'a'],
               [1677, 'acquire', 'b']],
    'lease_ms': 600,
    'max_concurrent': 2},
   [['granted',
     'released',
     'granted',
     'granted',
     'duplicate',
     'rejected',
     'duplicate',
     'granted',
     'released',
     'granted',
     'granted',
     'duplicate'],
    ['a', 'b']]],
  ['full pool',
   {'events': [[0, 'acquire', 'a'],
               [1, 'acquire', 'b'],
               [2, 'acquire', 'c'],
               [3, 'release', 'a'],
               [7, 'acquire', 'c']],
    'lease_ms': 10000,
    'max_concurrent': 2},
   [['granted', 'granted', 'rejected', 'released', 'granted'], ['b', 'c']]]],
 [['lease expires exactly',
   {'events': [[0, 'acquire', 'a'],
               [999, 'acquire', 'b'],
               [1000, 'acquire', 'b'],
               [1504, 'release', 'b']],
    'lease_ms': 1000,
    'max_concurrent': 1},
   [['granted', 'rejected', 'granted', 'released'], []]],
  ['unknown release',
   {'events': [[0, 'acquire', 'a'],
               [10, 'acquire', 'b'],
               [20, 'release', 'zz'],
               [30, 'acquire', 'c'],
               [44, 'release', 'a']],
    'lease_ms': 5000,
    'max_concurrent': 2},
   [['granted', 'granted', 'unknown', 'rejected', 'released'], ['b']]],
  ['duplicate acquire',
   {'events': [[0, 'acquire', 'a'],
               [800, 'acquire', 'a'],
               [1004, 'acquire', 'b'],
               [1100, 'acquire', 'c']],
    'lease_ms': 1000,
    'max_concurrent': 2},
   [['granted', 'duplicate', 'granted', 'granted'], ['b', 'c']]],
  ['release after expiry',
   {'events': [[0, 'acquire', 'a'], [304, 'release', 'a'], [305, 'acquire', 'b']],
    'lease_ms': 300,
    'max_concurrent': 1},
   [['granted', 'unknown', 'granted'], ['b']]],
  ['random leases',
   {'events': [[9, 'acquire', 'c'],
               [172, 'acquire', 'd'],
               [315, 'acquire', 'a'],
               [473, 'acquire', 'a'],
               [611, 'acquire', 'd'],
               [785, 'acquire', 'c'],
               [926, 'release', 'd'],
               [1088, 'release', 'd'],
               [1239, 'acquire', 'a'],
               [1373, 'release', 'b'],
               [1536, 'release', 'a'],
               [1672, 'acquire', 'a']],
    'lease_ms': 600,
    'max_concurrent': 2},
   [['granted',
     'granted',
     'rejected',
     'rejected',
     'duplicate',
     'granted',
     'unknown',
     'unknown',
     'granted',
     'unknown',
     'released',
     'granted'],
    ['a']]],
  ['full pool',
   {'events': [[0, 'acquire', 'a'],
               [1, 'acquire', 'b'],
               [2, 'acquire', 'c'],
               [3, 'release', 'a'],
               [8, 'acquire', 'c']],
    'lease_ms': 10000,
    'max_concurrent': 2},
   [['granted', 'granted', 'rejected', 'released', 'granted'], ['b', 'c']]]],
 [['lease expires exactly',
   {'events': [[0, 'acquire', 'a'],
               [999, 'acquire', 'b'],
               [1000, 'acquire', 'b'],
               [1505, 'release', 'b']],
    'lease_ms': 1000,
    'max_concurrent': 1},
   [['granted', 'rejected', 'granted', 'released'], []]],
  ['unknown release',
   {'events': [[0, 'acquire', 'a'],
               [10, 'acquire', 'b'],
               [20, 'release', 'zz'],
               [30, 'acquire', 'c'],
               [45, 'release', 'a']],
    'lease_ms': 5000,
    'max_concurrent': 2},
   [['granted', 'granted', 'unknown', 'rejected', 'released'], ['b']]],
  ['duplicate acquire',
   {'events': [[0, 'acquire', 'a'],
               [800, 'acquire', 'a'],
               [1005, 'acquire', 'b'],
               [1100, 'acquire', 'c']],
    'lease_ms': 1000,
    'max_concurrent': 2},
   [['granted', 'duplicate', 'granted', 'granted'], ['b', 'c']]],
  ['release after expiry',
   {'events': [[0, 'acquire', 'a'], [305, 'release', 'a'], [306, 'acquire', 'b']],
    'lease_ms': 300,
    'max_concurrent': 1},
   [['granted', 'unknown', 'granted'], ['b']]],
  ['random leases',
   {'events': [[14, 'acquire', 'b'],
               [177, 'release', 'a'],
               [334, 'release', 'd'],
               [476, 'acquire', 'a'],
               [631, 'release', 'a'],
               [764, 'acquire', 'b'],
               [901, 'acquire', 'a'],
               [1084, 'release', 'b'],
               [1200, 'acquire', 'b'],
               [1376, 'release', 'd'],
               [1506, 'release', 'a'],
               [1680, 'acquire', 'a']],
    'lease_ms': 600,
    'max_concurrent': 2},
   [['granted',
     'unknown',
     'unknown',
     'granted',
     'released',
     'granted',
     'granted',
     'released',
     'granted',
     'unknown',
     'unknown',
     'granted'],
    ['a', 'b']]],
  ['full pool',
   {'events': [[0, 'acquire', 'a'],
               [1, 'acquire', 'b'],
               [2, 'acquire', 'c'],
               [3, 'release', 'a'],
               [9, 'acquire', 'c']],
    'lease_ms': 10000,
    'max_concurrent': 2},
   [['granted', 'granted', 'rejected', 'released', 'granted'], ['b', 'c']]]]]
for label, args, expected in cases[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    cap = x['max_concurrent']
    D = x['lease_ms']
    held = {}
    out = []
    for t, op, ident in x['events']:
        for k in [k for k, s in held.items() if s + D * 1000 <= t]:
            del held[k]
        if op == 'acquire':
            if ident in held:
                out.append('duplicate')
            elif len(held) < cap:
                held[ident] = t
                out.append('granted')
            else:
                out.append('rejected')
        else:
            if ident in held:
                del held[ident]
                out.append('released')
            else:
                out.append('unknown')
    return [out, sorted(held)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[['lease expires exactly',
   {'events': [[0, 'acquire', 'a'],
               [999, 'acquire', 'b'],
               [1000, 'acquire', 'b'],
               [1501, 'release', 'b']],
    'lease_ms': 1000,
    'max_concurrent': 1},
   [['granted', 'rejected', 'granted', 'released'], []]],
  ['unknown release',
   {'events': [[0, 'acquire', 'a'],
               [10, 'acquire', 'b'],
               [20, 'release', 'zz'],
               [30, 'acquire', 'c'],
               [41, 'release', 'a']],
    'lease_ms': 5000,
    'max_concurrent': 2},
   [['granted', 'granted', 'unknown', 'rejected', 'released'], ['b']]],
  ['duplicate acquire',
   {'events': [[0, 'acquire', 'a'],
               [800, 'acquire', 'a'],
               [1001, 'acquire', 'b'],
               [1100, 'acquire', 'c']],
    'lease_ms': 1000,
    'max_concurrent': 2},
   [['granted', 'duplicate', 'granted', 'granted'], ['b', 'c']]],
  ['release after expiry',
   {'events': [[0, 'acquire', 'a'], [301, 'release', 'a'], [302, 'acquire', 'b']],
    'lease_ms': 300,
    'max_concurrent': 1},
   [['granted', 'unknown', 'granted'], ['b']]],
  ['random leases',
   {'events': [[8, 'acquire', 'b'],
               [172, 'acquire', 'c'],
               [317, 'acquire', 'd'],
               [462, 'acquire', 'a'],
               [606, 'acquire', 'c'],
               [769, 'acquire', 'd'],
               [900, 'acquire', 'a'],
               [1064, 'release', 'a'],
               [1238, 'release', 'b'],
               [1372, 'acquire', 'c'],
               [1516, 'acquire', 'c'],
               [1668, 'acquire', 'd']],
    'lease_ms': 600,
    'max_concurrent': 2},
   [['granted',
     'granted',
     'rejected',
     'rejected',
     'duplicate',
     'granted',
     'granted',
     'released',
     'unknown',
     'granted',
     'duplicate',
     'granted'],
    ['c', 'd']]],
  ['full pool',
   {'events': [[0, 'acquire', 'a'],
               [1, 'acquire', 'b'],
               [2, 'acquire', 'c'],
               [3, 'release', 'a'],
               [5, 'acquire', 'c']],
    'lease_ms': 10000,
    'max_concurrent': 2},
   [['granted', 'granted', 'rejected', 'released', 'granted'], ['b', 'c']]]],
 [['lease expires exactly',
   {'events': [[0, 'acquire', 'a'],
               [999, 'acquire', 'b'],
               [1000, 'acquire', 'b'],
               [1502, 'release', 'b']],
    'lease_ms': 1000,
    'max_concurrent': 1},
   [['granted', 'rejected', 'granted', 'released'], []]],
  ['unknown release',
   {'events': [[0, 'acquire', 'a'],
               [10, 'acquire', 'b'],
               [20, 'release', 'zz'],
               [30, 'acquire', 'c'],
               [42, 'release', 'a']],
    'lease_ms': 5000,
    'max_concurrent': 2},
   [['granted', 'granted', 'unknown', 'rejected', 'released'], ['b']]],
  ['duplicate acquire',
   {'events': [[0, 'acquire', 'a'],
               [800, 'acquire', 'a'],
               [1002, 'acquire', 'b'],
               [1100, 'acquire', 'c']],
    'lease_ms': 1000,
    'max_concurrent': 2},
   [['granted', 'duplicate', 'granted', 'granted'], ['b', 'c']]],
  ['release after expiry',
   {'events': [[0, 'acquire', 'a'], [302, 'release', 'a'], [303, 'acquire', 'b']],
    'lease_ms': 300,
    'max_concurrent': 1},
   [['granted', 'unknown', 'granted'], ['b']]],
  ['random leases',
   {'events': [[20, 'acquire', 'a'],
               [161, 'acquire', 'a'],
               [304, 'release', 'b'],
               [482, 'acquire', 'c'],
               [635, 'acquire', 'b'],
               [780, 'acquire', 'd'],
               [922, 'release', 'c'],
               [1055, 'acquire', 'a'],
               [1209, 'release', 'b'],
               [1366, 'release', 'a'],
               [1534, 'acquire', 'a'],
               [1689, 'acquire', 'b']],
    'lease_ms': 600,
    'max_concurrent': 2},
   [['granted',
     'duplicate',
     'unknown',
     'granted',
     'granted',
     'rejected',
     'released',
     'granted',
     'released',
     'released',
     'granted',
     'granted'],
    ['a', 'b']]],
  ['full pool',
   {'events': [[0, 'acquire', 'a'],
               [1, 'acquire', 'b'],
               [2, 'acquire', 'c'],
               [3, 'release', 'a'],
               [6, 'acquire', 'c']],
    'lease_ms': 10000,
    'max_concurrent': 2},
   [['granted', 'granted', 'rejected', 'released', 'granted'], ['b', 'c']]]],
 [['lease expires exactly',
   {'events': [[0, 'acquire', 'a'],
               [999, 'acquire', 'b'],
               [1000, 'acquire', 'b'],
               [1503, 'release', 'b']],
    'lease_ms': 1000,
    'max_concurrent': 1},
   [['granted', 'rejected', 'granted', 'released'], []]],
  ['unknown release',
   {'events': [[0, 'acquire', 'a'],
               [10, 'acquire', 'b'],
               [20, 'release', 'zz'],
               [30, 'acquire', 'c'],
               [43, 'release', 'a']],
    'lease_ms': 5000,
    'max_concurrent': 2},
   [['granted', 'granted', 'unknown', 'rejected', 'released'], ['b']]],
  ['duplicate acquire',
   {'events': [[0, 'acquire', 'a'],
               [800, 'acquire', 'a'],
               [1003, 'acquire', 'b'],
               [1100, 'acquire', 'c']],
    'lease_ms': 1000,
    'max_concurrent': 2},
   [['granted', 'duplicate', 'granted', 'granted'], ['b', 'c']]],
  ['release after expiry',
   {'events': [[0, 'acquire', 'a'], [303, 'release', 'a'], [304, 'acquire', 'b']],
    'lease_ms': 300,
    'max_concurrent': 1},
   [['granted', 'unknown', 'granted'], ['b']]],
  ['random leases',
   {'events': [[7, 'acquire', 'a'],
               [186, 'release', 'a'],
               [319, 'acquire', 'a'],
               [456, 'acquire', 'c'],
               [610, 'acquire', 'a'],
               [778, 'acquire', 'd'],
               [935, 'acquire', 'c'],
               [1055, 'acquire', 'a'],
               [1219, 'release', 'a'],
               [1368, 'acquire', 'b'],
               [1500, 'acquire', 'a'],
               [1677, 'acquire', 'b']],
    'lease_ms': 600,
    'max_concurrent': 2},
   [['granted',
     'released',
     'granted',
     'granted',
     'duplicate',
     'rejected',
     'duplicate',
     'granted',
     'released',
     'granted',
     'granted',
     'duplicate'],
    ['a', 'b']]],
  ['full pool',
   {'events': [[0, 'acquire', 'a'],
               [1, 'acquire', 'b'],
               [2, 'acquire', 'c'],
               [3, 'release', 'a'],
               [7, 'acquire', 'c']],
    'lease_ms': 10000,
    'max_concurrent': 2},
   [['granted', 'granted', 'rejected', 'released', 'granted'], ['b', 'c']]]],
 [['lease expires exactly',
   {'events': [[0, 'acquire', 'a'],
               [999, 'acquire', 'b'],
               [1000, 'acquire', 'b'],
               [1504, 'release', 'b']],
    'lease_ms': 1000,
    'max_concurrent': 1},
   [['granted', 'rejected', 'granted', 'released'], []]],
  ['unknown release',
   {'events': [[0, 'acquire', 'a'],
               [10, 'acquire', 'b'],
               [20, 'release', 'zz'],
               [30, 'acquire', 'c'],
               [44, 'release', 'a']],
    'lease_ms': 5000,
    'max_concurrent': 2},
   [['granted', 'granted', 'unknown', 'rejected', 'released'], ['b']]],
  ['duplicate acquire',
   {'events': [[0, 'acquire', 'a'],
               [800, 'acquire', 'a'],
               [1004, 'acquire', 'b'],
               [1100, 'acquire', 'c']],
    'lease_ms': 1000,
    'max_concurrent': 2},
   [['granted', 'duplicate', 'granted', 'granted'], ['b', 'c']]],
  ['release after expiry',
   {'events': [[0, 'acquire', 'a'], [304, 'release', 'a'], [305, 'acquire', 'b']],
    'lease_ms': 300,
    'max_concurrent': 1},
   [['granted', 'unknown', 'granted'], ['b']]],
  ['random leases',
   {'events': [[9, 'acquire', 'c'],
               [172, 'acquire', 'd'],
               [315, 'acquire', 'a'],
               [473, 'acquire', 'a'],
               [611, 'acquire', 'd'],
               [785, 'acquire', 'c'],
               [926, 'release', 'd'],
               [1088, 'release', 'd'],
               [1239, 'acquire', 'a'],
               [1373, 'release', 'b'],
               [1536, 'release', 'a'],
               [1672, 'acquire', 'a']],
    'lease_ms': 600,
    'max_concurrent': 2},
   [['granted',
     'granted',
     'rejected',
     'rejected',
     'duplicate',
     'granted',
     'unknown',
     'unknown',
     'granted',
     'unknown',
     'released',
     'granted'],
    ['a']]],
  ['full pool',
   {'events': [[0, 'acquire', 'a'],
               [1, 'acquire', 'b'],
               [2, 'acquire', 'c'],
               [3, 'release', 'a'],
               [8, 'acquire', 'c']],
    'lease_ms': 10000,
    'max_concurrent': 2},
   [['granted', 'granted', 'rejected', 'released', 'granted'], ['b', 'c']]]],
 [['lease expires exactly',
   {'events': [[0, 'acquire', 'a'],
               [999, 'acquire', 'b'],
               [1000, 'acquire', 'b'],
               [1505, 'release', 'b']],
    'lease_ms': 1000,
    'max_concurrent': 1},
   [['granted', 'rejected', 'granted', 'released'], []]],
  ['unknown release',
   {'events': [[0, 'acquire', 'a'],
               [10, 'acquire', 'b'],
               [20, 'release', 'zz'],
               [30, 'acquire', 'c'],
               [45, 'release', 'a']],
    'lease_ms': 5000,
    'max_concurrent': 2},
   [['granted', 'granted', 'unknown', 'rejected', 'released'], ['b']]],
  ['duplicate acquire',
   {'events': [[0, 'acquire', 'a'],
               [800, 'acquire', 'a'],
               [1005, 'acquire', 'b'],
               [1100, 'acquire', 'c']],
    'lease_ms': 1000,
    'max_concurrent': 2},
   [['granted', 'duplicate', 'granted', 'granted'], ['b', 'c']]],
  ['release after expiry',
   {'events': [[0, 'acquire', 'a'], [305, 'release', 'a'], [306, 'acquire', 'b']],
    'lease_ms': 300,
    'max_concurrent': 1},
   [['granted', 'unknown', 'granted'], ['b']]],
  ['random leases',
   {'events': [[14, 'acquire', 'b'],
               [177, 'release', 'a'],
               [334, 'release', 'd'],
               [476, 'acquire', 'a'],
               [631, 'release', 'a'],
               [764, 'acquire', 'b'],
               [901, 'acquire', 'a'],
               [1084, 'release', 'b'],
               [1200, 'acquire', 'b'],
               [1376, 'release', 'd'],
               [1506, 'release', 'a'],
               [1680, 'acquire', 'a']],
    'lease_ms': 600,
    'max_concurrent': 2},
   [['granted',
     'unknown',
     'unknown',
     'granted',
     'released',
     'granted',
     'granted',
     'released',
     'granted',
     'unknown',
     'unknown',
     'granted'],
    ['a', 'b']]],
  ['full pool',
   {'events': [[0, 'acquire', 'a'],
               [1, 'acquire', 'b'],
               [2, 'acquire', 'c'],
               [3, 'release', 'a'],
               [9, 'acquire', 'c']],
    'lease_ms': 10000,
    'max_concurrent': 2},
   [['granted', 'granted', 'rejected', 'released', 'granted'], ['b', 'c']]]]]
for label, args, expected in cases[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A slot stays occupied one tick past its lease, rejecting a waiting acquirer.

Input {max_concurrent, lease_ms, events [[t, op, id]]}. Before every event, leases with start + lease_ms <= t expire. acquire returns "duplicate" (no change) for a held id, "granted" (recording t) when fewer than max_concurrent leases are held, else "rejected". release returns "released" for a held id and "unknown" otherwise, changing nothing. Return [results, sorted held ids].

split: open-access