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

N = 1
observations = []
def solve(slots, item, qty, max_stack):
    slots = [s if s is None else list(s) for s in slots]
    left = qty
    for s in slots:
        if left == 0:
            break
        if s is not None and s[0] == item and s[1] != max_stack:
            add = min(left, max_stack - s[1])
            s[1] += add
            left -= add
    for i in range(len(slots)):
        if left == 0:
            break
        if slots[i] is None:
            add = min(left, max_stack)
            slots[i] = [item, add]
            left -= add
    return {'slots': slots, 'leftover': left}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('fault site partial stack test #1',
   [[['Potion', 20], None, ['Arrow', 13], ['arrow', 20]], 'Potion', 20, 5],
   {'slots': [['Potion', 20], ['Potion', 5], ['Arrow', 13], ['arrow', 20]], 'leftover': 15}),
  ('fault site partial stack test #2',
   [[['arrow', 0], ['arrow', 5], ['Potion', 18]], 'Potion', 45, 5],
   {'slots': [['arrow', 0], ['arrow', 5], ['Potion', 18]], 'leftover': 45}),
  ('partial repair boundary #1',
   [[['Arrow', 0], ['Arrow', 1], ['arrow', 5], None, ['arrow', 19], ['Potion', 0]], 'Arrow', 39, 20],
   {'slots': [['Arrow', 20], ['Arrow', 20], ['arrow', 5], None, ['arrow', 19], ['Potion', 0]],
    'leftover': 0}),
  ('regression partial stack test #1',
   [[None, None, ['Potion', 0], ['Potion', 18], ['Arrow', 19], ['arrow', 18]], 'Potion', 45, 5],
   {'slots': [['Potion', 5], ['Potion', 5], ['Potion', 5], ['Potion', 18], ['Arrow', 19], ['arrow', 18]],
    'leftover': 30}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('fault site partial stack test #1',
   [[['arrow', 0], ['arrow', 5], ['Potion', 18]], 'Potion', 45, 5],
   {'slots': [['arrow', 0], ['arrow', 5], ['Potion', 18]], 'leftover': 45}),
  ('fault site partial stack test #2',
   [[None, ['arrow', 1], ['Arrow', 19], ['arrow', 10], None], 'arrow', 20, 5],
   {'slots': [['arrow', 5], ['arrow', 5], ['Arrow', 19], ['arrow', 10], ['arrow', 5]], 'leftover': 6}),
  ('partial repair boundary #1',
   [[None, ['Arrow', 0], ['Potion', 0]], 'Potion', 20, 10],
   {'slots': [['Potion', 10], ['Arrow', 0], ['Potion', 10]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[['arrow', 20], ['Arrow', 0], ['Potion', 19]], 'Arrow', 7, 10],
   {'slots': [['arrow', 20], ['Arrow', 7], ['Potion', 19]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('fault site partial stack test #1',
   [[['arrow', 20], None, ['Potion', 4], ['Arrow', 20], ['arrow', 19]], 'arrow', 45, 10],
   {'slots': [['arrow', 20], ['arrow', 10], ['Potion', 4], ['Arrow', 20], ['arrow', 19]], 'leftover': 35}),
  ('regression partial stack test #1',
   [[None, None, ['Potion', 0], ['Potion', 18], ['Arrow', 19], ['arrow', 18]], 'Potion', 45, 5],
   {'slots': [['Potion', 5], ['Potion', 5], ['Potion', 5], ['Potion', 18], ['Arrow', 19], ['arrow', 18]],
    'leftover': 30}),
  ('partial repair boundary #1',
   [[['arrow', 20], ['Arrow', 16], ['arrow', 20], None, None, ['Potion', 0]], 'Potion', 1, 20],
   {'slots': [['arrow', 20], ['Arrow', 16], ['arrow', 20], None, None, ['Potion', 1]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[None, ['Potion', 1], ['Potion', 20], ['Potion', 19], ['Arrow', 0]], 'Arrow', 20, 5],
   {'slots': [['Arrow', 5], ['Potion', 1], ['Potion', 20], ['Potion', 19], ['Arrow', 5]], 'leftover': 10}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('fault site partial stack test #1',
   [[None, ['Potion', 5], ['Potion', 0], None, ['arrow', 20], ['Arrow', 17]], 'Arrow', 7, 5],
   {'slots': [['Arrow', 5], ['Potion', 5], ['Potion', 0], ['Arrow', 2], ['arrow', 20], ['Arrow', 17]],
    'leftover': 0}),
  ('fault site partial stack test #2',
   [[['arrow', 1], ['Potion', 19]], 'Potion', 20, 10],
   {'slots': [['arrow', 1], ['Potion', 19]], 'leftover': 20}),
  ('partial repair boundary #1',
   [[['Potion', 0], ['Arrow', 0], ['Arrow', 1]], 'Arrow', 45, 10],
   {'slots': [['Potion', 0], ['Arrow', 10], ['Arrow', 10]], 'leftover': 26}),
  ('partial repair boundary #2',
   [[['Arrow', 5], None, ['Arrow', 20], ['arrow', 0], ['Potion', 0]], 'arrow', 7, 20],
   {'slots': [['Arrow', 5], None, ['Arrow', 20], ['arrow', 7], ['Potion', 0]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[None, ['Arrow', 16], ['arrow', 20], None, ['Arrow', 25], None], 'Potion', 20, 10],
   {'slots': [['Potion', 10], ['Arrow', 16], ['arrow', 20], ['Potion', 10], ['Arrow', 25], None],
    'leftover': 0})],
 [('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('fault site partial stack test #1',
   [[['Arrow', 25], ['arrow', 19], None], 'Arrow', 1, 10],
   {'slots': [['Arrow', 25], ['arrow', 19], ['Arrow', 1]], 'leftover': 0}),
  ('fault site partial stack test #2',
   [[['Arrow', 20], ['Arrow', 25], None, ['arrow', 19]], 'arrow', 1, 10],
   {'slots': [['Arrow', 20], ['Arrow', 25], ['arrow', 1], ['arrow', 19]], 'leftover': 0}),
  ('partial repair boundary #1',
   [[None, None, ['arrow', 1], ['Potion', 0]], 'Potion', 45, 10],
   {'slots': [['Potion', 10], ['Potion', 10], ['arrow', 1], ['Potion', 10]], 'leftover': 15}),
  ('partial repair boundary #2',
   [[None, None, ['arrow', 14], ['Arrow', 0], None, None], 'Arrow', 1, 5],
   {'slots': [None, None, ['arrow', 14], ['Arrow', 1], None, None], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1', [[['Potion', 0]], 'Arrow', 45, 5], {'slots': [['Potion', 0]], 'leftover': 45})]]
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(slots, item, qty, max_stack):
    slots = [s if s is None else list(s) for s in slots]
    left = qty
    for s in slots:
        if left == 0:
            break
        if s is not None and s[0] == item and s[1] < max_stack and s[1] > 0:
            add = min(left, max_stack - s[1])
            s[1] += add
            left -= add
    for i in range(len(slots)):
        if left == 0:
            break
        if slots[i] is None:
            add = min(left, max_stack)
            slots[i] = [item, add]
            left -= add
    return {'slots': slots, 'leftover': left}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('fault site partial stack test #1',
   [[['Potion', 20], None, ['Arrow', 13], ['arrow', 20]], 'Potion', 20, 5],
   {'slots': [['Potion', 20], ['Potion', 5], ['Arrow', 13], ['arrow', 20]], 'leftover': 15}),
  ('fault site partial stack test #2',
   [[['arrow', 0], ['arrow', 5], ['Potion', 18]], 'Potion', 45, 5],
   {'slots': [['arrow', 0], ['arrow', 5], ['Potion', 18]], 'leftover': 45}),
  ('partial repair boundary #1',
   [[['Arrow', 0], ['Arrow', 1], ['arrow', 5], None, ['arrow', 19], ['Potion', 0]], 'Arrow', 39, 20],
   {'slots': [['Arrow', 20], ['Arrow', 20], ['arrow', 5], None, ['arrow', 19], ['Potion', 0]],
    'leftover': 0}),
  ('regression partial stack test #1',
   [[None, None, ['Potion', 0], ['Potion', 18], ['Arrow', 19], ['arrow', 18]], 'Potion', 45, 5],
   {'slots': [['Potion', 5], ['Potion', 5], ['Potion', 5], ['Potion', 18], ['Arrow', 19], ['arrow', 18]],
    'leftover': 30}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('fault site partial stack test #1',
   [[['arrow', 0], ['arrow', 5], ['Potion', 18]], 'Potion', 45, 5],
   {'slots': [['arrow', 0], ['arrow', 5], ['Potion', 18]], 'leftover': 45}),
  ('fault site partial stack test #2',
   [[None, ['arrow', 1], ['Arrow', 19], ['arrow', 10], None], 'arrow', 20, 5],
   {'slots': [['arrow', 5], ['arrow', 5], ['Arrow', 19], ['arrow', 10], ['arrow', 5]], 'leftover': 6}),
  ('partial repair boundary #1',
   [[None, ['Arrow', 0], ['Potion', 0]], 'Potion', 20, 10],
   {'slots': [['Potion', 10], ['Arrow', 0], ['Potion', 10]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[['arrow', 20], ['Arrow', 0], ['Potion', 19]], 'Arrow', 7, 10],
   {'slots': [['arrow', 20], ['Arrow', 7], ['Potion', 19]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('fault site partial stack test #1',
   [[['arrow', 20], None, ['Potion', 4], ['Arrow', 20], ['arrow', 19]], 'arrow', 45, 10],
   {'slots': [['arrow', 20], ['arrow', 10], ['Potion', 4], ['Arrow', 20], ['arrow', 19]], 'leftover': 35}),
  ('regression partial stack test #1',
   [[None, None, ['Potion', 0], ['Potion', 18], ['Arrow', 19], ['arrow', 18]], 'Potion', 45, 5],
   {'slots': [['Potion', 5], ['Potion', 5], ['Potion', 5], ['Potion', 18], ['Arrow', 19], ['arrow', 18]],
    'leftover': 30}),
  ('partial repair boundary #1',
   [[['arrow', 20], ['Arrow', 16], ['arrow', 20], None, None, ['Potion', 0]], 'Potion', 1, 20],
   {'slots': [['arrow', 20], ['Arrow', 16], ['arrow', 20], None, None, ['Potion', 1]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[None, ['Potion', 1], ['Potion', 20], ['Potion', 19], ['Arrow', 0]], 'Arrow', 20, 5],
   {'slots': [['Arrow', 5], ['Potion', 1], ['Potion', 20], ['Potion', 19], ['Arrow', 5]], 'leftover': 10}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('fault site partial stack test #1',
   [[None, ['Potion', 5], ['Potion', 0], None, ['arrow', 20], ['Arrow', 17]], 'Arrow', 7, 5],
   {'slots': [['Arrow', 5], ['Potion', 5], ['Potion', 0], ['Arrow', 2], ['arrow', 20], ['Arrow', 17]],
    'leftover': 0}),
  ('fault site partial stack test #2',
   [[['arrow', 1], ['Potion', 19]], 'Potion', 20, 10],
   {'slots': [['arrow', 1], ['Potion', 19]], 'leftover': 20}),
  ('partial repair boundary #1',
   [[['Potion', 0], ['Arrow', 0], ['Arrow', 1]], 'Arrow', 45, 10],
   {'slots': [['Potion', 0], ['Arrow', 10], ['Arrow', 10]], 'leftover': 26}),
  ('partial repair boundary #2',
   [[['Arrow', 5], None, ['Arrow', 20], ['arrow', 0], ['Potion', 0]], 'arrow', 7, 20],
   {'slots': [['Arrow', 5], None, ['Arrow', 20], ['arrow', 7], ['Potion', 0]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[None, ['Arrow', 16], ['arrow', 20], None, ['Arrow', 25], None], 'Potion', 20, 10],
   {'slots': [['Potion', 10], ['Arrow', 16], ['arrow', 20], ['Potion', 10], ['Arrow', 25], None],
    'leftover': 0})],
 [('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('fault site partial stack test #1',
   [[['Arrow', 25], ['arrow', 19], None], 'Arrow', 1, 10],
   {'slots': [['Arrow', 25], ['arrow', 19], ['Arrow', 1]], 'leftover': 0}),
  ('fault site partial stack test #2',
   [[['Arrow', 20], ['Arrow', 25], None, ['arrow', 19]], 'arrow', 1, 10],
   {'slots': [['Arrow', 20], ['Arrow', 25], ['arrow', 1], ['arrow', 19]], 'leftover': 0}),
  ('partial repair boundary #1',
   [[None, None, ['arrow', 1], ['Potion', 0]], 'Potion', 45, 10],
   {'slots': [['Potion', 10], ['Potion', 10], ['arrow', 1], ['Potion', 10]], 'leftover': 15}),
  ('partial repair boundary #2',
   [[None, None, ['arrow', 14], ['Arrow', 0], None, None], 'Arrow', 1, 5],
   {'slots': [None, None, ['arrow', 14], ['Arrow', 1], None, None], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1', [[['Potion', 0]], 'Arrow', 45, 5], {'slots': [['Potion', 0]], 'leftover': 45})]]
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:
Adding items to an inventory with a legacy overfull stack destroys items.

slots = list of None or [name, count]; names are case sensitive. First top up partial stacks (count < max_stack) of the same item in slot order, then fill empty slots in order with new stacks of at most max_stack. Reserved placeholders [name, 0] count as partial stacks. Legacy stacks above max_stack are left untouched. Returns {slots, leftover}.

split: open-access


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

N = 1
observations = []
def solve(slots, item, qty, max_stack):
    slots = [s if s is None else list(s) for s in slots]
    left = qty
    for s in slots:
        if left == 0:
            break
        if s is not None and s[0] == item and s[1] < max_stack:
            add = min(left, max_stack)
            s[1] += add
            left -= add
    for i in range(len(slots)):
        if left == 0:
            break
        if slots[i] is None:
            add = min(left, max_stack)
            slots[i] = [item, add]
            left -= add
    return {'slots': slots, 'leftover': left}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up amount #1',
   [[None, ['Arrow', 1], ['arrow', 1], ['Potion', 20], ['arrow', 19]], 'arrow', 20, 20],
   {'slots': [None, ['Arrow', 1], ['arrow', 20], ['Potion', 20], ['arrow', 20]], 'leftover': 0}),
  ('fault site top-up amount #2',
   [[None, ['arrow', 5], None, None], 'arrow', 45, 10],
   {'slots': [['arrow', 10], ['arrow', 10], ['arrow', 10], ['arrow', 10]], 'leftover': 10}),
  ('partial repair boundary #1',
   [[None, ['Potion', 0], ['Potion', 20], ['arrow', 1]], 'arrow', 1, 10],
   {'slots': [None, ['Potion', 0], ['Potion', 20], ['arrow', 2]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[['arrow', 5], ['Arrow', 1]], 'Arrow', 1, 20],
   {'slots': [['arrow', 5], ['Arrow', 2]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up amount #1',
   [[None, ['arrow', 5], None, None], 'arrow', 45, 10],
   {'slots': [['arrow', 10], ['arrow', 10], ['arrow', 10], ['arrow', 10]], 'leftover': 10}),
  ('fault site top-up amount #2',
   [[None, ['arrow', 1], ['Arrow', 19], ['arrow', 10], None], 'arrow', 20, 5],
   {'slots': [['arrow', 5], ['arrow', 5], ['Arrow', 19], ['arrow', 10], ['arrow', 5]], 'leftover': 6}),
  ('partial repair boundary #1',
   [[['arrow', 5], ['Arrow', 1]], 'Arrow', 1, 20],
   {'slots': [['arrow', 5], ['Arrow', 2]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[['Arrow', 25], ['Arrow', 25], ['Potion', 1]], 'Potion', 7, 20],
   {'slots': [['Arrow', 25], ['Arrow', 25], ['Potion', 8]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up amount #1',
   [[['Potion', 17], ['arrow', 1], ['Arrow', 0], ['arrow', 19]], 'arrow', 45, 20],
   {'slots': [['Potion', 17], ['arrow', 20], ['Arrow', 0], ['arrow', 20]], 'leftover': 25}),
  ('fault site top-up amount #2',
   [[['Arrow', 5]], 'Arrow', 20, 20],
   {'slots': [['Arrow', 20]], 'leftover': 5}),
  ('partial repair boundary #1',
   [[['arrow', 1], None, ['Potion', 5]], 'arrow', 7, 20],
   {'slots': [['arrow', 8], None, ['Potion', 5]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[['arrow', 20], ['Arrow', 0], ['Potion', 19]], 'Arrow', 7, 10],
   {'slots': [['arrow', 20], ['Arrow', 7], ['Potion', 19]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up amount #1',
   [[['arrow', 25], None, ['Potion', 5], None, ['arrow', 20]], 'Potion', 7, 10],
   {'slots': [['arrow', 25], ['Potion', 2], ['Potion', 10], None, ['arrow', 20]], 'leftover': 0}),
  ('fault site top-up amount #2',
   [[None, None, ['Arrow', 11], ['Potion', 10], ['Arrow', 25], None], 'Potion', 20, 20],
   {'slots': [['Potion', 10], None, ['Arrow', 11], ['Potion', 20], ['Arrow', 25], None], 'leftover': 0}),
  ('partial repair boundary #1',
   [[['arrow', 5], ['Potion', 5], ['Potion', 1], ['Potion', 1], ['Arrow', 3]], 'arrow', 1, 20],
   {'slots': [['arrow', 6], ['Potion', 5], ['Potion', 1], ['Potion', 1], ['Arrow', 3]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[['arrow', 20], ['Arrow', 16], ['arrow', 20], None, None, ['Potion', 0]], 'Potion', 1, 20],
   {'slots': [['arrow', 20], ['Arrow', 16], ['arrow', 20], None, None, ['Potion', 1]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[None, ['Arrow', 16], ['arrow', 20], None, ['Arrow', 25], None], 'Potion', 20, 10],
   {'slots': [['Potion', 10], ['Arrow', 16], ['arrow', 20], ['Potion', 10], ['Arrow', 25], None],
    'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up amount #1',
   [[['Potion', 0], ['Arrow', 0], ['Arrow', 1]], 'Arrow', 45, 10],
   {'slots': [['Potion', 0], ['Arrow', 10], ['Arrow', 10]], 'leftover': 26}),
  ('fault site top-up amount #2',
   [[None, ['Potion', 11], ['arrow', 5], ['Potion', 19], None, ['Arrow', 0]], 'arrow', 45, 10],
   {'slots': [['arrow', 10], ['Potion', 11], ['arrow', 10], ['Potion', 19], ['arrow', 10], ['Arrow', 0]],
    'leftover': 20}),
  ('partial repair boundary #1',
   [[['Arrow', 5], None, ['Arrow', 20], ['arrow', 0], ['Potion', 0]], 'arrow', 7, 20],
   {'slots': [['Arrow', 5], None, ['Arrow', 20], ['arrow', 7], ['Potion', 0]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[None, None, ['arrow', 14], ['Arrow', 0], None, None], 'Arrow', 1, 5],
   {'slots': [None, None, ['arrow', 14], ['Arrow', 1], None, None], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1', [[['Potion', 0]], 'Arrow', 45, 5], {'slots': [['Potion', 0]], 'leftover': 45})]]
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(slots, item, qty, max_stack):
    slots = [s if s is None else list(s) for s in slots]
    left = qty
    for s in slots:
        if left == 0:
            break
        if s is not None and s[0] == item and s[1] < max_stack:
            add = max_stack - s[1]
            s[1] += add
            left -= add
    for i in range(len(slots)):
        if left == 0:
            break
        if slots[i] is None:
            add = min(left, max_stack)
            slots[i] = [item, add]
            left -= add
    return {'slots': slots, 'leftover': left}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up amount #1',
   [[None, ['Arrow', 1], ['arrow', 1], ['Potion', 20], ['arrow', 19]], 'arrow', 20, 20],
   {'slots': [None, ['Arrow', 1], ['arrow', 20], ['Potion', 20], ['arrow', 20]], 'leftover': 0}),
  ('fault site top-up amount #2',
   [[None, ['arrow', 5], None, None], 'arrow', 45, 10],
   {'slots': [['arrow', 10], ['arrow', 10], ['arrow', 10], ['arrow', 10]], 'leftover': 10}),
  ('partial repair boundary #1',
   [[None, ['Potion', 0], ['Potion', 20], ['arrow', 1]], 'arrow', 1, 10],
   {'slots': [None, ['Potion', 0], ['Potion', 20], ['arrow', 2]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[['arrow', 5], ['Arrow', 1]], 'Arrow', 1, 20],
   {'slots': [['arrow', 5], ['Arrow', 2]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up amount #1',
   [[None, ['arrow', 5], None, None], 'arrow', 45, 10],
   {'slots': [['arrow', 10], ['arrow', 10], ['arrow', 10], ['arrow', 10]], 'leftover': 10}),
  ('fault site top-up amount #2',
   [[None, ['arrow', 1], ['Arrow', 19], ['arrow', 10], None], 'arrow', 20, 5],
   {'slots': [['arrow', 5], ['arrow', 5], ['Arrow', 19], ['arrow', 10], ['arrow', 5]], 'leftover': 6}),
  ('partial repair boundary #1',
   [[['arrow', 5], ['Arrow', 1]], 'Arrow', 1, 20],
   {'slots': [['arrow', 5], ['Arrow', 2]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[['Arrow', 25], ['Arrow', 25], ['Potion', 1]], 'Potion', 7, 20],
   {'slots': [['Arrow', 25], ['Arrow', 25], ['Potion', 8]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up amount #1',
   [[['Potion', 17], ['arrow', 1], ['Arrow', 0], ['arrow', 19]], 'arrow', 45, 20],
   {'slots': [['Potion', 17], ['arrow', 20], ['Arrow', 0], ['arrow', 20]], 'leftover': 25}),
  ('fault site top-up amount #2',
   [[['Arrow', 5]], 'Arrow', 20, 20],
   {'slots': [['Arrow', 20]], 'leftover': 5}),
  ('partial repair boundary #1',
   [[['arrow', 1], None, ['Potion', 5]], 'arrow', 7, 20],
   {'slots': [['arrow', 8], None, ['Potion', 5]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[['arrow', 20], ['Arrow', 0], ['Potion', 19]], 'Arrow', 7, 10],
   {'slots': [['arrow', 20], ['Arrow', 7], ['Potion', 19]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up amount #1',
   [[['arrow', 25], None, ['Potion', 5], None, ['arrow', 20]], 'Potion', 7, 10],
   {'slots': [['arrow', 25], ['Potion', 2], ['Potion', 10], None, ['arrow', 20]], 'leftover': 0}),
  ('fault site top-up amount #2',
   [[None, None, ['Arrow', 11], ['Potion', 10], ['Arrow', 25], None], 'Potion', 20, 20],
   {'slots': [['Potion', 10], None, ['Arrow', 11], ['Potion', 20], ['Arrow', 25], None], 'leftover': 0}),
  ('partial repair boundary #1',
   [[['arrow', 5], ['Potion', 5], ['Potion', 1], ['Potion', 1], ['Arrow', 3]], 'arrow', 1, 20],
   {'slots': [['arrow', 6], ['Potion', 5], ['Potion', 1], ['Potion', 1], ['Arrow', 3]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[['arrow', 20], ['Arrow', 16], ['arrow', 20], None, None, ['Potion', 0]], 'Potion', 1, 20],
   {'slots': [['arrow', 20], ['Arrow', 16], ['arrow', 20], None, None, ['Potion', 1]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[None, ['Arrow', 16], ['arrow', 20], None, ['Arrow', 25], None], 'Potion', 20, 10],
   {'slots': [['Potion', 10], ['Arrow', 16], ['arrow', 20], ['Potion', 10], ['Arrow', 25], None],
    'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up amount #1',
   [[['Potion', 0], ['Arrow', 0], ['Arrow', 1]], 'Arrow', 45, 10],
   {'slots': [['Potion', 0], ['Arrow', 10], ['Arrow', 10]], 'leftover': 26}),
  ('fault site top-up amount #2',
   [[None, ['Potion', 11], ['arrow', 5], ['Potion', 19], None, ['Arrow', 0]], 'arrow', 45, 10],
   {'slots': [['arrow', 10], ['Potion', 11], ['arrow', 10], ['Potion', 19], ['arrow', 10], ['Arrow', 0]],
    'leftover': 20}),
  ('partial repair boundary #1',
   [[['Arrow', 5], None, ['Arrow', 20], ['arrow', 0], ['Potion', 0]], 'arrow', 7, 20],
   {'slots': [['Arrow', 5], None, ['Arrow', 20], ['arrow', 7], ['Potion', 0]], 'leftover': 0}),
  ('partial repair boundary #2',
   [[None, None, ['arrow', 14], ['Arrow', 0], None, None], 'Arrow', 1, 5],
   {'slots': [None, None, ['arrow', 14], ['Arrow', 1], None, None], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1', [[['Potion', 0]], 'Arrow', 45, 5], {'slots': [['Potion', 0]], 'leftover': 45})]]
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:
Partial stacks overflow past max_stack.

slots = list of None or [name, count]; names are case sensitive. First top up partial stacks (count < max_stack) of the same item in slot order, then fill empty slots in order with new stacks of at most max_stack. Reserved placeholders [name, 0] count as partial stacks. Legacy stacks above max_stack are left untouched. Returns {slots, leftover}.

split: open-access


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

N = 1
observations = []
def solve(slots, item, qty, max_stack):
    slots = [s if s is None else list(s) for s in slots]
    left = qty
    for s in slots:
        if left == 0:
            break
        if s is not None and s[1] < max_stack:
            add = min(left, max_stack - s[1])
            s[1] += add
            left -= add
    for i in range(len(slots)):
        if left == 0:
            break
        if slots[i] is None:
            add = min(left, max_stack)
            slots[i] = [item, add]
            left -= add
    return {'slots': slots, 'leftover': left}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('fault site item identity match #1',
   [[['Potion', 0]], 'Arrow', 45, 5],
   {'slots': [['Potion', 0]], 'leftover': 45}),
  ('regression item identity match #1',
   [[None, ['Arrow', 1], ['arrow', 1], ['Potion', 20], ['arrow', 19]], 'arrow', 20, 20],
   {'slots': [None, ['Arrow', 1], ['arrow', 20], ['Potion', 20], ['arrow', 20]], 'leftover': 0}),
  ('regression item identity match #2',
   [[['Arrow', 5], None], 'arrow', 45, 10],
   {'slots': [['Arrow', 5], ['arrow', 10]], 'leftover': 35}),
  ('regression item identity match #3',
   [[['Arrow', 0], ['Arrow', 0], ['Arrow', 1], ['Arrow', 1]], 'arrow', 20, 20],
   {'slots': [['Arrow', 0], ['Arrow', 0], ['Arrow', 1], ['Arrow', 1]], 'leftover': 20}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('regression item identity match #1',
   [[None, ['Arrow', 1], ['arrow', 1], ['Potion', 20], ['arrow', 19]], 'arrow', 20, 20],
   {'slots': [None, ['Arrow', 1], ['arrow', 20], ['Potion', 20], ['arrow', 20]], 'leftover': 0}),
  ('fault site item identity match #1',
   [[['arrow', 0], ['arrow', 5], ['Potion', 18]], 'Potion', 45, 5],
   {'slots': [['arrow', 0], ['arrow', 5], ['Potion', 18]], 'leftover': 45}),
  ('regression item identity match #2',
   [[['Arrow', 5], None], 'arrow', 45, 10],
   {'slots': [['Arrow', 5], ['arrow', 10]], 'leftover': 35}),
  ('regression item identity match #3',
   [[['Arrow', 0], ['Arrow', 0], ['Arrow', 1], ['Arrow', 1]], 'arrow', 20, 20],
   {'slots': [['Arrow', 0], ['Arrow', 0], ['Arrow', 1], ['Arrow', 1]], 'leftover': 20}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('fault site item identity match #1',
   [[['arrow', 0]], 'Potion', 45, 5],
   {'slots': [['arrow', 0]], 'leftover': 45}),
  ('regression item identity match #1',
   [[['Arrow', 5], None], 'arrow', 45, 10],
   {'slots': [['Arrow', 5], ['arrow', 10]], 'leftover': 35}),
  ('regression item identity match #2',
   [[['Arrow', 19], None, None, None, ['Potion', 20]], 'arrow', 20, 20],
   {'slots': [['Arrow', 19], ['arrow', 20], None, None, ['Potion', 20]], 'leftover': 0}),
  ('regression item identity match #3',
   [[['Potion', 20], ['Arrow', 1], ['Potion', 10], ['Potion', 1]], 'arrow', 7, 20],
   {'slots': [['Potion', 20], ['Arrow', 1], ['Potion', 10], ['Potion', 1]], 'leftover': 7}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('regression item identity match #1',
   [[['Arrow', 0], ['Arrow', 0], ['Arrow', 1], ['Arrow', 1]], 'arrow', 20, 20],
   {'slots': [['Arrow', 0], ['Arrow', 0], ['Arrow', 1], ['Arrow', 1]], 'leftover': 20}),
  ('fault site item identity match #1',
   [[['arrow', 20], None, ['Potion', 4], ['Arrow', 20], ['arrow', 19]], 'arrow', 45, 10],
   {'slots': [['arrow', 20], ['arrow', 10], ['Potion', 4], ['Arrow', 20], ['arrow', 19]], 'leftover': 35}),
  ('regression item identity match #2',
   [[['Potion', 17], ['arrow', 1], ['Arrow', 0], ['arrow', 19]], 'arrow', 45, 20],
   {'slots': [['Potion', 17], ['arrow', 20], ['Arrow', 0], ['arrow', 20]], 'leftover': 25}),
  ('regression item identity match #3',
   [[['arrow', 5], ['Arrow', 1]], 'Arrow', 1, 20],
   {'slots': [['arrow', 5], ['Arrow', 2]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('control #1',
   [[None, ['Arrow', 16], ['arrow', 20], None, ['Arrow', 25], None], 'Potion', 20, 10],
   {'slots': [['Potion', 10], ['Arrow', 16], ['arrow', 20], ['Potion', 10], ['Arrow', 25], None],
    'leftover': 0})],
 [('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('regression item identity match #1',
   [[['Arrow', 19], None, None, None, ['Potion', 20]], 'arrow', 20, 20],
   {'slots': [['Arrow', 19], ['arrow', 20], None, None, ['Potion', 20]], 'leftover': 0}),
  ('regression item identity match #2',
   [[['Potion', 20], ['Arrow', 1], ['Potion', 10], ['Potion', 1]], 'arrow', 7, 20],
   {'slots': [['Potion', 20], ['Arrow', 1], ['Potion', 10], ['Potion', 1]], 'leftover': 7}),
  ('regression item identity match #3',
   [[['arrow', 0], None], 'Arrow', 45, 5],
   {'slots': [['arrow', 0], ['Arrow', 5]], 'leftover': 40}),
  ('regression item identity match #4',
   [[None, ['arrow', 5], None, None, ['Potion', 5], None], 'Arrow', 20, 10],
   {'slots': [['Arrow', 10], ['arrow', 5], ['Arrow', 10], None, ['Potion', 5], None], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('control #1',
   [[['Potion', 20], None, ['Arrow', 13], ['arrow', 20]], 'Potion', 20, 5],
   {'slots': [['Potion', 20], ['Potion', 5], ['Arrow', 13], ['arrow', 20]], 'leftover': 15})]]
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(slots, item, qty, max_stack):
    slots = [s if s is None else list(s) for s in slots]
    left = qty
    for s in slots:
        if left == 0:
            break
        if s is not None and s[0].lower() == item.lower() and s[1] < max_stack:
            add = min(left, max_stack - s[1])
            s[1] += add
            left -= add
    for i in range(len(slots)):
        if left == 0:
            break
        if slots[i] is None:
            add = min(left, max_stack)
            slots[i] = [item, add]
            left -= add
    return {'slots': slots, 'leftover': left}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('fault site item identity match #1',
   [[['Potion', 0]], 'Arrow', 45, 5],
   {'slots': [['Potion', 0]], 'leftover': 45}),
  ('regression item identity match #1',
   [[None, ['Arrow', 1], ['arrow', 1], ['Potion', 20], ['arrow', 19]], 'arrow', 20, 20],
   {'slots': [None, ['Arrow', 1], ['arrow', 20], ['Potion', 20], ['arrow', 20]], 'leftover': 0}),
  ('regression item identity match #2',
   [[['Arrow', 5], None], 'arrow', 45, 10],
   {'slots': [['Arrow', 5], ['arrow', 10]], 'leftover': 35}),
  ('regression item identity match #3',
   [[['Arrow', 0], ['Arrow', 0], ['Arrow', 1], ['Arrow', 1]], 'arrow', 20, 20],
   {'slots': [['Arrow', 0], ['Arrow', 0], ['Arrow', 1], ['Arrow', 1]], 'leftover': 20}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('regression item identity match #1',
   [[None, ['Arrow', 1], ['arrow', 1], ['Potion', 20], ['arrow', 19]], 'arrow', 20, 20],
   {'slots': [None, ['Arrow', 1], ['arrow', 20], ['Potion', 20], ['arrow', 20]], 'leftover': 0}),
  ('fault site item identity match #1',
   [[['arrow', 0], ['arrow', 5], ['Potion', 18]], 'Potion', 45, 5],
   {'slots': [['arrow', 0], ['arrow', 5], ['Potion', 18]], 'leftover': 45}),
  ('regression item identity match #2',
   [[['Arrow', 5], None], 'arrow', 45, 10],
   {'slots': [['Arrow', 5], ['arrow', 10]], 'leftover': 35}),
  ('regression item identity match #3',
   [[['Arrow', 0], ['Arrow', 0], ['Arrow', 1], ['Arrow', 1]], 'arrow', 20, 20],
   {'slots': [['Arrow', 0], ['Arrow', 0], ['Arrow', 1], ['Arrow', 1]], 'leftover': 20}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('fault site item identity match #1',
   [[['arrow', 0]], 'Potion', 45, 5],
   {'slots': [['arrow', 0]], 'leftover': 45}),
  ('regression item identity match #1',
   [[['Arrow', 5], None], 'arrow', 45, 10],
   {'slots': [['Arrow', 5], ['arrow', 10]], 'leftover': 35}),
  ('regression item identity match #2',
   [[['Arrow', 19], None, None, None, ['Potion', 20]], 'arrow', 20, 20],
   {'slots': [['Arrow', 19], ['arrow', 20], None, None, ['Potion', 20]], 'leftover': 0}),
  ('regression item identity match #3',
   [[['Potion', 20], ['Arrow', 1], ['Potion', 10], ['Potion', 1]], 'arrow', 7, 20],
   {'slots': [['Potion', 20], ['Arrow', 1], ['Potion', 10], ['Potion', 1]], 'leftover': 7}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('regression item identity match #1',
   [[['Arrow', 0], ['Arrow', 0], ['Arrow', 1], ['Arrow', 1]], 'arrow', 20, 20],
   {'slots': [['Arrow', 0], ['Arrow', 0], ['Arrow', 1], ['Arrow', 1]], 'leftover': 20}),
  ('fault site item identity match #1',
   [[['arrow', 20], None, ['Potion', 4], ['Arrow', 20], ['arrow', 19]], 'arrow', 45, 10],
   {'slots': [['arrow', 20], ['arrow', 10], ['Potion', 4], ['Arrow', 20], ['arrow', 19]], 'leftover': 35}),
  ('regression item identity match #2',
   [[['Potion', 17], ['arrow', 1], ['Arrow', 0], ['arrow', 19]], 'arrow', 45, 20],
   {'slots': [['Potion', 17], ['arrow', 20], ['Arrow', 0], ['arrow', 20]], 'leftover': 25}),
  ('regression item identity match #3',
   [[['arrow', 5], ['Arrow', 1]], 'Arrow', 1, 20],
   {'slots': [['arrow', 5], ['Arrow', 2]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('control #1',
   [[None, ['Arrow', 16], ['arrow', 20], None, ['Arrow', 25], None], 'Potion', 20, 10],
   {'slots': [['Potion', 10], ['Arrow', 16], ['arrow', 20], ['Potion', 10], ['Arrow', 25], None],
    'leftover': 0})],
 [('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('regression item identity match #1',
   [[['Arrow', 19], None, None, None, ['Potion', 20]], 'arrow', 20, 20],
   {'slots': [['Arrow', 19], ['arrow', 20], None, None, ['Potion', 20]], 'leftover': 0}),
  ('regression item identity match #2',
   [[['Potion', 20], ['Arrow', 1], ['Potion', 10], ['Potion', 1]], 'arrow', 7, 20],
   {'slots': [['Potion', 20], ['Arrow', 1], ['Potion', 10], ['Potion', 1]], 'leftover': 7}),
  ('regression item identity match #3',
   [[['arrow', 0], None], 'Arrow', 45, 5],
   {'slots': [['arrow', 0], ['Arrow', 5]], 'leftover': 40}),
  ('regression item identity match #4',
   [[None, ['arrow', 5], None, None, ['Potion', 5], None], 'Arrow', 20, 10],
   {'slots': [['Arrow', 10], ['arrow', 5], ['Arrow', 10], None, ['Potion', 5], None], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('control #1',
   [[['Potion', 20], None, ['Arrow', 13], ['arrow', 20]], 'Potion', 20, 5],
   {'slots': [['Potion', 20], ['Potion', 5], ['Arrow', 13], ['arrow', 20]], 'leftover': 15})]]
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:
Arrows are added onto potion stacks.

slots = list of None or [name, count]; names are case sensitive. First top up partial stacks (count < max_stack) of the same item in slot order, then fill empty slots in order with new stacks of at most max_stack. Reserved placeholders [name, 0] count as partial stacks. Legacy stacks above max_stack are left untouched. Returns {slots, leftover}.

split: open-access


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

N = 1
observations = []
def solve(slots, item, qty, max_stack):
    slots = [s if s is None else list(s) for s in slots]
    left = qty
    for s in reversed(slots):
        if left == 0:
            break
        if s is not None and s[0] == item and s[1] < max_stack:
            add = min(left, max_stack - s[1])
            s[1] += add
            left -= add
    for i in range(len(slots)):
        if left == 0:
            break
        if slots[i] is None:
            add = min(left, max_stack)
            slots[i] = [item, add]
            left -= add
    return {'slots': slots, 'leftover': left}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up order #1',
   [[None, ['Potion', 1], ['Potion', 25], None, ['Arrow', 19], ['Arrow', 15]], 'Arrow', 1, 20],
   {'slots': [None, ['Potion', 1], ['Potion', 25], None, ['Arrow', 20], ['Arrow', 15]], 'leftover': 0}),
  ('fault site top-up order #2',
   [[None, ['Arrow', 0], ['Potion', 19], None, ['Arrow', 14], ['Potion', 1]], 'Potion', 1, 20],
   {'slots': [None, ['Arrow', 0], ['Potion', 20], None, ['Arrow', 14], ['Potion', 1]], 'leftover': 0}),
  ('regression top-up order #1',
   [[['arrow', 0], ['arrow', 19]], 'arrow', 7, 20],
   {'slots': [['arrow', 7], ['arrow', 19]], 'leftover': 0}),
  ('regression top-up order #2',
   [[['Arrow', 5], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'Arrow', 1, 20],
   {'slots': [['Arrow', 6], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up order #1',
   [[None, ['Arrow', 0], ['Potion', 19], None, ['Arrow', 14], ['Potion', 1]], 'Potion', 1, 20],
   {'slots': [None, ['Arrow', 0], ['Potion', 20], None, ['Arrow', 14], ['Potion', 1]], 'leftover': 0}),
  ('fault site top-up order #2',
   [[['Potion', 8], ['Potion', 20], ['arrow', 1], ['Potion', 0], ['arrow', 20], None], 'Potion', 7, 10],
   {'slots': [['Potion', 10], ['Potion', 20], ['arrow', 1], ['Potion', 5], ['arrow', 20], None],
    'leftover': 0}),
  ('regression top-up order #1',
   [[['arrow', 0], ['arrow', 19]], 'arrow', 7, 20],
   {'slots': [['arrow', 7], ['arrow', 19]], 'leftover': 0}),
  ('regression top-up order #2',
   [[['Arrow', 5], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'Arrow', 1, 20],
   {'slots': [['Arrow', 6], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up order #1',
   [[['arrow', 25], ['arrow', 4], ['Arrow', 0], ['arrow', 0], ['arrow', 25]], 'arrow', 1, 10],
   {'slots': [['arrow', 25], ['arrow', 5], ['Arrow', 0], ['arrow', 0], ['arrow', 25]], 'leftover': 0}),
  ('fault site top-up order #2',
   [[['Potion', 0], ['Arrow', 1], ['Arrow', 0]], 'Arrow', 7, 20],
   {'slots': [['Potion', 0], ['Arrow', 8], ['Arrow', 0]], 'leftover': 0}),
  ('regression top-up order #1',
   [[['arrow', 0], ['arrow', 19]], 'arrow', 7, 20],
   {'slots': [['arrow', 7], ['arrow', 19]], 'leftover': 0}),
  ('regression top-up order #2',
   [[['Arrow', 5], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'Arrow', 1, 20],
   {'slots': [['Arrow', 6], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up order #1',
   [[['Arrow', 19], ['Arrow', 5]], 'Arrow', 7, 20],
   {'slots': [['Arrow', 20], ['Arrow', 11]], 'leftover': 0}),
  ('fault site top-up order #2',
   [[['arrow', 3], None, ['arrow', 0]], 'arrow', 1, 10],
   {'slots': [['arrow', 4], None, ['arrow', 0]], 'leftover': 0}),
  ('regression top-up order #1',
   [[['arrow', 0], ['arrow', 19]], 'arrow', 7, 20],
   {'slots': [['arrow', 7], ['arrow', 19]], 'leftover': 0}),
  ('regression top-up order #2',
   [[['Arrow', 5], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'Arrow', 1, 20],
   {'slots': [['Arrow', 6], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[None, ['Arrow', 16], ['arrow', 20], None, ['Arrow', 25], None], 'Potion', 20, 10],
   {'slots': [['Potion', 10], ['Arrow', 16], ['arrow', 20], ['Potion', 10], ['Arrow', 25], None],
    'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('regression top-up order #1',
   [[['arrow', 0], ['arrow', 19]], 'arrow', 7, 20],
   {'slots': [['arrow', 7], ['arrow', 19]], 'leftover': 0}),
  ('regression top-up order #2',
   [[['Arrow', 5], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'Arrow', 1, 20],
   {'slots': [['Arrow', 6], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1', [[None], 'Arrow', 11, 20], {'slots': [['Arrow', 11]], 'leftover': 0}),
  ('control #2', [[['Potion', 20]], 'Arrow', 0, 20], {'slots': [['Potion', 20]], 'leftover': 0}),
  ('control #3', [[], 'arrow', 45, 5], {'slots': [], 'leftover': 45})]]
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(slots, item, qty, max_stack):
    slots = [s if s is None else list(s) for s in slots]
    left = qty
    for s in sorted((s for s in slots if s), key=lambda s: -s[1]):
        if left == 0:
            break
        if s is not None and s[0] == item and s[1] < max_stack:
            add = min(left, max_stack - s[1])
            s[1] += add
            left -= add
    for i in range(len(slots)):
        if left == 0:
            break
        if slots[i] is None:
            add = min(left, max_stack)
            slots[i] = [item, add]
            left -= add
    return {'slots': slots, 'leftover': left}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up order #1',
   [[None, ['Potion', 1], ['Potion', 25], None, ['Arrow', 19], ['Arrow', 15]], 'Arrow', 1, 20],
   {'slots': [None, ['Potion', 1], ['Potion', 25], None, ['Arrow', 20], ['Arrow', 15]], 'leftover': 0}),
  ('fault site top-up order #2',
   [[None, ['Arrow', 0], ['Potion', 19], None, ['Arrow', 14], ['Potion', 1]], 'Potion', 1, 20],
   {'slots': [None, ['Arrow', 0], ['Potion', 20], None, ['Arrow', 14], ['Potion', 1]], 'leftover': 0}),
  ('regression top-up order #1',
   [[['arrow', 0], ['arrow', 19]], 'arrow', 7, 20],
   {'slots': [['arrow', 7], ['arrow', 19]], 'leftover': 0}),
  ('regression top-up order #2',
   [[['Arrow', 5], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'Arrow', 1, 20],
   {'slots': [['Arrow', 6], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up order #1',
   [[None, ['Arrow', 0], ['Potion', 19], None, ['Arrow', 14], ['Potion', 1]], 'Potion', 1, 20],
   {'slots': [None, ['Arrow', 0], ['Potion', 20], None, ['Arrow', 14], ['Potion', 1]], 'leftover': 0}),
  ('fault site top-up order #2',
   [[['Potion', 8], ['Potion', 20], ['arrow', 1], ['Potion', 0], ['arrow', 20], None], 'Potion', 7, 10],
   {'slots': [['Potion', 10], ['Potion', 20], ['arrow', 1], ['Potion', 5], ['arrow', 20], None],
    'leftover': 0}),
  ('regression top-up order #1',
   [[['arrow', 0], ['arrow', 19]], 'arrow', 7, 20],
   {'slots': [['arrow', 7], ['arrow', 19]], 'leftover': 0}),
  ('regression top-up order #2',
   [[['Arrow', 5], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'Arrow', 1, 20],
   {'slots': [['Arrow', 6], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up order #1',
   [[['arrow', 25], ['arrow', 4], ['Arrow', 0], ['arrow', 0], ['arrow', 25]], 'arrow', 1, 10],
   {'slots': [['arrow', 25], ['arrow', 5], ['Arrow', 0], ['arrow', 0], ['arrow', 25]], 'leftover': 0}),
  ('fault site top-up order #2',
   [[['Potion', 0], ['Arrow', 1], ['Arrow', 0]], 'Arrow', 7, 20],
   {'slots': [['Potion', 0], ['Arrow', 8], ['Arrow', 0]], 'leftover': 0}),
  ('regression top-up order #1',
   [[['arrow', 0], ['arrow', 19]], 'arrow', 7, 20],
   {'slots': [['arrow', 7], ['arrow', 19]], 'leftover': 0}),
  ('regression top-up order #2',
   [[['Arrow', 5], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'Arrow', 1, 20],
   {'slots': [['Arrow', 6], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('fault site top-up order #1',
   [[['Arrow', 19], ['Arrow', 5]], 'Arrow', 7, 20],
   {'slots': [['Arrow', 20], ['Arrow', 11]], 'leftover': 0}),
  ('fault site top-up order #2',
   [[['arrow', 3], None, ['arrow', 0]], 'arrow', 1, 10],
   {'slots': [['arrow', 4], None, ['arrow', 0]], 'leftover': 0}),
  ('regression top-up order #1',
   [[['arrow', 0], ['arrow', 19]], 'arrow', 7, 20],
   {'slots': [['arrow', 7], ['arrow', 19]], 'leftover': 0}),
  ('regression top-up order #2',
   [[['Arrow', 5], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'Arrow', 1, 20],
   {'slots': [['Arrow', 6], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[None, ['Arrow', 16], ['arrow', 20], None, ['Arrow', 25], None], 'Potion', 20, 10],
   {'slots': [['Potion', 10], ['Arrow', 16], ['arrow', 20], ['Potion', 10], ['Arrow', 25], None],
    'leftover': 0})],
 [('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('regression top-up order #1',
   [[['arrow', 0], ['arrow', 19]], 'arrow', 7, 20],
   {'slots': [['arrow', 7], ['arrow', 19]], 'leftover': 0}),
  ('regression top-up order #2',
   [[['Arrow', 5], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'Arrow', 1, 20],
   {'slots': [['Arrow', 6], ['arrow', 1], ['arrow', 25], None, ['Arrow', 19]], 'leftover': 0}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1', [[None], 'Arrow', 11, 20], {'slots': [['Arrow', 11]], 'leftover': 0}),
  ('control #2', [[['Potion', 20]], 'Arrow', 0, 20], {'slots': [['Potion', 20]], 'leftover': 0}),
  ('control #3', [[], 'arrow', 45, 5], {'slots': [], 'leftover': 45})]]
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:
Loot lands in the last partial stack instead of the first.

slots = list of None or [name, count]; names are case sensitive. First top up partial stacks (count < max_stack) of the same item in slot order, then fill empty slots in order with new stacks of at most max_stack. Reserved placeholders [name, 0] count as partial stacks. Legacy stacks above max_stack are left untouched. Returns {slots, leftover}.

split: open-access


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

N = 1
observations = []
def solve(slots, item, qty, max_stack):
    slots = [s if s is None else list(s) for s in slots]
    left = qty
    for s in slots:
        if left == 0:
            break
        if s is not None and s[0] == item and s[1] < max_stack:
            add = min(left, max_stack - s[1])
            s[1] += add
            left -= add
    for i in range(len(slots)):
        if left == 0:
            break
        if slots[i] is None:
            add = left
            slots[i] = [item, add]
            left -= add
    return {'slots': slots, 'leftover': left}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression new stack size #1',
   [[None, ['Arrow', 16], ['arrow', 20], None, ['Arrow', 25], None], 'Potion', 20, 10],
   {'slots': [['Potion', 10], ['Arrow', 16], ['arrow', 20], ['Potion', 10], ['Arrow', 25], None],
    'leftover': 0}),
  ('regression new stack size #2',
   [[['Potion', 20], None, ['Arrow', 13], ['arrow', 20]], 'Potion', 20, 5],
   {'slots': [['Potion', 20], ['Potion', 5], ['Arrow', 13], ['arrow', 20]], 'leftover': 15}),
  ('regression new stack size #3',
   [[['Arrow', 5], None], 'arrow', 45, 10],
   {'slots': [['Arrow', 5], ['arrow', 10]], 'leftover': 35}),
  ('regression new stack size #4',
   [[None, ['arrow', 5], None, None], 'arrow', 45, 10],
   {'slots': [['arrow', 10], ['arrow', 10], ['arrow', 10], ['arrow', 10]], 'leftover': 10}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('regression new stack size #1',
   [[['Arrow', 5], None], 'arrow', 45, 10],
   {'slots': [['Arrow', 5], ['arrow', 10]], 'leftover': 35}),
  ('regression new stack size #2',
   [[None, ['arrow', 5], None, None], 'arrow', 45, 10],
   {'slots': [['arrow', 10], ['arrow', 10], ['arrow', 10], ['arrow', 10]], 'leftover': 10}),
  ('regression new stack size #3',
   [[None, ['arrow', 5], None], 'Arrow', 20, 5],
   {'slots': [['Arrow', 5], ['arrow', 5], ['Arrow', 5]], 'leftover': 10}),
  ('regression new stack size #4', [[None], 'Arrow', 45, 10], {'slots': [['Arrow', 10]], 'leftover': 35}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0}),
  ('control #2', [[['Potion', 0]], 'Arrow', 45, 5], {'slots': [['Potion', 0]], 'leftover': 45})],
 [('regression new stack size #1',
   [[None, ['arrow', 5], None], 'Arrow', 20, 5],
   {'slots': [['Arrow', 5], ['arrow', 5], ['Arrow', 5]], 'leftover': 10}),
  ('regression new stack size #2', [[None], 'Arrow', 45, 10], {'slots': [['Arrow', 10]], 'leftover': 35}),
  ('regression new stack size #3', [[None], 'Arrow', 20, 5], {'slots': [['Arrow', 5]], 'leftover': 15}),
  ('regression new stack size #4',
   [[['Potion', 25], None], 'arrow', 45, 10],
   {'slots': [['Potion', 25], ['arrow', 10]], 'leftover': 35}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1', [[['Potion', 0]], 'Arrow', 45, 5], {'slots': [['Potion', 0]], 'leftover': 45}),
  ('control #2',
   [[['arrow', 0], ['arrow', 19], None, ['arrow', 20], None, None], 'Arrow', 0, 5],
   {'slots': [['arrow', 0], ['arrow', 19], None, ['arrow', 20], None, None], 'leftover': 0})],
 [('regression new stack size #1', [[None], 'Arrow', 20, 5], {'slots': [['Arrow', 5]], 'leftover': 15}),
  ('regression new stack size #2',
   [[['Potion', 25], None], 'arrow', 45, 10],
   {'slots': [['Potion', 25], ['arrow', 10]], 'leftover': 35}),
  ('regression new stack size #3',
   [[None, ['arrow', 19]], 'Potion', 45, 5],
   {'slots': [['Potion', 5], ['arrow', 19]], 'leftover': 40}),
  ('regression new stack size #4',
   [[None, ['arrow', 1], ['Arrow', 19], ['arrow', 10], None], 'arrow', 20, 5],
   {'slots': [['arrow', 5], ['arrow', 5], ['Arrow', 19], ['arrow', 10], ['arrow', 5]], 'leftover': 6}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('control #1',
   [[None, ['Arrow', 1], ['arrow', 1], ['Potion', 20], ['arrow', 19]], 'arrow', 20, 20],
   {'slots': [None, ['Arrow', 1], ['arrow', 20], ['Potion', 20], ['arrow', 20]], 'leftover': 0}),
  ('control #2',
   [[['arrow', 0], ['arrow', 5], ['Potion', 18]], 'Potion', 45, 5],
   {'slots': [['arrow', 0], ['arrow', 5], ['Potion', 18]], 'leftover': 45})],
 [('regression new stack size #1',
   [[None, ['arrow', 19]], 'Potion', 45, 5],
   {'slots': [['Potion', 5], ['arrow', 19]], 'leftover': 40}),
  ('regression new stack size #2',
   [[None, ['arrow', 1], ['Arrow', 19], ['arrow', 10], None], 'arrow', 20, 5],
   {'slots': [['arrow', 5], ['arrow', 5], ['Arrow', 19], ['arrow', 10], ['arrow', 5]], 'leftover': 6}),
  ('regression new stack size #3',
   [[['arrow', 20], None, ['Potion', 4], ['Arrow', 20], ['arrow', 19]], 'arrow', 45, 10],
   {'slots': [['arrow', 20], ['arrow', 10], ['Potion', 4], ['Arrow', 20], ['arrow', 19]], 'leftover': 35}),
  ('partial repair boundary #1',
   [[['Arrow', 19], None, None, None, ['Potion', 20]], 'arrow', 20, 20],
   {'slots': [['Arrow', 19], ['arrow', 20], None, None, ['Potion', 20]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('control #1', [[['arrow', 0]], 'Potion', 45, 5], {'slots': [['arrow', 0]], 'leftover': 45}),
  ('control #2', [[], 'Potion', 20, 5], {'slots': [], 'leftover': 20})]]
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(slots, item, qty, max_stack):
    slots = [s if s is None else list(s) for s in slots]
    left = qty
    for s in slots:
        if left == 0:
            break
        if s is not None and s[0] == item and s[1] < max_stack:
            add = min(left, max_stack - s[1])
            s[1] += add
            left -= add
    for i in range(len(slots)):
        if left == 0:
            break
        if slots[i] is None:
            add = min(left, max_stack - 1)
            slots[i] = [item, add]
            left -= add
    return {'slots': slots, 'leftover': left}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('regression new stack size #1',
   [[None, ['Arrow', 16], ['arrow', 20], None, ['Arrow', 25], None], 'Potion', 20, 10],
   {'slots': [['Potion', 10], ['Arrow', 16], ['arrow', 20], ['Potion', 10], ['Arrow', 25], None],
    'leftover': 0}),
  ('regression new stack size #2',
   [[['Potion', 20], None, ['Arrow', 13], ['arrow', 20]], 'Potion', 20, 5],
   {'slots': [['Potion', 20], ['Potion', 5], ['Arrow', 13], ['arrow', 20]], 'leftover': 15}),
  ('regression new stack size #3',
   [[['Arrow', 5], None], 'arrow', 45, 10],
   {'slots': [['Arrow', 5], ['arrow', 10]], 'leftover': 35}),
  ('regression new stack size #4',
   [[None, ['arrow', 5], None, None], 'arrow', 45, 10],
   {'slots': [['arrow', 10], ['arrow', 10], ['arrow', 10], ['arrow', 10]], 'leftover': 10}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0})],
 [('regression new stack size #1',
   [[['Arrow', 5], None], 'arrow', 45, 10],
   {'slots': [['Arrow', 5], ['arrow', 10]], 'leftover': 35}),
  ('regression new stack size #2',
   [[None, ['arrow', 5], None, None], 'arrow', 45, 10],
   {'slots': [['arrow', 10], ['arrow', 10], ['arrow', 10], ['arrow', 10]], 'leftover': 10}),
  ('regression new stack size #3',
   [[None, ['arrow', 5], None], 'Arrow', 20, 5],
   {'slots': [['Arrow', 5], ['arrow', 5], ['Arrow', 5]], 'leftover': 10}),
  ('regression new stack size #4', [[None], 'Arrow', 45, 10], {'slots': [['Arrow', 10]], 'leftover': 35}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('control #1',
   [[['Arrow', 6], None, ['arrow', 5]], 'Potion', 0, 20],
   {'slots': [['Arrow', 6], None, ['arrow', 5]], 'leftover': 0}),
  ('control #2', [[['Potion', 0]], 'Arrow', 45, 5], {'slots': [['Potion', 0]], 'leftover': 45})],
 [('regression new stack size #1',
   [[None, ['arrow', 5], None], 'Arrow', 20, 5],
   {'slots': [['Arrow', 5], ['arrow', 5], ['Arrow', 5]], 'leftover': 10}),
  ('regression new stack size #2', [[None], 'Arrow', 45, 10], {'slots': [['Arrow', 10]], 'leftover': 35}),
  ('regression new stack size #3', [[None], 'Arrow', 20, 5], {'slots': [['Arrow', 5]], 'leftover': 15}),
  ('regression new stack size #4',
   [[['Potion', 25], None], 'arrow', 45, 10],
   {'slots': [['Potion', 25], ['arrow', 10]], 'leftover': 35}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('control #1', [[['Potion', 0]], 'Arrow', 45, 5], {'slots': [['Potion', 0]], 'leftover': 45}),
  ('control #2',
   [[['arrow', 0], ['arrow', 19], None, ['arrow', 20], None, None], 'Arrow', 0, 5],
   {'slots': [['arrow', 0], ['arrow', 19], None, ['arrow', 20], None, None], 'leftover': 0})],
 [('regression new stack size #1', [[None], 'Arrow', 20, 5], {'slots': [['Arrow', 5]], 'leftover': 15}),
  ('regression new stack size #2',
   [[['Potion', 25], None], 'arrow', 45, 10],
   {'slots': [['Potion', 25], ['arrow', 10]], 'leftover': 35}),
  ('regression new stack size #3',
   [[None, ['arrow', 19]], 'Potion', 45, 5],
   {'slots': [['Potion', 5], ['arrow', 19]], 'leftover': 40}),
  ('regression new stack size #4',
   [[None, ['arrow', 1], ['Arrow', 19], ['arrow', 10], None], 'arrow', 20, 5],
   {'slots': [['arrow', 5], ['arrow', 5], ['Arrow', 19], ['arrow', 10], ['arrow', 5]], 'leftover': 6}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('two partial stacks #1',
   [[['Arrow', 18], ['Arrow', 15], None], 'Arrow', 4, 20],
   {'slots': [['Arrow', 20], ['Arrow', 17], None], 'leftover': 0}),
  ('control #1',
   [[None, ['Arrow', 1], ['arrow', 1], ['Potion', 20], ['arrow', 19]], 'arrow', 20, 20],
   {'slots': [None, ['Arrow', 1], ['arrow', 20], ['Potion', 20], ['arrow', 20]], 'leftover': 0}),
  ('control #2',
   [[['arrow', 0], ['arrow', 5], ['Potion', 18]], 'Potion', 45, 5],
   {'slots': [['arrow', 0], ['arrow', 5], ['Potion', 18]], 'leftover': 45})],
 [('regression new stack size #1',
   [[None, ['arrow', 19]], 'Potion', 45, 5],
   {'slots': [['Potion', 5], ['arrow', 19]], 'leftover': 40}),
  ('regression new stack size #2',
   [[None, ['arrow', 1], ['Arrow', 19], ['arrow', 10], None], 'arrow', 20, 5],
   {'slots': [['arrow', 5], ['arrow', 5], ['Arrow', 19], ['arrow', 10], ['arrow', 5]], 'leftover': 6}),
  ('regression new stack size #3',
   [[['arrow', 20], None, ['Potion', 4], ['Arrow', 20], ['arrow', 19]], 'arrow', 45, 10],
   {'slots': [['arrow', 20], ['arrow', 10], ['Potion', 4], ['Arrow', 20], ['arrow', 19]], 'leftover': 35}),
  ('partial repair boundary #1',
   [[['Arrow', 19], None, None, None, ['Potion', 20]], 'arrow', 20, 20],
   {'slots': [['Arrow', 19], ['arrow', 20], None, None, ['Potion', 20]], 'leftover': 0}),
  ('case distinct item #1', [[['arrow', 3]], 'Arrow', 2, 20], {'slots': [['arrow', 3]], 'leftover': 2}),
  ('legacy overfull stack #1',
   [[['Arrow', 25], None], 'Arrow', 3, 20],
   {'slots': [['Arrow', 25], ['Arrow', 3]], 'leftover': 0}),
  ('control #1', [[['arrow', 0]], 'Potion', 45, 5], {'slots': [['arrow', 0]], 'leftover': 45}),
  ('control #2', [[], 'Potion', 20, 5], {'slots': [], 'leftover': 20})]]
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 single empty slot swallows any quantity.

slots = list of None or [name, count]; names are case sensitive. First top up partial stacks (count < max_stack) of the same item in slot order, then fill empty slots in order with new stacks of at most max_stack. Reserved placeholders [name, 0] count as partial stacks. Legacy stacks above max_stack are left untouched. Returns {slots, leftover}.

split: open-access


prompt:
Freshly made gems do not cascade to the next tier.

gems[t] = count of tier t gems for t = 0..max_tier. Tiers are processed upward: three tier t gems make one tier t+1 gem at a cost of (t+1)*10 gold each, limited by the remaining gold budget; newly made gems can cascade into the next tier. Max-tier gems never combine. Returns [counts, gold_spent].

split: open-access


prompt:
Gems that do not form a full triple are destroyed.

gems[t] = count of tier t gems for t = 0..max_tier. Tiers are processed upward: three tier t gems make one tier t+1 gem at a cost of (t+1)*10 gold each, limited by the remaining gold budget; newly made gems can cascade into the next tier. Max-tier gems never combine. Returns [counts, gold_spent].

split: open-access


prompt:
Exactly three gems stay uncombined.

gems[t] = count of tier t gems for t = 0..max_tier. Tiers are processed upward: three tier t gems make one tier t+1 gem at a cost of (t+1)*10 gold each, limited by the remaining gold budget; newly made gems can cascade into the next tier. Max-tier gems never combine. Returns [counts, gold_spent].

split: open-access


prompt:
A cascade spends more gold than the budget.

gems[t] = count of tier t gems for t = 0..max_tier. Tiers are processed upward: three tier t gems make one tier t+1 gem at a cost of (t+1)*10 gold each, limited by the remaining gold budget; newly made gems can cascade into the next tier. Max-tier gems never combine. Returns [counts, gold_spent].

split: open-access


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

N = 1
observations = []
def solve(amount, from_cur, to_cur, rates, fee_bp):
    if from_cur == to_cur and fee_bp == 0:
        return [amount, 0]
    value = amount * rates[from_cur]
    fee = -(-value * fee_bp // 10000)
    if value > 0 and fee_bp > 0:
        fee = max(fee, 1)
    net = value - fee
    return [net // rates[to_cur], net % rates[to_cur]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('same currency no fee #1',
   [5, 'gold', 'gold', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250}, 150],
   [5, 0]),
  ('fault site same currency shortcut #1',
   [751, 'silver', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [751, 0]),
  ('fault site same currency shortcut #2',
   [1, 'copper', 'copper', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 50],
   [1, 0]),
  ('partial repair boundary #1',
   [3, 'token', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [2, 99]),
  ('partial repair boundary #2',
   [7, 'silver', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 150],
   [6, 89]),
  ('zero amount with fee #1', [0, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 50], [0, 0]),
  ('fee rounds up #1', [3, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 25], [7, 48]),
  ('control #1',
   [1, 'gem', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [2, 49])],
 [('same currency no fee #1',
   [5, 'gold', 'gold', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250}, 150],
   [5, 0]),
  ('fault site same currency shortcut #1',
   [1, 'copper', 'copper', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 50],
   [1, 0]),
  ('fault site same currency shortcut #2',
   [3, 'gem', 'gem', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [3, 0]),
  ('partial repair boundary #1',
   [1, 'token', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [0, 99]),
  ('partial repair boundary #2',
   [1, 'token', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [0, 99]),
  ('zero amount with fee #1', [0, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 50], [0, 0]),
  ('fee rounds up #1', [3, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 25], [7, 48]),
  ('control #1',
   [1, 'gem', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [2, 49])],
 [('same currency no fee #1',
   [5, 'gold', 'gold', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250}, 150],
   [5, 0]),
  ('fault site same currency shortcut #1',
   [3, 'gem', 'gem', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 333],
   [3, 0]),
  ('fault site same currency shortcut #2',
   [7, 'gem', 'gem', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 333],
   [7, 0]),
  ('partial repair boundary #1',
   [40, 'silver', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 150],
   [39, 40]),
  ('partial repair boundary #2',
   [3, 'silver', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [2, 99]),
  ('zero amount with fee #1', [0, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 50], [0, 0]),
  ('fee rounds up #1', [3, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 25], [7, 48]),
  ('control #1',
   [1, 'gem', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [2, 49])],
 [('same currency no fee #1',
   [5, 'gold', 'gold', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250}, 150],
   [5, 0]),
  ('fault site same currency shortcut #1',
   [7, 'silver', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [7, 0]),
  ('fault site same currency shortcut #2',
   [40, 'token', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [40, 0]),
  ('partial repair boundary #1',
   [7, 'silver', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [6, 99]),
  ('partial repair boundary #2',
   [1, 'silver', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [0, 99]),
  ('zero amount with fee #1', [0, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 50], [0, 0]),
  ('fee rounds up #1', [3, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 25], [7, 48]),
  ('control #1',
   [7, 'gold', 'copper', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 333],
   [67669, 0])],
 [('same currency no fee #1',
   [5, 'gold', 'gold', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250}, 150],
   [5, 0]),
  ('fault site same currency shortcut #1',
   [1, 'silver', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [1, 0]),
  ('fault site same currency shortcut #2',
   [1, 'token', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [1, 0]),
  ('partial repair boundary #1',
   [7, 'token', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [6, 99]),
  ('partial repair boundary #2',
   [3, 'silver', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 333],
   [2, 90]),
  ('zero amount with fee #1', [0, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 50], [0, 0]),
  ('fee rounds up #1', [3, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 25], [7, 48]),
  ('control #1',
   [0, 'gem', 'gold', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 333],
   [0, 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(amount, from_cur, to_cur, rates, fee_bp):
    if rates[from_cur] == rates[to_cur]:
        return [amount, 0]
    value = amount * rates[from_cur]
    fee = -(-value * fee_bp // 10000)
    if value > 0 and fee_bp > 0:
        fee = max(fee, 1)
    net = value - fee
    return [net // rates[to_cur], net % rates[to_cur]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = [[('same currency no fee #1',
   [5, 'gold', 'gold', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250}, 150],
   [5, 0]),
  ('fault site same currency shortcut #1',
   [751, 'silver', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [751, 0]),
  ('fault site same currency shortcut #2',
   [1, 'copper', 'copper', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 50],
   [1, 0]),
  ('partial repair boundary #1',
   [3, 'token', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [2, 99]),
  ('partial repair boundary #2',
   [7, 'silver', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 150],
   [6, 89]),
  ('zero amount with fee #1', [0, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 50], [0, 0]),
  ('fee rounds up #1', [3, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 25], [7, 48]),
  ('control #1',
   [1, 'gem', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [2, 49])],
 [('same currency no fee #1',
   [5, 'gold', 'gold', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250}, 150],
   [5, 0]),
  ('fault site same currency shortcut #1',
   [1, 'copper', 'copper', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 50],
   [1, 0]),
  ('fault site same currency shortcut #2',
   [3, 'gem', 'gem', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [3, 0]),
  ('partial repair boundary #1',
   [1, 'token', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [0, 99]),
  ('partial repair boundary #2',
   [1, 'token', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [0, 99]),
  ('zero amount with fee #1', [0, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 50], [0, 0]),
  ('fee rounds up #1', [3, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 25], [7, 48]),
  ('control #1',
   [1, 'gem', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [2, 49])],
 [('same currency no fee #1',
   [5, 'gold', 'gold', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250}, 150],
   [5, 0]),
  ('fault site same currency shortcut #1',
   [3, 'gem', 'gem', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 333],
   [3, 0]),
  ('fault site same currency shortcut #2',
   [7, 'gem', 'gem', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 333],
   [7, 0]),
  ('partial repair boundary #1',
   [40, 'silver', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 150],
   [39, 40]),
  ('partial repair boundary #2',
   [3, 'silver', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [2, 99]),
  ('zero amount with fee #1', [0, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 50], [0, 0]),
  ('fee rounds up #1', [3, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 25], [7, 48]),
  ('control #1',
   [1, 'gem', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [2, 49])],
 [('same currency no fee #1',
   [5, 'gold', 'gold', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250}, 150],
   [5, 0]),
  ('fault site same currency shortcut #1',
   [7, 'silver', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [7, 0]),
  ('fault site same currency shortcut #2',
   [40, 'token', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [40, 0]),
  ('partial repair boundary #1',
   [7, 'silver', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [6, 99]),
  ('partial repair boundary #2',
   [1, 'silver', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [0, 99]),
  ('zero amount with fee #1', [0, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 50], [0, 0]),
  ('fee rounds up #1', [3, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 25], [7, 48]),
  ('control #1',
   [7, 'gold', 'copper', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 333],
   [67669, 0])],
 [('same currency no fee #1',
   [5, 'gold', 'gold', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250}, 150],
   [5, 0]),
  ('fault site same currency shortcut #1',
   [1, 'silver', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [1, 0]),
  ('fault site same currency shortcut #2',
   [1, 'token', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 25],
   [1, 0]),
  ('partial repair boundary #1',
   [7, 'token', 'silver', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 1],
   [6, 99]),
  ('partial repair boundary #2',
   [3, 'silver', 'token', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 333],
   [2, 90]),
  ('zero amount with fee #1', [0, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 50], [0, 0]),
  ('fee rounds up #1', [3, 'gem', 'silver', {'copper': 1, 'silver': 100, 'gem': 250}, 25], [7, 48]),
  ('control #1',
   [0, 'gem', 'gold', {'copper': 1, 'silver': 100, 'gold': 10000, 'gem': 250, 'token': 100}, 333],
   [0, 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:
Converting gold to gold loses money.

rates map currency -> copper value. Same currency returns [amount, 0] without fee. Otherwise value = amount*rate[from]; fee = ceil(value*fee_bp/10000), at least 1 copper when value > 0 and fee_bp > 0 and 0 otherwise; net = value - fee; return [net // rate[to], net % rate[to]] (change in copper).

split: open-access