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

N = 1
observations = []
def solve(frames, maps, moves):
    out = []
    for f in frames:
        key = f['pc']
        if key not in maps:
            return ['no-stackmap', key]
        m = maps[key]
        old = list(f['slots'])
        slots = list(old)
        for i in range(len(slots)):
            if m['refs'] >> i & 1 and old[i] is not None:
                slots[i] = moves.get(old[i], old[i])
        for dslot, bslot in m['derived']:
            base = old[bslot]
            slots[dslot] = moves.get(base, base) + abs(old[dslot] - base)
        out.append(slots)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: references, integers and derived pointers',
   ([{'pc': 10, 'slots': [1000, 8, 2000, 1016, 1000, 1992]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5016, 2000: 2400, 3000: 3000}),
   [[5016, 8, 2400, 5032, 1000, 2392]]),
  ('integer that looks like a moved address',
   ([{'pc': 20, 'slots': [2000, 1000, 3001]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5016, 2000: 2400, 3000: 3000}),
   [[2000, 5016, 3001]]),
  ('derived pointer below its base',
   ([{'pc': 30, 'slots': [None, 2000, 1989]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5016, 2000: 2400, 3000: 3000}),
   [[None, 2400, 2389]]),
  ('several frames',
   ([{'pc': 20, 'slots': [5, 2000, 9]}, {'pc': 30, 'slots': [1000, 3000, 3005]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5016, 2000: 2400, 3000: 3000}),
   [[5, 2400, 9], [5016, 3000, 3005]]),
  ('missing stack map',
   ([{'pc': 20, 'slots': [1, 2, 3]}, {'pc': 12, 'slots': []}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5016, 2000: 2400, 3000: 3000}),
   ['no-stackmap', 12]),
  ('control: null reference slots',
   ([{'pc': 20, 'slots': [1, None, 0]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5016, 2000: 2400, 3000: 3000}),
   [[1, None, 0]])],
 [('regression: references, integers and derived pointers',
   ([{'pc': 10, 'slots': [1000, 9, 2000, 1016, 1000, 1992]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5032, 2000: 2400, 3000: 3000}),
   [[5032, 9, 2400, 5048, 1000, 2392]]),
  ('integer that looks like a moved address',
   ([{'pc': 20, 'slots': [2000, 1000, 3002]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5032, 2000: 2400, 3000: 3000}),
   [[2000, 5032, 3002]]),
  ('derived pointer below its base',
   ([{'pc': 30, 'slots': [None, 2000, 1988]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5032, 2000: 2400, 3000: 3000}),
   [[None, 2400, 2388]]),
  ('several frames',
   ([{'pc': 20, 'slots': [5, 2000, 9]}, {'pc': 30, 'slots': [1000, 3000, 3006]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5032, 2000: 2400, 3000: 3000}),
   [[5, 2400, 9], [5032, 3000, 3006]]),
  ('missing stack map',
   ([{'pc': 20, 'slots': [1, 2, 3]}, {'pc': 13, 'slots': []}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5032, 2000: 2400, 3000: 3000}),
   ['no-stackmap', 13]),
  ('control: null reference slots',
   ([{'pc': 20, 'slots': [2, None, 0]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5032, 2000: 2400, 3000: 3000}),
   [[2, None, 0]])],
 [('regression: references, integers and derived pointers',
   ([{'pc': 10, 'slots': [1000, 10, 2000, 1016, 1000, 1992]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5048, 2000: 2400, 3000: 3000}),
   [[5048, 10, 2400, 5064, 1000, 2392]]),
  ('integer that looks like a moved address',
   ([{'pc': 20, 'slots': [2000, 1000, 3003]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5048, 2000: 2400, 3000: 3000}),
   [[2000, 5048, 3003]]),
  ('derived pointer below its base',
   ([{'pc': 30, 'slots': [None, 2000, 1987]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5048, 2000: 2400, 3000: 3000}),
   [[None, 2400, 2387]]),
  ('several frames',
   ([{'pc': 20, 'slots': [5, 2000, 9]}, {'pc': 30, 'slots': [1000, 3000, 3007]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5048, 2000: 2400, 3000: 3000}),
   [[5, 2400, 9], [5048, 3000, 3007]]),
  ('missing stack map',
   ([{'pc': 20, 'slots': [1, 2, 3]}, {'pc': 14, 'slots': []}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5048, 2000: 2400, 3000: 3000}),
   ['no-stackmap', 14]),
  ('control: null reference slots',
   ([{'pc': 20, 'slots': [3, None, 0]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5048, 2000: 2400, 3000: 3000}),
   [[3, None, 0]])],
 [('regression: references, integers and derived pointers',
   ([{'pc': 10, 'slots': [1000, 11, 2000, 1016, 1000, 1992]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5064, 2000: 2400, 3000: 3000}),
   [[5064, 11, 2400, 5080, 1000, 2392]]),
  ('integer that looks like a moved address',
   ([{'pc': 20, 'slots': [2000, 1000, 3004]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5064, 2000: 2400, 3000: 3000}),
   [[2000, 5064, 3004]]),
  ('derived pointer below its base',
   ([{'pc': 30, 'slots': [None, 2000, 1986]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5064, 2000: 2400, 3000: 3000}),
   [[None, 2400, 2386]]),
  ('several frames',
   ([{'pc': 20, 'slots': [5, 2000, 9]}, {'pc': 30, 'slots': [1000, 3000, 3008]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5064, 2000: 2400, 3000: 3000}),
   [[5, 2400, 9], [5064, 3000, 3008]]),
  ('missing stack map',
   ([{'pc': 20, 'slots': [1, 2, 3]}, {'pc': 15, 'slots': []}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5064, 2000: 2400, 3000: 3000}),
   ['no-stackmap', 15]),
  ('control: null reference slots',
   ([{'pc': 20, 'slots': [4, None, 0]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5064, 2000: 2400, 3000: 3000}),
   [[4, None, 0]])],
 [('regression: references, integers and derived pointers',
   ([{'pc': 10, 'slots': [1000, 12, 2000, 1016, 1000, 1992]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5080, 2000: 2400, 3000: 3000}),
   [[5080, 12, 2400, 5096, 1000, 2392]]),
  ('integer that looks like a moved address',
   ([{'pc': 20, 'slots': [2000, 1000, 3005]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5080, 2000: 2400, 3000: 3000}),
   [[2000, 5080, 3005]]),
  ('derived pointer below its base',
   ([{'pc': 30, 'slots': [None, 2000, 1985]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5080, 2000: 2400, 3000: 3000}),
   [[None, 2400, 2385]]),
  ('several frames',
   ([{'pc': 20, 'slots': [5, 2000, 9]}, {'pc': 30, 'slots': [1000, 3000, 3009]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5080, 2000: 2400, 3000: 3000}),
   [[5, 2400, 9], [5080, 3000, 3009]]),
  ('missing stack map',
   ([{'pc': 20, 'slots': [1, 2, 3]}, {'pc': 16, 'slots': []}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5080, 2000: 2400, 3000: 3000}),
   ['no-stackmap', 16]),
  ('control: null reference slots',
   ([{'pc': 20, 'slots': [5, None, 0]}],
    {10: {'derived': [[3, 0], [5, 2]], 'refs': 5},
     20: {'derived': [], 'refs': 2},
     30: {'derived': [[2, 1]], 'refs': 3}},
    {1000: 5080, 2000: 2400, 3000: 3000}),
   [[5, None, 0]])]]
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:
Derived pointers are reset to the object start after a move.

Relocate frame slots after a moving collection. Each frame has a safepoint pc and a slot list; maps[pc] gives a reference bitmask (bit i set means slot i holds a reference; bit 0 is slot 0) and derived pointers [derived_slot, base_slot], which hold base + offset and are not in the mask. moves maps old object addresses to new ones (unmoved objects are absent or map to themselves). Reference slots are relocated, derived slots become new base + (old derived - old base), and every other slot is left untouched. A pc without a map returns ["no-stackmap", pc].

split: open-access


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

N = 1
observations = []
def solve(ages, capacity, ratio, max_threshold):
    desired = capacity * ratio // 100
    by_age = {}
    for _, age, size in ages:
        by_age[age] = by_age.get(age, 0) + size
    total = 0
    threshold = max_threshold
    for age in range(1, max_threshold + 1):
        total += by_age.get(age, 0)
        if total >= desired:
            threshold = age
            break
    promote = [o for o, age, size in ages if age >= threshold]
    stay = sorted([x for x in ages if x[1] < threshold], key=lambda x: (x[1], x[0]))
    used = 0
    kept = []
    for o, age, size in stay:
        if used + size <= capacity:
            kept.append(o)
            used += size
        else:
            promote.append(o)
    return {'threshold': threshold, 'promoted': sorted(promote), 'kept': sorted(kept), 'used': used}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 1]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 46}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 6]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 1]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 71}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 11], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 71}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 1]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 6]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 11}),
  ('control: empty survivor set',
   ([], 101, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 2]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 47}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 7]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 2]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 72}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 12], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 72}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 2]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 7]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 12}),
  ('control: empty survivor set',
   ([], 102, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 3]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 48}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 8]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 3]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 73}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 13], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 73}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 3]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 8]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 13}),
  ('control: empty survivor set',
   ([], 103, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 4]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 49}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 9]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 4]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 74}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 14], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 74}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 4]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 9]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 14}),
  ('control: empty survivor set',
   ([], 104, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 5]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 50}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 10]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 5]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 75}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 15], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 75}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 5]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 10]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 15}),
  ('control: empty survivor set',
   ([], 105, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})]]
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(ages, capacity, ratio, max_threshold):
    desired = capacity * ratio // 100
    by_age = {}
    for _, age, size in ages:
        by_age[age] = by_age.get(age, 0) + size
    total = 0
    threshold = max_threshold
    for age in range(1, max_threshold + 1):
        total += by_age.get(age, 0)
        if total > capacity:
            threshold = age
            break
    promote = [o for o, age, size in ages if age >= threshold]
    stay = sorted([x for x in ages if x[1] < threshold], key=lambda x: (x[1], x[0]))
    used = 0
    kept = []
    for o, age, size in stay:
        if used + size <= capacity:
            kept.append(o)
            used += size
        else:
            promote.append(o)
    return {'threshold': threshold, 'promoted': sorted(promote), 'kept': sorted(kept), 'used': used}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 1]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 46}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 6]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 1]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 71}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 11], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 71}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 1]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 6]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 11}),
  ('control: empty survivor set',
   ([], 101, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 2]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 47}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 7]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 2]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 72}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 12], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 72}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 2]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 7]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 12}),
  ('control: empty survivor set',
   ([], 102, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 3]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 48}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 8]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 3]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 73}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 13], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 73}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 3]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 8]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 13}),
  ('control: empty survivor set',
   ([], 103, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 4]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 49}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 9]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 4]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 74}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 14], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 74}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 4]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 9]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 14}),
  ('control: empty survivor set',
   ([], 104, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 5]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 50}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 10]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 5]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 75}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 15], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 75}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 5]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 10]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 15}),
  ('control: empty survivor set',
   ([], 105, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})]]
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:
Survivor populations exactly at the target size are promoted a cycle early.

