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

N = 1
observations = []
def solve(x):
    layers = [list(l) for l in x['layers']]
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            if q <= 0:
                continue
            if layers and layers[-1][1] == c:
                layers[-1][0] += q
            else:
                layers.append([q, c])
            continue
        need = ev[1]
        onhand = sum(l[0] for l in layers)
        if need > onhand:
            out.append('short')
            continue
        cost = 0
        while need > 0:
            idx = -1
            take = min(need, layers[idx][0])
            cost += take * layers[idx][1]
            layers[idx][0] -= take
            need -= take
            if layers[idx][0] == 0:
                layers.pop(idx)
        out.append(cost)
    return {'cogs': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [1, 70]], 'events': [['issue', 5]]}, {'cogs': [270], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 4], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 13], ['issue', 4], ['recv', 1, 13]]}, {'cogs': [46], 'layers': [[4, 13]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 1, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[2, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 31], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [2, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': []}], ['price decline', {'layers': [[2, 61], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [122], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [2, 70]], 'events': [['issue', 6]]}, {'cogs': [340], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 5], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 14], ['issue', 4], ['recv', 1, 14]]}, {'cogs': [48], 'layers': [[4, 14]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 2, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[3, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 32], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [3, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[1, 9]]}], ['price decline', {'layers': [[2, 62], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [124], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 7]]}, {'cogs': [740], 'layers': [[8, 120]]}], ['exact depletion', {'layers': [[4, 50], [3, 70]], 'events': [['issue', 7]]}, {'cogs': [410], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 6], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 15], ['issue', 4], ['recv', 1, 15]]}, {'cogs': [50], 'layers': [[4, 15]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 3, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[4, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 33], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [4, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[2, 9]]}], ['price decline', {'layers': [[2, 63], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [126], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [4, 70]], 'events': [['issue', 8]]}, {'cogs': [480], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 7], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 16], ['issue', 4], ['recv', 1, 16]]}, {'cogs': [52], 'layers': [[4, 16]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 4, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[5, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 34], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [5, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[3, 9]]}], ['price decline', {'layers': [[2, 64], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [128], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [5, 70]], 'events': [['issue', 9]]}, {'cogs': [550], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 8], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 17], ['issue', 4], ['recv', 1, 17]]}, {'cogs': [54], 'layers': [[4, 17]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 5, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[6, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 35], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [6, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[4, 9]]}], ['price decline', {'layers': [[2, 65], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [130], 'layers': [[3, 45]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    layers = [list(l) for l in x['layers']]
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            if q <= 0:
                continue
            if layers and layers[-1][1] == c:
                layers[-1][0] += q
            else:
                layers.append([q, c])
            continue
        need = ev[1]
        onhand = sum(l[0] for l in layers)
        if need > onhand:
            out.append('short')
            continue
        cost = 0
        while need > 0:
            idx = min(range(len(layers)), key=lambda i: layers[i][1])
            take = min(need, layers[idx][0])
            cost += take * layers[idx][1]
            layers[idx][0] -= take
            need -= take
            if layers[idx][0] == 0:
                layers.pop(idx)
        out.append(cost)
    return {'cogs': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [1, 70]], 'events': [['issue', 5]]}, {'cogs': [270], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 4], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 13], ['issue', 4], ['recv', 1, 13]]}, {'cogs': [46], 'layers': [[4, 13]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 1, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[2, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 31], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [2, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': []}], ['price decline', {'layers': [[2, 61], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [122], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [2, 70]], 'events': [['issue', 6]]}, {'cogs': [340], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 5], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 14], ['issue', 4], ['recv', 1, 14]]}, {'cogs': [48], 'layers': [[4, 14]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 2, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[3, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 32], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [3, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[1, 9]]}], ['price decline', {'layers': [[2, 62], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [124], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 7]]}, {'cogs': [740], 'layers': [[8, 120]]}], ['exact depletion', {'layers': [[4, 50], [3, 70]], 'events': [['issue', 7]]}, {'cogs': [410], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 6], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 15], ['issue', 4], ['recv', 1, 15]]}, {'cogs': [50], 'layers': [[4, 15]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 3, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[4, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 33], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [4, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[2, 9]]}], ['price decline', {'layers': [[2, 63], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [126], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [4, 70]], 'events': [['issue', 8]]}, {'cogs': [480], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 7], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 16], ['issue', 4], ['recv', 1, 16]]}, {'cogs': [52], 'layers': [[4, 16]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 4, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[5, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 34], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [5, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[3, 9]]}], ['price decline', {'layers': [[2, 64], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [128], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [5, 70]], 'events': [['issue', 9]]}, {'cogs': [550], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 8], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 17], ['issue', 4], ['recv', 1, 17]]}, {'cogs': [54], 'layers': [[4, 17]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 5, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[6, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 35], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [6, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[4, 9]]}], ['price decline', {'layers': [[2, 65], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [130], 'layers': [[3, 45]]}]]]
for label, args, expected in fixtures[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:
Issues are costed at the most recent receipt prices and the oldest layers linger on the balance sheet.

Input {layers:[[qty,unit_cost]] oldest first, events:[["recv",qty,unit_cost]|["issue",qty]]}. Issues consume the oldest layer first; an issue larger than on-hand is rejected whole and reported as "short"; a receipt at exactly the newest layer cost merges into it; receipts of zero or negative quantity are ignored. Return {cogs:[per-issue cost or "short"], layers}.

split: open-access


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

N = 1
observations = []
def solve(x):
    layers = [list(l) for l in x['layers']]
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            if q <= 0:
                continue
            if layers and layers[-1][1] == c:
                layers[-1][0] += q
            else:
                layers.append([q, c])
            continue
        need = ev[1]
        onhand = sum(l[0] for l in layers)
        if need >= onhand:
            out.append('short')
            continue
        cost = 0
        while need > 0:
            idx = 0
            take = min(need, layers[idx][0])
            cost += take * layers[idx][1]
            layers[idx][0] -= take
            need -= take
            if layers[idx][0] == 0:
                layers.pop(idx)
        out.append(cost)
    return {'cogs': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [1, 70]], 'events': [['issue', 5]]}, {'cogs': [270], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 4], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 13], ['issue', 4], ['recv', 1, 13]]}, {'cogs': [46], 'layers': [[4, 13]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 1, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[2, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 31], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [2, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': []}], ['price decline', {'layers': [[2, 61], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [122], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [2, 70]], 'events': [['issue', 6]]}, {'cogs': [340], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 5], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 14], ['issue', 4], ['recv', 1, 14]]}, {'cogs': [48], 'layers': [[4, 14]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 2, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[3, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 32], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [3, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[1, 9]]}], ['price decline', {'layers': [[2, 62], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [124], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 7]]}, {'cogs': [740], 'layers': [[8, 120]]}], ['exact depletion', {'layers': [[4, 50], [3, 70]], 'events': [['issue', 7]]}, {'cogs': [410], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 6], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 15], ['issue', 4], ['recv', 1, 15]]}, {'cogs': [50], 'layers': [[4, 15]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 3, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[4, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 33], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [4, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[2, 9]]}], ['price decline', {'layers': [[2, 63], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [126], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [4, 70]], 'events': [['issue', 8]]}, {'cogs': [480], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 7], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 16], ['issue', 4], ['recv', 1, 16]]}, {'cogs': [52], 'layers': [[4, 16]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 4, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[5, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 34], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [5, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[3, 9]]}], ['price decline', {'layers': [[2, 64], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [128], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [5, 70]], 'events': [['issue', 9]]}, {'cogs': [550], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 8], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 17], ['issue', 4], ['recv', 1, 17]]}, {'cogs': [54], 'layers': [[4, 17]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 5, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[6, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 35], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [6, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[4, 9]]}], ['price decline', {'layers': [[2, 65], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [130], 'layers': [[3, 45]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

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

N = 1
observations = []
def solve(x):
    layers = [list(l) for l in x['layers']]
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            if q <= 0:
                continue
            if layers and layers[-1][1] == c:
                layers[-1][0] += q
            else:
                layers.append([q, c])
            continue
        need = ev[1]
        onhand = sum(l[0] for l in layers)
        if need >= onhand and need > 0:
            out.append('short')
            continue
        cost = 0
        while need > 0:
            idx = 0
            take = min(need, layers[idx][0])
            cost += take * layers[idx][1]
            layers[idx][0] -= take
            need -= take
            if layers[idx][0] == 0:
                layers.pop(idx)
        out.append(cost)
    return {'cogs': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [1, 70]], 'events': [['issue', 5]]}, {'cogs': [270], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 4], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 13], ['issue', 4], ['recv', 1, 13]]}, {'cogs': [46], 'layers': [[4, 13]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 1, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[2, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 31], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [2, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': []}], ['price decline', {'layers': [[2, 61], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [122], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [2, 70]], 'events': [['issue', 6]]}, {'cogs': [340], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 5], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 14], ['issue', 4], ['recv', 1, 14]]}, {'cogs': [48], 'layers': [[4, 14]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 2, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[3, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 32], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [3, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[1, 9]]}], ['price decline', {'layers': [[2, 62], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [124], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 7]]}, {'cogs': [740], 'layers': [[8, 120]]}], ['exact depletion', {'layers': [[4, 50], [3, 70]], 'events': [['issue', 7]]}, {'cogs': [410], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 6], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 15], ['issue', 4], ['recv', 1, 15]]}, {'cogs': [50], 'layers': [[4, 15]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 3, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[4, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 33], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [4, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[2, 9]]}], ['price decline', {'layers': [[2, 63], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [126], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [4, 70]], 'events': [['issue', 8]]}, {'cogs': [480], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 7], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 16], ['issue', 4], ['recv', 1, 16]]}, {'cogs': [52], 'layers': [[4, 16]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 4, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[5, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 34], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [5, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[3, 9]]}], ['price decline', {'layers': [[2, 64], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [128], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [5, 70]], 'events': [['issue', 9]]}, {'cogs': [550], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 8], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 17], ['issue', 4], ['recv', 1, 17]]}, {'cogs': [54], 'layers': [[4, 17]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 5, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[6, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 35], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [6, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[4, 9]]}], ['price decline', {'layers': [[2, 65], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [130], 'layers': [[3, 45]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
An issue that exactly empties the warehouse is rejected as short even though every unit is on hand.

Input {layers:[[qty,unit_cost]] oldest first, events:[["recv",qty,unit_cost]|["issue",qty]]}. Issues consume the oldest layer first; an issue larger than on-hand is rejected whole and reported as "short"; a receipt at exactly the newest layer cost merges into it; receipts of zero or negative quantity are ignored. Return {cogs:[per-issue cost or "short"], layers}.

split: open-access


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

N = 1
observations = []
def solve(x):
    layers = [list(l) for l in x['layers']]
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            if q <= 0:
                continue
            if layers:
                layers[-1][0] += q
            else:
                layers.append([q, c])
            continue
        need = ev[1]
        onhand = sum(l[0] for l in layers)
        if need > onhand:
            out.append('short')
            continue
        cost = 0
        while need > 0:
            idx = 0
            take = min(need, layers[idx][0])
            cost += take * layers[idx][1]
            layers[idx][0] -= take
            need -= take
            if layers[idx][0] == 0:
                layers.pop(idx)
        out.append(cost)
    return {'cogs': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [1, 70]], 'events': [['issue', 5]]}, {'cogs': [270], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 4], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 13], ['issue', 4], ['recv', 1, 13]]}, {'cogs': [46], 'layers': [[4, 13]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 1, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[2, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 31], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [2, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': []}], ['price decline', {'layers': [[2, 61], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [122], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [2, 70]], 'events': [['issue', 6]]}, {'cogs': [340], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 5], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 14], ['issue', 4], ['recv', 1, 14]]}, {'cogs': [48], 'layers': [[4, 14]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 2, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[3, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 32], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [3, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[1, 9]]}], ['price decline', {'layers': [[2, 62], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [124], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 7]]}, {'cogs': [740], 'layers': [[8, 120]]}], ['exact depletion', {'layers': [[4, 50], [3, 70]], 'events': [['issue', 7]]}, {'cogs': [410], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 6], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 15], ['issue', 4], ['recv', 1, 15]]}, {'cogs': [50], 'layers': [[4, 15]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 3, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[4, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 33], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [4, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[2, 9]]}], ['price decline', {'layers': [[2, 63], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [126], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [4, 70]], 'events': [['issue', 8]]}, {'cogs': [480], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 7], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 16], ['issue', 4], ['recv', 1, 16]]}, {'cogs': [52], 'layers': [[4, 16]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 4, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[5, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 34], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [5, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[3, 9]]}], ['price decline', {'layers': [[2, 64], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [128], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [5, 70]], 'events': [['issue', 9]]}, {'cogs': [550], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 8], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 17], ['issue', 4], ['recv', 1, 17]]}, {'cogs': [54], 'layers': [[4, 17]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 5, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[6, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 35], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [6, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[4, 9]]}], ['price decline', {'layers': [[2, 65], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [130], 'layers': [[3, 45]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    layers = [list(l) for l in x['layers']]
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            if q <= 0:
                continue
            if layers and abs(layers[-1][1] - c) <= 1:
                layers[-1][0] += q
            else:
                layers.append([q, c])
            continue
        need = ev[1]
        onhand = sum(l[0] for l in layers)
        if need > onhand:
            out.append('short')
            continue
        cost = 0
        while need > 0:
            idx = 0
            take = min(need, layers[idx][0])
            cost += take * layers[idx][1]
            layers[idx][0] -= take
            need -= take
            if layers[idx][0] == 0:
                layers.pop(idx)
        out.append(cost)
    return {'cogs': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [1, 70]], 'events': [['issue', 5]]}, {'cogs': [270], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 4], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 13], ['issue', 4], ['recv', 1, 13]]}, {'cogs': [46], 'layers': [[4, 13]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 1, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[2, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 31], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [2, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': []}], ['price decline', {'layers': [[2, 61], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [122], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [2, 70]], 'events': [['issue', 6]]}, {'cogs': [340], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 5], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 14], ['issue', 4], ['recv', 1, 14]]}, {'cogs': [48], 'layers': [[4, 14]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 2, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[3, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 32], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [3, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[1, 9]]}], ['price decline', {'layers': [[2, 62], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [124], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 7]]}, {'cogs': [740], 'layers': [[8, 120]]}], ['exact depletion', {'layers': [[4, 50], [3, 70]], 'events': [['issue', 7]]}, {'cogs': [410], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 6], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 15], ['issue', 4], ['recv', 1, 15]]}, {'cogs': [50], 'layers': [[4, 15]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 3, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[4, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 33], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [4, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[2, 9]]}], ['price decline', {'layers': [[2, 63], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [126], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [4, 70]], 'events': [['issue', 8]]}, {'cogs': [480], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 7], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 16], ['issue', 4], ['recv', 1, 16]]}, {'cogs': [52], 'layers': [[4, 16]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 4, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[5, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 34], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [5, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[3, 9]]}], ['price decline', {'layers': [[2, 64], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [128], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [5, 70]], 'events': [['issue', 9]]}, {'cogs': [550], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 8], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 17], ['issue', 4], ['recv', 1, 17]]}, {'cogs': [54], 'layers': [[4, 17]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 5, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[6, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 35], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [6, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[4, 9]]}], ['price decline', {'layers': [[2, 65], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [130], 'layers': [[3, 45]]}]]]
for label, args, expected in fixtures[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 receipt at a different price is folded into the previous layer, so later issues use the stale price.

Input {layers:[[qty,unit_cost]] oldest first, events:[["recv",qty,unit_cost]|["issue",qty]]}. Issues consume the oldest layer first; an issue larger than on-hand is rejected whole and reported as "short"; a receipt at exactly the newest layer cost merges into it; receipts of zero or negative quantity are ignored. Return {cogs:[per-issue cost or "short"], layers}.

split: open-access


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

N = 1
observations = []
def solve(x):
    layers = [list(l) for l in x['layers']]
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            if q <= 0:
                continue
            if layers and layers[-1][1] == c:
                layers[-1][0] += q
            else:
                layers.append([q, c])
            continue
        need = ev[1]
        onhand = sum(l[0] for l in layers)
        if need > onhand:
            out.append('short')
            continue
        cost = 0
        while need > 0:
            idx = 0
            take = min(need, layers[idx][0])
            cost = take * layers[idx][1]
            layers[idx][0] -= take
            need -= take
            if layers[idx][0] == 0:
                layers.pop(idx)
        out.append(cost)
    return {'cogs': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [1, 70]], 'events': [['issue', 5]]}, {'cogs': [270], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 4], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 13], ['issue', 4], ['recv', 1, 13]]}, {'cogs': [46], 'layers': [[4, 13]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 1, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[2, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 31], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [2, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': []}], ['price decline', {'layers': [[2, 61], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [122], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [2, 70]], 'events': [['issue', 6]]}, {'cogs': [340], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 5], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 14], ['issue', 4], ['recv', 1, 14]]}, {'cogs': [48], 'layers': [[4, 14]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 2, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[3, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 32], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [3, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[1, 9]]}], ['price decline', {'layers': [[2, 62], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [124], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 7]]}, {'cogs': [740], 'layers': [[8, 120]]}], ['exact depletion', {'layers': [[4, 50], [3, 70]], 'events': [['issue', 7]]}, {'cogs': [410], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 6], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 15], ['issue', 4], ['recv', 1, 15]]}, {'cogs': [50], 'layers': [[4, 15]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 3, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[4, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 33], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [4, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[2, 9]]}], ['price decline', {'layers': [[2, 63], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [126], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [4, 70]], 'events': [['issue', 8]]}, {'cogs': [480], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 7], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 16], ['issue', 4], ['recv', 1, 16]]}, {'cogs': [52], 'layers': [[4, 16]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 4, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[5, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 34], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [5, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[3, 9]]}], ['price decline', {'layers': [[2, 64], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [128], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [5, 70]], 'events': [['issue', 9]]}, {'cogs': [550], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 8], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 17], ['issue', 4], ['recv', 1, 17]]}, {'cogs': [54], 'layers': [[4, 17]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 5, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[6, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 35], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [6, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[4, 9]]}], ['price decline', {'layers': [[2, 65], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [130], 'layers': [[3, 45]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    layers = [list(l) for l in x['layers']]
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            if q <= 0:
                continue
            if layers and layers[-1][1] == c:
                layers[-1][0] += q
            else:
                layers.append([q, c])
            continue
        need = ev[1]
        onhand = sum(l[0] for l in layers)
        if need > onhand:
            out.append('short')
            continue
        cost = 0
        while need > 0:
            idx = 0
            take = min(need, layers[idx][0])
            cost += need * layers[idx][1]
            layers[idx][0] -= take
            need -= take
            if layers[idx][0] == 0:
                layers.pop(idx)
        out.append(cost)
    return {'cogs': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [1, 70]], 'events': [['issue', 5]]}, {'cogs': [270], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 4], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 13], ['issue', 4], ['recv', 1, 13]]}, {'cogs': [46], 'layers': [[4, 13]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 1, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[2, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 31], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [2, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': []}], ['price decline', {'layers': [[2, 61], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [122], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [2, 70]], 'events': [['issue', 6]]}, {'cogs': [340], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 5], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 14], ['issue', 4], ['recv', 1, 14]]}, {'cogs': [48], 'layers': [[4, 14]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 2, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[3, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 32], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [3, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[1, 9]]}], ['price decline', {'layers': [[2, 62], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [124], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 7]]}, {'cogs': [740], 'layers': [[8, 120]]}], ['exact depletion', {'layers': [[4, 50], [3, 70]], 'events': [['issue', 7]]}, {'cogs': [410], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 6], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 15], ['issue', 4], ['recv', 1, 15]]}, {'cogs': [50], 'layers': [[4, 15]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 3, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[4, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 33], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [4, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[2, 9]]}], ['price decline', {'layers': [[2, 63], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [126], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [4, 70]], 'events': [['issue', 8]]}, {'cogs': [480], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 7], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 16], ['issue', 4], ['recv', 1, 16]]}, {'cogs': [52], 'layers': [[4, 16]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 4, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[5, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 34], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [5, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[3, 9]]}], ['price decline', {'layers': [[2, 64], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [128], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [5, 70]], 'events': [['issue', 9]]}, {'cogs': [550], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 8], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 17], ['issue', 4], ['recv', 1, 17]]}, {'cogs': [54], 'layers': [[4, 17]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 5, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[6, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 35], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [6, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[4, 9]]}], ['price decline', {'layers': [[2, 65], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [130], 'layers': [[3, 45]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
An issue that spans several layers reports only the cost of the last layer touched.

Input {layers:[[qty,unit_cost]] oldest first, events:[["recv",qty,unit_cost]|["issue",qty]]}. Issues consume the oldest layer first; an issue larger than on-hand is rejected whole and reported as "short"; a receipt at exactly the newest layer cost merges into it; receipts of zero or negative quantity are ignored. Return {cogs:[per-issue cost or "short"], layers}.

split: open-access


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

N = 1
observations = []
def solve(x):
    layers = [list(l) for l in x['layers']]
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            if q < 0:
                continue
            if layers and layers[-1][1] == c:
                layers[-1][0] += q
            else:
                layers.append([q, c])
            continue
        need = ev[1]
        onhand = sum(l[0] for l in layers)
        if need > onhand:
            out.append('short')
            continue
        cost = 0
        while need > 0:
            idx = 0
            take = min(need, layers[idx][0])
            cost += take * layers[idx][1]
            layers[idx][0] -= take
            need -= take
            if layers[idx][0] == 0:
                layers.pop(idx)
        out.append(cost)
    return {'cogs': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [1, 70]], 'events': [['issue', 5]]}, {'cogs': [270], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 4], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 13], ['issue', 4], ['recv', 1, 13]]}, {'cogs': [46], 'layers': [[4, 13]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 1, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[2, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 31], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [2, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': []}], ['price decline', {'layers': [[2, 61], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [122], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [2, 70]], 'events': [['issue', 6]]}, {'cogs': [340], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 5], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 14], ['issue', 4], ['recv', 1, 14]]}, {'cogs': [48], 'layers': [[4, 14]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 2, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[3, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 32], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [3, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[1, 9]]}], ['price decline', {'layers': [[2, 62], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [124], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 7]]}, {'cogs': [740], 'layers': [[8, 120]]}], ['exact depletion', {'layers': [[4, 50], [3, 70]], 'events': [['issue', 7]]}, {'cogs': [410], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 6], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 15], ['issue', 4], ['recv', 1, 15]]}, {'cogs': [50], 'layers': [[4, 15]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 3, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[4, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 33], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [4, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[2, 9]]}], ['price decline', {'layers': [[2, 63], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [126], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [4, 70]], 'events': [['issue', 8]]}, {'cogs': [480], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 7], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 16], ['issue', 4], ['recv', 1, 16]]}, {'cogs': [52], 'layers': [[4, 16]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 4, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[5, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 34], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [5, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[3, 9]]}], ['price decline', {'layers': [[2, 64], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [128], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [5, 70]], 'events': [['issue', 9]]}, {'cogs': [550], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 8], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 17], ['issue', 4], ['recv', 1, 17]]}, {'cogs': [54], 'layers': [[4, 17]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 5, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[6, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 35], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [6, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[4, 9]]}], ['price decline', {'layers': [[2, 65], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [130], 'layers': [[3, 45]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    layers = [list(l) for l in x['layers']]
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            if q < 0 or (q == 0 and c == 0):
                continue
            if layers and layers[-1][1] == c:
                layers[-1][0] += q
            else:
                layers.append([q, c])
            continue
        need = ev[1]
        onhand = sum(l[0] for l in layers)
        if need > onhand:
            out.append('short')
            continue
        cost = 0
        while need > 0:
            idx = 0
            take = min(need, layers[idx][0])
            cost += take * layers[idx][1]
            layers[idx][0] -= take
            need -= take
            if layers[idx][0] == 0:
                layers.pop(idx)
        out.append(cost)
    return {'cogs': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [1, 70]], 'events': [['issue', 5]]}, {'cogs': [270], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 4], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 13], ['issue', 4], ['recv', 1, 13]]}, {'cogs': [46], 'layers': [[4, 13]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 1, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[2, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 31], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [2, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': []}], ['price decline', {'layers': [[2, 61], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [122], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [2, 70]], 'events': [['issue', 6]]}, {'cogs': [340], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 5], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 14], ['issue', 4], ['recv', 1, 14]]}, {'cogs': [48], 'layers': [[4, 14]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 2, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[3, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 32], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [3, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[1, 9]]}], ['price decline', {'layers': [[2, 62], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [124], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 7]]}, {'cogs': [740], 'layers': [[8, 120]]}], ['exact depletion', {'layers': [[4, 50], [3, 70]], 'events': [['issue', 7]]}, {'cogs': [410], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 6], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 15], ['issue', 4], ['recv', 1, 15]]}, {'cogs': [50], 'layers': [[4, 15]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 3, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[4, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 33], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [4, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[2, 9]]}], ['price decline', {'layers': [[2, 63], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [126], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [4, 70]], 'events': [['issue', 8]]}, {'cogs': [480], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 7], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 16], ['issue', 4], ['recv', 1, 16]]}, {'cogs': [52], 'layers': [[4, 16]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 4, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[5, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 34], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [5, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[3, 9]]}], ['price decline', {'layers': [[2, 64], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [128], 'layers': [[3, 45]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [5, 70]], 'events': [['issue', 9]]}, {'cogs': [550], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 8], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 17], ['issue', 4], ['recv', 1, 17]]}, {'cogs': [54], 'layers': [[4, 17]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 5, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[6, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 35], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [6, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[4, 9]]}], ['price decline', {'layers': [[2, 65], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [130], 'layers': [[3, 45]]}]]]
for label, args, expected in fixtures[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 zero-quantity receipt line creates an empty cost layer that shows up in the valuation report.

Input {layers:[[qty,unit_cost]] oldest first, events:[["recv",qty,unit_cost]|["issue",qty]]}. Issues consume the oldest layer first; an issue larger than on-hand is rejected whole and reported as "short"; a receipt at exactly the newest layer cost merges into it; receipts of zero or negative quantity are ignored. Return {cogs:[per-issue cost or "short"], layers}.

split: open-access


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

N = 1
observations = []
def solve(x):
    layers = [list(l) for l in x['layers']]
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            if q <= 0:
                continue
            if layers and layers[-1][1] == c:
                layers[-1][0] += q
            else:
                layers.append([q, c])
            continue
        need = ev[1]
        onhand = sum(l[0] for l in layers)
        if need > onhand:
            out.append('short')
            continue
        cost = 0
        while need > 0:
            idx = 0
            take = min(need, layers[idx][0])
            cost += take * layers[idx][1]
            layers[idx][0] -= take
            need -= take
            if layers[idx][0] == 0 and need == 0:
                layers.pop(idx)
            elif layers[idx][0] == 0:
                layers.append(layers.pop(idx))
        out.append(cost)
    return {'cogs': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [1, 70]], 'events': [['issue', 5]]}, {'cogs': [270], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 4], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 13], ['issue', 4], ['recv', 1, 13]]}, {'cogs': [46], 'layers': [[4, 13]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 1, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[2, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 31], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [2, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': []}], ['price decline', {'layers': [[2, 61], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [122], 'layers': [[3, 45]]}], ['exhaust layer then merge', {'layers': [[2, 10], [3, 20]], 'events': [['issue', 2], ['recv', 1, 10]]}, {'cogs': [20], 'layers': [[3, 20], [1, 10]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [2, 70]], 'events': [['issue', 6]]}, {'cogs': [340], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 5], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 14], ['issue', 4], ['recv', 1, 14]]}, {'cogs': [48], 'layers': [[4, 14]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 2, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[3, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 32], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [3, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[1, 9]]}], ['price decline', {'layers': [[2, 62], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [124], 'layers': [[3, 45]]}], ['exhaust layer then merge', {'layers': [[2, 10], [3, 20]], 'events': [['issue', 2], ['recv', 2, 10]]}, {'cogs': [20], 'layers': [[3, 20], [2, 10]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 7]]}, {'cogs': [740], 'layers': [[8, 120]]}], ['exact depletion', {'layers': [[4, 50], [3, 70]], 'events': [['issue', 7]]}, {'cogs': [410], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 6], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 15], ['issue', 4], ['recv', 1, 15]]}, {'cogs': [50], 'layers': [[4, 15]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 3, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[4, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 33], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [4, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[2, 9]]}], ['price decline', {'layers': [[2, 63], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [126], 'layers': [[3, 45]]}], ['exhaust layer then merge', {'layers': [[2, 10], [3, 20]], 'events': [['issue', 2], ['recv', 3, 10]]}, {'cogs': [20], 'layers': [[3, 20], [3, 10]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [4, 70]], 'events': [['issue', 8]]}, {'cogs': [480], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 7], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 16], ['issue', 4], ['recv', 1, 16]]}, {'cogs': [52], 'layers': [[4, 16]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 4, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[5, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 34], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [5, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[3, 9]]}], ['price decline', {'layers': [[2, 64], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [128], 'layers': [[3, 45]]}], ['exhaust layer then merge', {'layers': [[2, 10], [3, 20]], 'events': [['issue', 2], ['recv', 4, 10]]}, {'cogs': [20], 'layers': [[3, 20], [4, 10]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [5, 70]], 'events': [['issue', 9]]}, {'cogs': [550], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 8], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 17], ['issue', 4], ['recv', 1, 17]]}, {'cogs': [54], 'layers': [[4, 17]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 5, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[6, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 35], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [6, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[4, 9]]}], ['price decline', {'layers': [[2, 65], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [130], 'layers': [[3, 45]]}], ['exhaust layer then merge', {'layers': [[2, 10], [3, 20]], 'events': [['issue', 2], ['recv', 5, 10]]}, {'cogs': [20], 'layers': [[3, 20], [5, 10]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    layers = [list(l) for l in x['layers']]
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            if q <= 0:
                continue
            if layers and layers[-1][1] == c:
                layers[-1][0] += q
            else:
                layers.append([q, c])
            continue
        need = ev[1]
        onhand = sum(l[0] for l in layers)
        if need > onhand:
            out.append('short')
            continue
        cost = 0
        while need > 0:
            idx = 0
            take = min(need, layers[idx][0])
            cost += take * layers[idx][1]
            layers[idx][0] -= take
            need -= take
            if layers[idx][0] == 0 and need > 0:
                layers.pop(idx)
            elif layers[idx][0] == 0:
                layers.append(layers.pop(idx))
        out.append(cost)
    return {'cogs': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [1, 70]], 'events': [['issue', 5]]}, {'cogs': [270], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 4], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 13], ['issue', 4], ['recv', 1, 13]]}, {'cogs': [46], 'layers': [[4, 13]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 1, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[2, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 31], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [2, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': []}], ['price decline', {'layers': [[2, 61], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [122], 'layers': [[3, 45]]}], ['exhaust layer then merge', {'layers': [[2, 10], [3, 20]], 'events': [['issue', 2], ['recv', 1, 10]]}, {'cogs': [20], 'layers': [[3, 20], [1, 10]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [2, 70]], 'events': [['issue', 6]]}, {'cogs': [340], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 5], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 14], ['issue', 4], ['recv', 1, 14]]}, {'cogs': [48], 'layers': [[4, 14]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 2, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[3, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 32], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [3, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[1, 9]]}], ['price decline', {'layers': [[2, 62], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [124], 'layers': [[3, 45]]}], ['exhaust layer then merge', {'layers': [[2, 10], [3, 20]], 'events': [['issue', 2], ['recv', 2, 10]]}, {'cogs': [20], 'layers': [[3, 20], [2, 10]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 7]]}, {'cogs': [740], 'layers': [[8, 120]]}], ['exact depletion', {'layers': [[4, 50], [3, 70]], 'events': [['issue', 7]]}, {'cogs': [410], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 6], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 15], ['issue', 4], ['recv', 1, 15]]}, {'cogs': [50], 'layers': [[4, 15]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 3, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[4, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 33], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [4, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[2, 9]]}], ['price decline', {'layers': [[2, 63], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [126], 'layers': [[3, 45]]}], ['exhaust layer then merge', {'layers': [[2, 10], [3, 20]], 'events': [['issue', 2], ['recv', 3, 10]]}, {'cogs': [20], 'layers': [[3, 20], [3, 10]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 8]]}, {'cogs': [860], 'layers': [[7, 120]]}], ['exact depletion', {'layers': [[4, 50], [4, 70]], 'events': [['issue', 8]]}, {'cogs': [480], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 7], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 16], ['issue', 4], ['recv', 1, 16]]}, {'cogs': [52], 'layers': [[4, 16]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 4, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[5, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 34], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [5, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[3, 9]]}], ['price decline', {'layers': [[2, 64], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [128], 'layers': [[3, 45]]}], ['exhaust layer then merge', {'layers': [[2, 10], [3, 20]], 'events': [['issue', 2], ['recv', 4, 10]]}, {'cogs': [20], 'layers': [[3, 20], [4, 10]]}]], [['span two layers', {'layers': [[5, 100], [10, 120]], 'events': [['issue', 9]]}, {'cogs': [980], 'layers': [[6, 120]]}], ['exact depletion', {'layers': [[4, 50], [5, 70]], 'events': [['issue', 9]]}, {'cogs': [550], 'layers': []}], ['short rejected', {'layers': [[3, 90]], 'events': [['issue', 8], ['issue', 2]]}, {'cogs': ['short', 180], 'layers': [[1, 90]]}], ['receipt then issue', {'layers': [[2, 10]], 'events': [['recv', 5, 17], ['issue', 4], ['recv', 1, 17]]}, {'cogs': [54], 'layers': [[4, 17]]}], ['same cost merge', {'layers': [[2, 40]], 'events': [['recv', 5, 40], ['recv', 3, 41], ['issue', 1]]}, {'cogs': [40], 'layers': [[6, 40], [3, 41]]}], ['zero receipt', {'layers': [[6, 25]], 'events': [['recv', 0, 35], ['issue', 2]]}, {'cogs': [50], 'layers': [[4, 25]]}], ['multi issue', {'layers': [[1, 5], [1, 7], [6, 9]], 'events': [['issue', 1], ['issue', 2], ['issue', 1]]}, {'cogs': [5, 16, 9], 'layers': [[4, 9]]}], ['price decline', {'layers': [[2, 65], [3, 45]], 'events': [['issue', 2]]}, {'cogs': [130], 'layers': [[3, 45]]}], ['exhaust layer then merge', {'layers': [[2, 10], [3, 20]], 'events': [['issue', 2], ['recv', 5, 10]]}, {'cogs': [20], 'layers': [[3, 20], [5, 10]]}]]]
for label, args, expected in fixtures[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:
Fully consumed layers remain in the layer list with zero quantity.

Input {layers:[[qty,unit_cost]] oldest first, events:[["recv",qty,unit_cost]|["issue",qty]]}. Issues consume the oldest layer first; an issue larger than on-hand is rejected whole and reported as "short"; a receipt at exactly the newest layer cost merges into it; receipts of zero or negative quantity are ignored. Return {cogs:[per-issue cost or "short"], layers}.

split: open-access