Survivors are [id, age, size] after a minor collection. desired = capacity * ratio // 100. Walking ages 1..max_threshold, accumulate sizes; the first age at which the running total exceeds desired becomes the tenuring threshold (max_threshold if never exceeded). Objects with age >= threshold are promoted; the rest are placed youngest first (ties by id) into the survivor space of the given capacity, and any that do not fit overflow into promotion. Return threshold, promoted ids, kept ids and bytes used.

split: open-access


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

N = 1
observations = []
def solve(ages, capacity, ratio, max_threshold):
    desired = capacity * (ratio // 100)
    by_age = {}
    for _, age, size in ages:
        by_age[age] = by_age.get(age, 0) + size
    total = 0
    threshold = max_threshold
    for age in range(1, max_threshold + 1):
        total += by_age.get(age, 0)
        if total > desired:
            threshold = age
            break
    promote = [o for o, age, size in ages if age >= threshold]
    stay = sorted([x for x in ages if x[1] < threshold], key=lambda x: (x[1], x[0]))
    used = 0
    kept = []
    for o, age, size in stay:
        if used + size <= capacity:
            kept.append(o)
            used += size
        else:
            promote.append(o)
    return {'threshold': threshold, 'promoted': sorted(promote), 'kept': sorted(kept), 'used': used}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 1]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 46}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 6]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 1]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 71}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 11], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 71}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 1]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 6]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 11}),
  ('control: empty survivor set',
   ([], 101, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 2]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 47}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 7]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 2]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 72}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 12], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 72}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 2]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 7]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 12}),
  ('control: empty survivor set',
   ([], 102, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 3]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 48}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 8]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 3]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 73}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 13], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 73}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 3]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 8]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 13}),
  ('control: empty survivor set',
   ([], 103, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 4]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 49}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 9]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 4]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 74}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 14], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 74}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 4]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 9]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 14}),
  ('control: empty survivor set',
   ([], 104, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 5]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 50}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 10]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 5]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 75}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 15], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 75}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 5]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 10]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 15}),
  ('control: empty survivor set',
   ([], 105, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})]]
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(ages, capacity, ratio, max_threshold):
    desired = capacity // 100 * ratio
    by_age = {}
    for _, age, size in ages:
        by_age[age] = by_age.get(age, 0) + size
    total = 0
    threshold = max_threshold
    for age in range(1, max_threshold + 1):
        total += by_age.get(age, 0)
        if total > desired:
            threshold = age
            break
    promote = [o for o, age, size in ages if age >= threshold]
    stay = sorted([x for x in ages if x[1] < threshold], key=lambda x: (x[1], x[0]))
    used = 0
    kept = []
    for o, age, size in stay:
        if used + size <= capacity:
            kept.append(o)
            used += size
        else:
            promote.append(o)
    return {'threshold': threshold, 'promoted': sorted(promote), 'kept': sorted(kept), 'used': used}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 1]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 46}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 6]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 1]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 71}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 11], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 71}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 1]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 6]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 11}),
  ('control: empty survivor set',
   ([], 101, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 2]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 47}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 7]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 2]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 72}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 12], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 72}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 2]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 7]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 12}),
  ('control: empty survivor set',
   ([], 102, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 3]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 48}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 8]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 3]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 73}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 13], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 73}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 3]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 8]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 13}),
  ('control: empty survivor set',
   ([], 103, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 4]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 49}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 9]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 4]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 74}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 14], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 74}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 4]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 9]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 14}),
  ('control: empty survivor set',
   ([], 104, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 5]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 50}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 10]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 5]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 75}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 15], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 75}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 5]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 10]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 15}),
  ('control: empty survivor set',
   ([], 105, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})]]
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:
Every survivor is promoted after its first collection.

Survivors are [id, age, size] after a minor collection. desired = capacity * ratio // 100. Walking ages 1..max_threshold, accumulate sizes; the first age at which the running total exceeds desired becomes the tenuring threshold (max_threshold if never exceeded). Objects with age >= threshold are promoted; the rest are placed youngest first (ties by id) into the survivor space of the given capacity, and any that do not fit overflow into promotion. Return threshold, promoted ids, kept ids and bytes used.

split: open-access


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

N = 1
observations = []
def solve(ages, capacity, ratio, max_threshold):
    desired = capacity * ratio // 100
    by_age = {}
    for _, age, size in ages:
        by_age[age] = by_age.get(age, 0) + size
    total = 0
    threshold = max_threshold
    for age in range(1, max_threshold + 1):
        total += by_age.get(age, 0)
        if total > desired:
            threshold = age
            break
    promote = [o for o, age, size in ages if age > threshold]
    stay = sorted([x for x in ages if x[1] < threshold], key=lambda x: (x[1], x[0]))
    used = 0
    kept = []
    for o, age, size in stay:
        if used + size <= capacity:
            kept.append(o)
            used += size
        else:
            promote.append(o)
    return {'threshold': threshold, 'promoted': sorted(promote), 'kept': sorted(kept), 'used': used}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 1]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 46}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 6]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 1]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 71}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 11], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 71}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 1]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 6]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 11}),
  ('control: empty survivor set',
   ([], 101, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 2]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 47}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 7]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 2]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 72}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 12], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 72}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 2]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 7]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 12}),
  ('control: empty survivor set',
   ([], 102, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 3]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 48}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 8]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 3]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 73}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 13], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 73}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 3]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 8]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 13}),
  ('control: empty survivor set',
   ([], 103, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 4]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 49}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 9]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 4]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 74}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 14], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 74}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 4]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 9]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 14}),
  ('control: empty survivor set',
   ([], 104, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 5]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 50}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 10]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 5]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 75}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 15], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 75}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 5]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 10]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 15}),
  ('control: empty survivor set',
   ([], 105, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})]]
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(ages, capacity, ratio, max_threshold):
    desired = capacity * ratio // 100
    by_age = {}
    for _, age, size in ages:
        by_age[age] = by_age.get(age, 0) + size
    total = 0
    threshold = max_threshold
    for age in range(1, max_threshold + 1):
        total += by_age.get(age, 0)
        if total > desired:
            threshold = age
            break
    promote = [o for o, age, size in ages if age >= threshold - 1]
    stay = sorted([x for x in ages if x[1] < threshold], key=lambda x: (x[1], x[0]))
    used = 0
    kept = []
    for o, age, size in stay:
        if used + size <= capacity:
            kept.append(o)
            used += size
        else:
            promote.append(o)
    return {'threshold': threshold, 'promoted': sorted(promote), 'kept': sorted(kept), 'used': used}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 1]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 46}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 6]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 1]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 71}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 11], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 71}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 1]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 6]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 11}),
  ('control: empty survivor set',
   ([], 101, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 2]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 47}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 7]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 2]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 72}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 12], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 72}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 2]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 7]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 12}),
  ('control: empty survivor set',
   ([], 102, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 3]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 48}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 8]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 3]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 73}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 13], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 73}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 3]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 8]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 13}),
  ('control: empty survivor set',
   ([], 103, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 4]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 49}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 9]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 4]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 74}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 14], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 74}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 4]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 9]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 14}),
  ('control: empty survivor set',
   ([], 104, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 5]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 50}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 10]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 5]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 75}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 15], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 75}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 5]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 10]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 15}),
  ('control: empty survivor set',
   ([], 105, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})]]
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:
Survivors whose age equals the threshold vanish from both lists.

Survivors are [id, age, size] after a minor collection. desired = capacity * ratio // 100. Walking ages 1..max_threshold, accumulate sizes; the first age at which the running total exceeds desired becomes the tenuring threshold (max_threshold if never exceeded). Objects with age >= threshold are promoted; the rest are placed youngest first (ties by id) into the survivor space of the given capacity, and any that do not fit overflow into promotion. Return threshold, promoted ids, kept ids and bytes used.

split: open-access


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

N = 1
observations = []
def solve(ages, capacity, ratio, max_threshold):
    desired = capacity * ratio // 100
    by_age = {}
    for _, age, size in ages:
        by_age[age] = by_age.get(age, 0) + size
    total = 0
    threshold = max_threshold
    for age in range(1, max_threshold + 1):
        total += by_age.get(age, 0)
        if total > desired:
            threshold = age
            break
    promote = [o for o, age, size in ages if age >= threshold]
    stay = sorted([x for x in ages if x[1] < threshold], key=lambda x: (-x[1], x[0]))
    used = 0
    kept = []
    for o, age, size in stay:
        if used + size <= capacity:
            kept.append(o)
            used += size
        else:
            promote.append(o)
    return {'threshold': threshold, 'promoted': sorted(promote), 'kept': sorted(kept), 'used': used}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 1]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 46}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 6]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 1]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 71}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 11], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 71}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 1]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 6]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 11}),
  ('control: empty survivor set',
   ([], 101, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 2]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 47}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 7]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 2]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 72}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 12], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 72}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 2]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 7]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 12}),
  ('control: empty survivor set',
   ([], 102, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 3]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 48}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 8]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 3]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 73}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 13], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 73}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 3]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 8]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 13}),
  ('control: empty survivor set',
   ([], 103, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 4]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 49}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 9]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 4]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 74}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 14], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 74}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 4]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 9]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 14}),
  ('control: empty survivor set',
   ([], 104, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 5]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 50}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 10]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 5]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 75}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 15], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 75}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 5]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 10]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 15}),
  ('control: empty survivor set',
   ([], 105, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})]]
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(ages, capacity, ratio, max_threshold):
    desired = capacity * ratio // 100
    by_age = {}
    for _, age, size in ages:
        by_age[age] = by_age.get(age, 0) + size
    total = 0
    threshold = max_threshold
    for age in range(1, max_threshold + 1):
        total += by_age.get(age, 0)
        if total > desired:
            threshold = age
            break
    promote = [o for o, age, size in ages if age >= threshold]
    stay = sorted([x for x in ages if x[1] < threshold], key=lambda x: x[0])
    used = 0
    kept = []
    for o, age, size in stay:
        if used + size <= capacity:
            kept.append(o)
            used += size
        else:
            promote.append(o)
    return {'threshold': threshold, 'promoted': sorted(promote), 'kept': sorted(kept), 'used': used}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 1]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 46}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 6]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 1]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 71}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 11], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 71}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 1]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 6]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 11}),
  ('control: empty survivor set',
   ([], 101, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 2]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 47}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 7]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 2]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 72}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 12], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 72}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 2]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 7]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 12}),
  ('control: empty survivor set',
   ([], 102, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 3]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 48}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 8]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 3]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 73}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 13], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 73}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 3]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 8]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 13}),
  ('control: empty survivor set',
   ([], 103, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 4]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 49}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 9]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 4]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 74}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 14], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 74}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 4]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 9]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 14}),
  ('control: empty survivor set',
   ([], 104, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 5]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 50}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 10]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 5]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 75}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 15], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 75}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 5]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 10]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 15}),
  ('control: empty survivor set',
   ([], 105, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})]]
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:
On overflow the youngest objects are promoted while older ones stay in survivor space.

Survivors are [id, age, size] after a minor collection. desired = capacity * ratio // 100. Walking ages 1..max_threshold, accumulate sizes; the first age at which the running total exceeds desired becomes the tenuring threshold (max_threshold if never exceeded). Objects with age >= threshold are promoted; the rest are placed youngest first (ties by id) into the survivor space of the given capacity, and any that do not fit overflow into promotion. Return threshold, promoted ids, kept ids and bytes used.

split: open-access


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

N = 1
observations = []
def solve(ages, capacity, ratio, max_threshold):
    desired = capacity * ratio // 100
    by_age = {}
    for _, age, size in ages:
        by_age[age] = by_age.get(age, 0) + size
    total = 0
    threshold = max_threshold
    for age in range(1, max_threshold + 1):
        total += by_age.get(age, 0)
        if total > desired:
            threshold = age
            break
    promote = [o for o, age, size in ages if age >= threshold]
    stay = sorted([x for x in ages if x[1] < threshold], key=lambda x: (x[1], x[0]))
    used = 0
    kept = []
    for o, age, size in stay:
        if used + size < capacity:
            kept.append(o)
            used += size
        else:
            promote.append(o)
    return {'threshold': threshold, 'promoted': sorted(promote), 'kept': sorted(kept), 'used': used}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 1]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 46}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 6]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 1]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 71}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 11], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 71}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 1]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 6]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 11}),
  ('control: empty survivor set',
   ([], 101, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 2]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 47}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 7]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 2]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 72}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 12], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 72}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 2]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 7]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 12}),
  ('control: empty survivor set',
   ([], 102, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 3]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 48}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 8]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 3]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 73}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 13], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 73}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 3]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 8]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 13}),
  ('control: empty survivor set',
   ([], 103, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 4]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 49}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 9]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 4]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 74}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 14], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 74}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 4]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 9]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 14}),
  ('control: empty survivor set',
   ([], 104, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 5]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 50}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 10]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 5]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 75}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 15], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 75}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 5]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 10]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 15}),
  ('control: empty survivor set',
   ([], 105, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})]]
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(ages, capacity, ratio, max_threshold):
    desired = capacity * ratio // 100
    by_age = {}
    for _, age, size in ages:
        by_age[age] = by_age.get(age, 0) + size
    total = 0
    threshold = max_threshold
    for age in range(1, max_threshold + 1):
        total += by_age.get(age, 0)
        if total > desired:
            threshold = age
            break
    promote = [o for o, age, size in ages if age >= threshold]
    stay = sorted([x for x in ages if x[1] < threshold], key=lambda x: (x[1], x[0]))
    used = 0
    kept = []
    for o, age, size in stay:
        if used < capacity:
            kept.append(o)
            used += size
        else:
            promote.append(o)
    return {'threshold': threshold, 'promoted': sorted(promote), 'kept': sorted(kept), 'used': used}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 1]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 46}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 6]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 1]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 71}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 11], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 71}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 1]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 6]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 11}),
  ('control: empty survivor set',
   ([], 101, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 2]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 47}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 7]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 2]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 72}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 12], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 72}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 2]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 7]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 12}),
  ('control: empty survivor set',
   ([], 102, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 3]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 48}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 8]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 3]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 73}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 13], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 73}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 3]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 8]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 13}),
  ('control: empty survivor set',
   ([], 103, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 4]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 49}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 9]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 4]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 74}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 14], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 74}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 4]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 9]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 14}),
  ('control: empty survivor set',
   ([], 104, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})],
 [('regression: cumulative sizes cross the desired size',
   ([[1, 1, 20], [2, 2, 25], [3, 3, 10], [4, 4, 30], [5, 1, 5]], 100, 50, 15),
   {'kept': [1, 2, 5], 'promoted': [3, 4], 'threshold': 3, 'used': 50}),
  ('cumulative size exactly at the desired size',
   ([[1, 1, 30], [2, 2, 20], [3, 3, 10]], 100, 50, 15),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 3, 'used': 50}),
  ('ratio of a capacity that is not a multiple of 100',
   ([[1, 1, 40], [2, 2, 30], [3, 3, 5]], 150, 50, 15),
   {'kept': [1, 2, 3], 'promoted': [], 'threshold': 15, 'used': 75}),
  ('survivors overflow the survivor space',
   ([[1, 1, 30], [2, 1, 30], [3, 2, 30], [4, 1, 15], [5, 2, 40]], 100, 200, 3),
   {'kept': [1, 2, 4], 'promoted': [3, 5], 'threshold': 3, 'used': 75}),
  ('survivors fill the space exactly',
   ([[1, 1, 60], [2, 1, 40], [3, 2, 5]], 100, 200, 2),
   {'kept': [1, 2], 'promoted': [3], 'threshold': 2, 'used': 100}),
  ('threshold never crossed',
   ([[1, 1, 5], [2, 3, 10]], 100, 50, 6),
   {'kept': [1, 2], 'promoted': [], 'threshold': 6, 'used': 15}),
  ('control: empty survivor set',
   ([], 105, 50, 15),
   {'kept': [], 'promoted': [], 'threshold': 15, 'used': 0})]]
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:
An object that would fill the survivor space exactly is promoted instead.

Survivors are [id, age, size] after a minor collection. desired = capacity * ratio // 100. Walking ages 1..max_threshold, accumulate sizes; the first age at which the running total exceeds desired becomes the tenuring threshold (max_threshold if never exceeded). Objects with age >= threshold are promoted; the rest are placed youngest first (ties by id) into the survivor space of the given capacity, and any that do not fit overflow into promotion. Return threshold, promoted ids, kept ids and bytes used.

split: open-access