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

N = 1
observations = []
def solve(x):
    layers = []
    neg = []
    last = x['default_cost']
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            adj = 0
            while q > 0 and neg:
                k = 0
                t = min(q, neg[k][0])
                adj += t * (c - neg[k][1])
                neg[k][0] -= t
                q -= t
                if neg[k][0] == 0:
                    neg.pop(k)
            if q > 0:
                layers.append([q, c])
            last = c
            out.append(adj)
        else:
            q = ev[1]
            cost = 0
            while q > 0 and layers:
                t = min(q, layers[0][0])
                cost += t * layers[0][1]
                layers[0][0] -= t
                q -= t
                if layers[0][0] == 0:
                    layers.pop(0)
            if q > 0:
                neg.append([q, x['default_cost']])
                cost += q * x['default_cost']
            out.append(cost)
    return {'results': out, 'layers': layers, 'backorder': neg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 46]]}, {'results': [0, 200, 18], 'layers': [[3, 46]], 'backorder': []}], ['never received', {'default_cost': 31, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [62, 8], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 26]]}, {'results': [0, 40, 10, 90, -12], 'layers': [[1, 26]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 2], ['recv', 3, 50]]}, {'results': [0, 120, -10], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 1], ['issue', 1]]}, {'results': [7, 7], 'layers': [], 'backorder': [[1, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 47]]}, {'results': [0, 200, 21], 'layers': [[3, 47]], 'backorder': []}], ['never received', {'default_cost': 32, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [64, 6], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 27]]}, {'results': [0, 40, 10, 90, -9], 'layers': [[1, 27]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 3], ['recv', 4, 50]]}, {'results': [0, 180, -20], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 2], ['issue', 1]]}, {'results': [14, 7], 'layers': [], 'backorder': [[2, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 48]]}, {'results': [0, 200, 24], 'layers': [[3, 48]], 'backorder': []}], ['never received', {'default_cost': 33, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [66, 4], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 28]]}, {'results': [0, 40, 10, 90, -6], 'layers': [[1, 28]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 4], ['recv', 5, 50]]}, {'results': [0, 240, -30], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 3], ['issue', 1]]}, {'results': [21, 7], 'layers': [], 'backorder': [[3, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 49]]}, {'results': [0, 200, 27], 'layers': [[3, 49]], 'backorder': []}], ['never received', {'default_cost': 34, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [68, 2], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 29]]}, {'results': [0, 40, 10, 90, -3], 'layers': [[1, 29]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 300, -40], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 4], ['issue', 1]]}, {'results': [28, 7], 'layers': [], 'backorder': [[4, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 200, 30], 'layers': [[3, 50]], 'backorder': []}], ['never received', {'default_cost': 35, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [70, 0], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 30]]}, {'results': [0, 40, 10, 90, 0], 'layers': [[1, 30]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 6], ['recv', 7, 50]]}, {'results': [0, 360, -50], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 5], ['issue', 1]]}, {'results': [35, 7], 'layers': [], 'backorder': [[5, 7], [1, 7]]}]]]
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 = []
    neg = []
    last = x['default_cost']
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            adj = 0
            while q > 0 and neg:
                k = 0
                t = min(q, neg[k][0])
                adj += t * (c - neg[k][1])
                neg[k][0] -= t
                q -= t
                if neg[k][0] == 0:
                    neg.pop(k)
            if q > 0:
                layers.append([q, c])
            last = c
            out.append(adj)
        else:
            q = ev[1]
            cost = 0
            while q > 0 and layers:
                t = min(q, layers[0][0])
                cost += t * layers[0][1]
                layers[0][0] -= t
                q -= t
                if layers[0][0] == 0:
                    layers.pop(0)
            if q > 0:
                neg.append([q, last if layers else x['default_cost']])
                cost += q * (last if layers else x['default_cost'])
            out.append(cost)
    return {'results': out, 'layers': layers, 'backorder': neg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 46]]}, {'results': [0, 200, 18], 'layers': [[3, 46]], 'backorder': []}], ['never received', {'default_cost': 31, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [62, 8], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 26]]}, {'results': [0, 40, 10, 90, -12], 'layers': [[1, 26]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 2], ['recv', 3, 50]]}, {'results': [0, 120, -10], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 1], ['issue', 1]]}, {'results': [7, 7], 'layers': [], 'backorder': [[1, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 47]]}, {'results': [0, 200, 21], 'layers': [[3, 47]], 'backorder': []}], ['never received', {'default_cost': 32, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [64, 6], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 27]]}, {'results': [0, 40, 10, 90, -9], 'layers': [[1, 27]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 3], ['recv', 4, 50]]}, {'results': [0, 180, -20], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 2], ['issue', 1]]}, {'results': [14, 7], 'layers': [], 'backorder': [[2, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 48]]}, {'results': [0, 200, 24], 'layers': [[3, 48]], 'backorder': []}], ['never received', {'default_cost': 33, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [66, 4], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 28]]}, {'results': [0, 40, 10, 90, -6], 'layers': [[1, 28]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 4], ['recv', 5, 50]]}, {'results': [0, 240, -30], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 3], ['issue', 1]]}, {'results': [21, 7], 'layers': [], 'backorder': [[3, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 49]]}, {'results': [0, 200, 27], 'layers': [[3, 49]], 'backorder': []}], ['never received', {'default_cost': 34, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [68, 2], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 29]]}, {'results': [0, 40, 10, 90, -3], 'layers': [[1, 29]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 300, -40], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 4], ['issue', 1]]}, {'results': [28, 7], 'layers': [], 'backorder': [[4, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 200, 30], 'layers': [[3, 50]], 'backorder': []}], ['never received', {'default_cost': 35, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [70, 0], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 30]]}, {'results': [0, 40, 10, 90, 0], 'layers': [[1, 30]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 6], ['recv', 7, 50]]}, {'results': [0, 360, -50], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 5], ['issue', 1]]}, {'results': [35, 7], 'layers': [], 'backorder': [[5, 7], [1, 7]]}]]]
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:
Backordered issues are always costed at the item default even after real receipts established a price.

Events: receipts ["recv",qty,cost] and issues ["issue",qty]. Issues draw FIFO layers; any shortfall is issued provisionally at the last receipt cost (default_cost before any receipt) and queued as a backorder. A receipt first settles backorders oldest first, returning the true-up qty*(receipt cost - provisional cost); only the remainder becomes a layer. Return {results, layers, backorder}.

split: open-access


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

N = 1
observations = []
def solve(x):
    layers = []
    neg = []
    last = x['default_cost']
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            adj = 0
            while q > 0 and neg and False:
                k = 0
                t = min(q, neg[k][0])
                adj += t * (c - neg[k][1])
                neg[k][0] -= t
                q -= t
                if neg[k][0] == 0:
                    neg.pop(k)
            if q > 0:
                layers.append([q, c])
            last = c
            out.append(adj)
        else:
            q = ev[1]
            cost = 0
            while q > 0 and layers:
                t = min(q, layers[0][0])
                cost += t * layers[0][1]
                layers[0][0] -= t
                q -= t
                if layers[0][0] == 0:
                    layers.pop(0)
            if q > 0:
                neg.append([q, last])
                cost += q * last
            out.append(cost)
    return {'results': out, 'layers': layers, 'backorder': neg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 46]]}, {'results': [0, 200, 18], 'layers': [[3, 46]], 'backorder': []}], ['never received', {'default_cost': 31, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [62, 8], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 26]]}, {'results': [0, 40, 10, 90, -12], 'layers': [[1, 26]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 2], ['recv', 3, 50]]}, {'results': [0, 120, -10], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 1], ['issue', 1]]}, {'results': [7, 7], 'layers': [], 'backorder': [[1, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 47]]}, {'results': [0, 200, 21], 'layers': [[3, 47]], 'backorder': []}], ['never received', {'default_cost': 32, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [64, 6], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 27]]}, {'results': [0, 40, 10, 90, -9], 'layers': [[1, 27]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 3], ['recv', 4, 50]]}, {'results': [0, 180, -20], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 2], ['issue', 1]]}, {'results': [14, 7], 'layers': [], 'backorder': [[2, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 48]]}, {'results': [0, 200, 24], 'layers': [[3, 48]], 'backorder': []}], ['never received', {'default_cost': 33, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [66, 4], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 28]]}, {'results': [0, 40, 10, 90, -6], 'layers': [[1, 28]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 4], ['recv', 5, 50]]}, {'results': [0, 240, -30], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 3], ['issue', 1]]}, {'results': [21, 7], 'layers': [], 'backorder': [[3, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 49]]}, {'results': [0, 200, 27], 'layers': [[3, 49]], 'backorder': []}], ['never received', {'default_cost': 34, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [68, 2], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 29]]}, {'results': [0, 40, 10, 90, -3], 'layers': [[1, 29]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 300, -40], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 4], ['issue', 1]]}, {'results': [28, 7], 'layers': [], 'backorder': [[4, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 200, 30], 'layers': [[3, 50]], 'backorder': []}], ['never received', {'default_cost': 35, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [70, 0], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 30]]}, {'results': [0, 40, 10, 90, 0], 'layers': [[1, 30]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 6], ['recv', 7, 50]]}, {'results': [0, 360, -50], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 5], ['issue', 1]]}, {'results': [35, 7], 'layers': [], 'backorder': [[5, 7], [1, 7]]}]]]
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 = []
    neg = []
    last = x['default_cost']
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            adj = 0
            while q > 0 and neg:
                k = 0
                t = min(q, neg[k][0])
                adj += t * (c - neg[k][1])
                neg[k][0] -= t
                q -= t
                if neg[k][0] == 0:
                    neg.pop(k)
            if ev[1] > 0:
                layers.append([ev[1], c])
            last = c
            out.append(adj)
        else:
            q = ev[1]
            cost = 0
            while q > 0 and layers:
                t = min(q, layers[0][0])
                cost += t * layers[0][1]
                layers[0][0] -= t
                q -= t
                if layers[0][0] == 0:
                    layers.pop(0)
            if q > 0:
                neg.append([q, last])
                cost += q * last
            out.append(cost)
    return {'results': out, 'layers': layers, 'backorder': neg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 46]]}, {'results': [0, 200, 18], 'layers': [[3, 46]], 'backorder': []}], ['never received', {'default_cost': 31, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [62, 8], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 26]]}, {'results': [0, 40, 10, 90, -12], 'layers': [[1, 26]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 2], ['recv', 3, 50]]}, {'results': [0, 120, -10], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 1], ['issue', 1]]}, {'results': [7, 7], 'layers': [], 'backorder': [[1, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 47]]}, {'results': [0, 200, 21], 'layers': [[3, 47]], 'backorder': []}], ['never received', {'default_cost': 32, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [64, 6], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 27]]}, {'results': [0, 40, 10, 90, -9], 'layers': [[1, 27]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 3], ['recv', 4, 50]]}, {'results': [0, 180, -20], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 2], ['issue', 1]]}, {'results': [14, 7], 'layers': [], 'backorder': [[2, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 48]]}, {'results': [0, 200, 24], 'layers': [[3, 48]], 'backorder': []}], ['never received', {'default_cost': 33, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [66, 4], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 28]]}, {'results': [0, 40, 10, 90, -6], 'layers': [[1, 28]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 4], ['recv', 5, 50]]}, {'results': [0, 240, -30], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 3], ['issue', 1]]}, {'results': [21, 7], 'layers': [], 'backorder': [[3, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 49]]}, {'results': [0, 200, 27], 'layers': [[3, 49]], 'backorder': []}], ['never received', {'default_cost': 34, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [68, 2], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 29]]}, {'results': [0, 40, 10, 90, -3], 'layers': [[1, 29]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 300, -40], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 4], ['issue', 1]]}, {'results': [28, 7], 'layers': [], 'backorder': [[4, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 200, 30], 'layers': [[3, 50]], 'backorder': []}], ['never received', {'default_cost': 35, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [70, 0], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 30]]}, {'results': [0, 40, 10, 90, 0], 'layers': [[1, 30]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 6], ['recv', 7, 50]]}, {'results': [0, 360, -50], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 5], ['issue', 1]]}, {'results': [35, 7], 'layers': [], 'backorder': [[5, 7], [1, 7]]}]]]
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:
Receipts rebuild positive stock while the negative balance stays outstanding forever.

Events: receipts ["recv",qty,cost] and issues ["issue",qty]. Issues draw FIFO layers; any shortfall is issued provisionally at the last receipt cost (default_cost before any receipt) and queued as a backorder. A receipt first settles backorders oldest first, returning the true-up qty*(receipt cost - provisional cost); only the remainder becomes a layer. Return {results, layers, backorder}.

split: open-access


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

N = 1
observations = []
def solve(x):
    layers = []
    neg = []
    last = x['default_cost']
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            adj = 0
            while q > 0 and neg:
                k = 0
                t = min(q, neg[k][0])
                adj += t * (neg[k][1] - c)
                neg[k][0] -= t
                q -= t
                if neg[k][0] == 0:
                    neg.pop(k)
            if q > 0:
                layers.append([q, c])
            last = c
            out.append(adj)
        else:
            q = ev[1]
            cost = 0
            while q > 0 and layers:
                t = min(q, layers[0][0])
                cost += t * layers[0][1]
                layers[0][0] -= t
                q -= t
                if layers[0][0] == 0:
                    layers.pop(0)
            if q > 0:
                neg.append([q, last])
                cost += q * last
            out.append(cost)
    return {'results': out, 'layers': layers, 'backorder': neg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 46]]}, {'results': [0, 200, 18], 'layers': [[3, 46]], 'backorder': []}], ['never received', {'default_cost': 31, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [62, 8], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 26]]}, {'results': [0, 40, 10, 90, -12], 'layers': [[1, 26]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 2], ['recv', 3, 50]]}, {'results': [0, 120, -10], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 1], ['issue', 1]]}, {'results': [7, 7], 'layers': [], 'backorder': [[1, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 47]]}, {'results': [0, 200, 21], 'layers': [[3, 47]], 'backorder': []}], ['never received', {'default_cost': 32, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [64, 6], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 27]]}, {'results': [0, 40, 10, 90, -9], 'layers': [[1, 27]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 3], ['recv', 4, 50]]}, {'results': [0, 180, -20], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 2], ['issue', 1]]}, {'results': [14, 7], 'layers': [], 'backorder': [[2, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 48]]}, {'results': [0, 200, 24], 'layers': [[3, 48]], 'backorder': []}], ['never received', {'default_cost': 33, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [66, 4], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 28]]}, {'results': [0, 40, 10, 90, -6], 'layers': [[1, 28]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 4], ['recv', 5, 50]]}, {'results': [0, 240, -30], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 3], ['issue', 1]]}, {'results': [21, 7], 'layers': [], 'backorder': [[3, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 49]]}, {'results': [0, 200, 27], 'layers': [[3, 49]], 'backorder': []}], ['never received', {'default_cost': 34, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [68, 2], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 29]]}, {'results': [0, 40, 10, 90, -3], 'layers': [[1, 29]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 300, -40], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 4], ['issue', 1]]}, {'results': [28, 7], 'layers': [], 'backorder': [[4, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 200, 30], 'layers': [[3, 50]], 'backorder': []}], ['never received', {'default_cost': 35, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [70, 0], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 30]]}, {'results': [0, 40, 10, 90, 0], 'layers': [[1, 30]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 6], ['recv', 7, 50]]}, {'results': [0, 360, -50], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 5], ['issue', 1]]}, {'results': [35, 7], 'layers': [], 'backorder': [[5, 7], [1, 7]]}]]]
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 = []
    neg = []
    last = x['default_cost']
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            adj = 0
            while q > 0 and neg:
                k = 0
                t = min(q, neg[k][0])
                adj += t * abs(c - neg[k][1])
                neg[k][0] -= t
                q -= t
                if neg[k][0] == 0:
                    neg.pop(k)
            if q > 0:
                layers.append([q, c])
            last = c
            out.append(adj)
        else:
            q = ev[1]
            cost = 0
            while q > 0 and layers:
                t = min(q, layers[0][0])
                cost += t * layers[0][1]
                layers[0][0] -= t
                q -= t
                if layers[0][0] == 0:
                    layers.pop(0)
            if q > 0:
                neg.append([q, last])
                cost += q * last
            out.append(cost)
    return {'results': out, 'layers': layers, 'backorder': neg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 46]]}, {'results': [0, 200, 18], 'layers': [[3, 46]], 'backorder': []}], ['never received', {'default_cost': 31, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [62, 8], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 26]]}, {'results': [0, 40, 10, 90, -12], 'layers': [[1, 26]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 2], ['recv', 3, 50]]}, {'results': [0, 120, -10], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 1], ['issue', 1]]}, {'results': [7, 7], 'layers': [], 'backorder': [[1, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 47]]}, {'results': [0, 200, 21], 'layers': [[3, 47]], 'backorder': []}], ['never received', {'default_cost': 32, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [64, 6], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 27]]}, {'results': [0, 40, 10, 90, -9], 'layers': [[1, 27]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 3], ['recv', 4, 50]]}, {'results': [0, 180, -20], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 2], ['issue', 1]]}, {'results': [14, 7], 'layers': [], 'backorder': [[2, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 48]]}, {'results': [0, 200, 24], 'layers': [[3, 48]], 'backorder': []}], ['never received', {'default_cost': 33, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [66, 4], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 28]]}, {'results': [0, 40, 10, 90, -6], 'layers': [[1, 28]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 4], ['recv', 5, 50]]}, {'results': [0, 240, -30], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 3], ['issue', 1]]}, {'results': [21, 7], 'layers': [], 'backorder': [[3, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 49]]}, {'results': [0, 200, 27], 'layers': [[3, 49]], 'backorder': []}], ['never received', {'default_cost': 34, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [68, 2], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 29]]}, {'results': [0, 40, 10, 90, -3], 'layers': [[1, 29]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 300, -40], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 4], ['issue', 1]]}, {'results': [28, 7], 'layers': [], 'backorder': [[4, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 200, 30], 'layers': [[3, 50]], 'backorder': []}], ['never received', {'default_cost': 35, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [70, 0], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 30]]}, {'results': [0, 40, 10, 90, 0], 'layers': [[1, 30]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 6], ['recv', 7, 50]]}, {'results': [0, 360, -50], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 5], ['issue', 1]]}, {'results': [35, 7], 'layers': [], 'backorder': [[5, 7], [1, 7]]}]]]
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:
Receipts dearer than the provisional cost reduce COGS instead of increasing it.

Events: receipts ["recv",qty,cost] and issues ["issue",qty]. Issues draw FIFO layers; any shortfall is issued provisionally at the last receipt cost (default_cost before any receipt) and queued as a backorder. A receipt first settles backorders oldest first, returning the true-up qty*(receipt cost - provisional cost); only the remainder becomes a layer. Return {results, layers, backorder}.

split: open-access


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

N = 1
observations = []
def solve(x):
    layers = []
    neg = []
    last = x['default_cost']
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            adj = 0
            while q > 0 and neg:
                k = -1
                t = min(q, neg[k][0])
                adj += t * (c - neg[k][1])
                neg[k][0] -= t
                q -= t
                if neg[k][0] == 0:
                    neg.pop(k)
            if q > 0:
                layers.append([q, c])
            last = c
            out.append(adj)
        else:
            q = ev[1]
            cost = 0
            while q > 0 and layers:
                t = min(q, layers[0][0])
                cost += t * layers[0][1]
                layers[0][0] -= t
                q -= t
                if layers[0][0] == 0:
                    layers.pop(0)
            if q > 0:
                neg.append([q, last])
                cost += q * last
            out.append(cost)
    return {'results': out, 'layers': layers, 'backorder': neg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 46]]}, {'results': [0, 200, 18], 'layers': [[3, 46]], 'backorder': []}], ['never received', {'default_cost': 31, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [62, 8], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 26]]}, {'results': [0, 40, 10, 90, -12], 'layers': [[1, 26]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 2], ['recv', 3, 50]]}, {'results': [0, 120, -10], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 1], ['issue', 1]]}, {'results': [7, 7], 'layers': [], 'backorder': [[1, 7], [1, 7]]}], ['cheap backorder first', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 3], ['recv', 1, 41], ['issue', 2], ['recv', 2, 30]]}, {'results': [0, 60, 21, 82, -1], 'layers': [], 'backorder': [[1, 41]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 47]]}, {'results': [0, 200, 21], 'layers': [[3, 47]], 'backorder': []}], ['never received', {'default_cost': 32, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [64, 6], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 27]]}, {'results': [0, 40, 10, 90, -9], 'layers': [[1, 27]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 3], ['recv', 4, 50]]}, {'results': [0, 180, -20], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 2], ['issue', 1]]}, {'results': [14, 7], 'layers': [], 'backorder': [[2, 7], [1, 7]]}], ['cheap backorder first', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 3], ['recv', 1, 42], ['issue', 2], ['recv', 2, 30]]}, {'results': [0, 60, 22, 84, -2], 'layers': [], 'backorder': [[1, 42]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 48]]}, {'results': [0, 200, 24], 'layers': [[3, 48]], 'backorder': []}], ['never received', {'default_cost': 33, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [66, 4], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 28]]}, {'results': [0, 40, 10, 90, -6], 'layers': [[1, 28]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 4], ['recv', 5, 50]]}, {'results': [0, 240, -30], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 3], ['issue', 1]]}, {'results': [21, 7], 'layers': [], 'backorder': [[3, 7], [1, 7]]}], ['cheap backorder first', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 3], ['recv', 1, 43], ['issue', 2], ['recv', 2, 30]]}, {'results': [0, 60, 23, 86, -3], 'layers': [], 'backorder': [[1, 43]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 49]]}, {'results': [0, 200, 27], 'layers': [[3, 49]], 'backorder': []}], ['never received', {'default_cost': 34, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [68, 2], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 29]]}, {'results': [0, 40, 10, 90, -3], 'layers': [[1, 29]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 300, -40], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 4], ['issue', 1]]}, {'results': [28, 7], 'layers': [], 'backorder': [[4, 7], [1, 7]]}], ['cheap backorder first', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 3], ['recv', 1, 44], ['issue', 2], ['recv', 2, 30]]}, {'results': [0, 60, 24, 88, -4], 'layers': [], 'backorder': [[1, 44]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 200, 30], 'layers': [[3, 50]], 'backorder': []}], ['never received', {'default_cost': 35, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [70, 0], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 30]]}, {'results': [0, 40, 10, 90, 0], 'layers': [[1, 30]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 6], ['recv', 7, 50]]}, {'results': [0, 360, -50], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 5], ['issue', 1]]}, {'results': [35, 7], 'layers': [], 'backorder': [[5, 7], [1, 7]]}], ['cheap backorder first', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 3], ['recv', 1, 45], ['issue', 2], ['recv', 2, 30]]}, {'results': [0, 60, 25, 90, -5], 'layers': [], 'backorder': [[1, 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 = []
    neg = []
    last = x['default_cost']
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            adj = 0
            while q > 0 and neg:
                k = max(range(len(neg)), key=lambda i: neg[i][1])
                t = min(q, neg[k][0])
                adj += t * (c - neg[k][1])
                neg[k][0] -= t
                q -= t
                if neg[k][0] == 0:
                    neg.pop(k)
            if q > 0:
                layers.append([q, c])
            last = c
            out.append(adj)
        else:
            q = ev[1]
            cost = 0
            while q > 0 and layers:
                t = min(q, layers[0][0])
                cost += t * layers[0][1]
                layers[0][0] -= t
                q -= t
                if layers[0][0] == 0:
                    layers.pop(0)
            if q > 0:
                neg.append([q, last])
                cost += q * last
            out.append(cost)
    return {'results': out, 'layers': layers, 'backorder': neg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 46]]}, {'results': [0, 200, 18], 'layers': [[3, 46]], 'backorder': []}], ['never received', {'default_cost': 31, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [62, 8], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 26]]}, {'results': [0, 40, 10, 90, -12], 'layers': [[1, 26]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 2], ['recv', 3, 50]]}, {'results': [0, 120, -10], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 1], ['issue', 1]]}, {'results': [7, 7], 'layers': [], 'backorder': [[1, 7], [1, 7]]}], ['cheap backorder first', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 3], ['recv', 1, 41], ['issue', 2], ['recv', 2, 30]]}, {'results': [0, 60, 21, 82, -1], 'layers': [], 'backorder': [[1, 41]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 47]]}, {'results': [0, 200, 21], 'layers': [[3, 47]], 'backorder': []}], ['never received', {'default_cost': 32, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [64, 6], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 27]]}, {'results': [0, 40, 10, 90, -9], 'layers': [[1, 27]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 3], ['recv', 4, 50]]}, {'results': [0, 180, -20], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 2], ['issue', 1]]}, {'results': [14, 7], 'layers': [], 'backorder': [[2, 7], [1, 7]]}], ['cheap backorder first', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 3], ['recv', 1, 42], ['issue', 2], ['recv', 2, 30]]}, {'results': [0, 60, 22, 84, -2], 'layers': [], 'backorder': [[1, 42]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 48]]}, {'results': [0, 200, 24], 'layers': [[3, 48]], 'backorder': []}], ['never received', {'default_cost': 33, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [66, 4], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 28]]}, {'results': [0, 40, 10, 90, -6], 'layers': [[1, 28]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 4], ['recv', 5, 50]]}, {'results': [0, 240, -30], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 3], ['issue', 1]]}, {'results': [21, 7], 'layers': [], 'backorder': [[3, 7], [1, 7]]}], ['cheap backorder first', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 3], ['recv', 1, 43], ['issue', 2], ['recv', 2, 30]]}, {'results': [0, 60, 23, 86, -3], 'layers': [], 'backorder': [[1, 43]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 49]]}, {'results': [0, 200, 27], 'layers': [[3, 49]], 'backorder': []}], ['never received', {'default_cost': 34, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [68, 2], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 29]]}, {'results': [0, 40, 10, 90, -3], 'layers': [[1, 29]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 300, -40], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 4], ['issue', 1]]}, {'results': [28, 7], 'layers': [], 'backorder': [[4, 7], [1, 7]]}], ['cheap backorder first', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 3], ['recv', 1, 44], ['issue', 2], ['recv', 2, 30]]}, {'results': [0, 60, 24, 88, -4], 'layers': [], 'backorder': [[1, 44]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 200, 30], 'layers': [[3, 50]], 'backorder': []}], ['never received', {'default_cost': 35, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [70, 0], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 30]]}, {'results': [0, 40, 10, 90, 0], 'layers': [[1, 30]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 6], ['recv', 7, 50]]}, {'results': [0, 360, -50], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 5], ['issue', 1]]}, {'results': [35, 7], 'layers': [], 'backorder': [[5, 7], [1, 7]]}], ['cheap backorder first', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 3], ['recv', 1, 45], ['issue', 2], ['recv', 2, 30]]}, {'results': [0, 60, 25, 90, -5], 'layers': [], 'backorder': [[1, 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:
The newest backorder is settled first, attaching the wrong provisional cost to the true-up.

Events: receipts ["recv",qty,cost] and issues ["issue",qty]. Issues draw FIFO layers; any shortfall is issued provisionally at the last receipt cost (default_cost before any receipt) and queued as a backorder. A receipt first settles backorders oldest first, returning the true-up qty*(receipt cost - provisional cost); only the remainder becomes a layer. Return {results, layers, backorder}.

split: open-access


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

N = 1
observations = []
def solve(x):
    layers = []
    neg = []
    last = x['default_cost']
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            adj = 0
            while q > 0 and neg:
                k = 0
                t = min(q, neg[k][0])
                adj += t * (c - neg[k][1])
                neg[k][0] -= t
                q -= t
                if neg[k][0] == 0:
                    neg.pop(k)
            if q > 0:
                layers.append([q, c])
            last = c
            out.append(adj)
        else:
            q = ev[1]
            cost = 0
            while q > 0 and layers:
                t = min(q, layers[0][0])
                cost += t * layers[0][1]
                layers[0][0] -= t
                q -= t
                if layers[0][0] == 0:
                    layers.pop(0)
            if q > 0:
                neg.append([q, last])
            out.append(cost)
    return {'results': out, 'layers': layers, 'backorder': neg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 46]]}, {'results': [0, 200, 18], 'layers': [[3, 46]], 'backorder': []}], ['never received', {'default_cost': 31, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [62, 8], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 26]]}, {'results': [0, 40, 10, 90, -12], 'layers': [[1, 26]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 2], ['recv', 3, 50]]}, {'results': [0, 120, -10], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 1], ['issue', 1]]}, {'results': [7, 7], 'layers': [], 'backorder': [[1, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 47]]}, {'results': [0, 200, 21], 'layers': [[3, 47]], 'backorder': []}], ['never received', {'default_cost': 32, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [64, 6], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 27]]}, {'results': [0, 40, 10, 90, -9], 'layers': [[1, 27]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 3], ['recv', 4, 50]]}, {'results': [0, 180, -20], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 2], ['issue', 1]]}, {'results': [14, 7], 'layers': [], 'backorder': [[2, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 48]]}, {'results': [0, 200, 24], 'layers': [[3, 48]], 'backorder': []}], ['never received', {'default_cost': 33, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [66, 4], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 28]]}, {'results': [0, 40, 10, 90, -6], 'layers': [[1, 28]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 4], ['recv', 5, 50]]}, {'results': [0, 240, -30], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 3], ['issue', 1]]}, {'results': [21, 7], 'layers': [], 'backorder': [[3, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 49]]}, {'results': [0, 200, 27], 'layers': [[3, 49]], 'backorder': []}], ['never received', {'default_cost': 34, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [68, 2], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 29]]}, {'results': [0, 40, 10, 90, -3], 'layers': [[1, 29]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 300, -40], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 4], ['issue', 1]]}, {'results': [28, 7], 'layers': [], 'backorder': [[4, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 200, 30], 'layers': [[3, 50]], 'backorder': []}], ['never received', {'default_cost': 35, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [70, 0], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 30]]}, {'results': [0, 40, 10, 90, 0], 'layers': [[1, 30]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 6], ['recv', 7, 50]]}, {'results': [0, 360, -50], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 5], ['issue', 1]]}, {'results': [35, 7], 'layers': [], 'backorder': [[5, 7], [1, 7]]}]]]
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 = []
    neg = []
    last = x['default_cost']
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            q, c = ev[1], ev[2]
            adj = 0
            while q > 0 and neg:
                k = 0
                t = min(q, neg[k][0])
                adj += t * (c - neg[k][1])
                neg[k][0] -= t
                q -= t
                if neg[k][0] == 0:
                    neg.pop(k)
            if q > 0:
                layers.append([q, c])
            last = c
            out.append(adj)
        else:
            q = ev[1]
            cost = 0
            while q > 0 and layers:
                t = min(q, layers[0][0])
                cost += t * layers[0][1]
                layers[0][0] -= t
                q -= t
                if layers[0][0] == 0:
                    layers.pop(0)
            if q > 0:
                neg.append([q, last])
                cost += q * x['default_cost']
            out.append(cost)
    return {'results': out, 'layers': layers, 'backorder': neg}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 46]]}, {'results': [0, 200, 18], 'layers': [[3, 46]], 'backorder': []}], ['never received', {'default_cost': 31, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [62, 8], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 26]]}, {'results': [0, 40, 10, 90, -12], 'layers': [[1, 26]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 2], ['recv', 3, 50]]}, {'results': [0, 120, -10], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 1], ['issue', 1]]}, {'results': [7, 7], 'layers': [], 'backorder': [[1, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 47]]}, {'results': [0, 200, 21], 'layers': [[3, 47]], 'backorder': []}], ['never received', {'default_cost': 32, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [64, 6], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 27]]}, {'results': [0, 40, 10, 90, -9], 'layers': [[1, 27]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 3], ['recv', 4, 50]]}, {'results': [0, 180, -20], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 2], ['issue', 1]]}, {'results': [14, 7], 'layers': [], 'backorder': [[2, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 48]]}, {'results': [0, 200, 24], 'layers': [[3, 48]], 'backorder': []}], ['never received', {'default_cost': 33, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [66, 4], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 28]]}, {'results': [0, 40, 10, 90, -6], 'layers': [[1, 28]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 4], ['recv', 5, 50]]}, {'results': [0, 240, -30], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 3], ['issue', 1]]}, {'results': [21, 7], 'layers': [], 'backorder': [[3, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 49]]}, {'results': [0, 200, 27], 'layers': [[3, 49]], 'backorder': []}], ['never received', {'default_cost': 34, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [68, 2], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 29]]}, {'results': [0, 40, 10, 90, -3], 'layers': [[1, 29]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 300, -40], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 4], ['issue', 1]]}, {'results': [28, 7], 'layers': [], 'backorder': [[4, 7], [1, 7]]}]], [['backorder settled', {'default_cost': 50, 'events': [['recv', 2, 40], ['issue', 5], ['recv', 6, 50]]}, {'results': [0, 200, 30], 'layers': [[3, 50]], 'backorder': []}], ['never received', {'default_cost': 35, 'events': [['issue', 2], ['recv', 3, 35]]}, {'results': [70, 0], 'layers': [[1, 35]], 'backorder': []}], ['partial settle', {'default_cost': 10, 'events': [['recv', 1, 12], ['issue', 4], ['recv', 2, 15], ['issue', 1], ['recv', 5, 11]]}, {'results': [0, 48, 6, 15, -5], 'layers': [[3, 11]], 'backorder': []}], ['two backorders', {'default_cost': 10, 'events': [['recv', 1, 20], ['issue', 2], ['recv', 1, 30], ['issue', 3], ['recv', 4, 30]]}, {'results': [0, 40, 10, 90, 0], 'layers': [[1, 30]], 'backorder': []}], ['no negative', {'default_cost': 10, 'events': [['recv', 5, 8], ['issue', 3], ['recv', 1, 9]]}, {'results': [0, 24, 0], 'layers': [[2, 8], [1, 9]], 'backorder': []}], ['cheaper receipt', {'default_cost': 99, 'events': [['recv', 1, 60], ['issue', 6], ['recv', 7, 50]]}, {'results': [0, 360, -50], 'layers': [[2, 50]], 'backorder': []}], ['left negative', {'default_cost': 7, 'events': [['issue', 5], ['issue', 1]]}, {'results': [35, 7], 'layers': [], 'backorder': [[5, 7], [1, 7]]}]]]
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 that run stock negative report only the cost of units actually on hand.

Events: receipts ["recv",qty,cost] and issues ["issue",qty]. Issues draw FIFO layers; any shortfall is issued provisionally at the last receipt cost (default_cost before any receipt) and queued as a backorder. A receipt first settles backorders oldest first, returning the true-up qty*(receipt cost - provisional cost); only the remainder becomes a layer. Return {results, layers, backorder}.

split: open-access


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

N = 1
observations = []
def solve(x):
    src = [list(l) for l in x['src']]
    dst = [list(l) for l in x['dst']]
    q = x['qty']
    if q <= 0 or q > sum(l[0] for l in src):
        return {'error': 'invalid quantity'}
    pieces = []
    while q > 0:
        lq, lv = src[0]
        t = min(q, lq)
        val = lv if t == lq else t * (lv // lq)
        pieces.append([t, val])
        src[0] = [lq - t, lv - val]
        if src[0][0] == 0:
            src.pop(0)
        q -= t
    fr = x['freight']
    tq = x['qty']
    shares = [fr * p[0] // tq for p in pieces]
    shares[0] += fr - sum(shares)
    for p, s in zip(pieces, shares):
        dst.append([p[0], p[1] + s])
    return {'src': src, 'dst': dst}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 11}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 78]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 1}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 75]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 8}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 8]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 14}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 62], [3, 188]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 4, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 12]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 12]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 12}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 79]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 2}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 76]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 9}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 9]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 15}, {'src': [[2, 120]], 'dst': [[1, 53], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 5, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 13]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 13]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 13}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 80]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 3}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 77]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 10}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 10]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 16}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 6, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 14]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 14]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 14}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 81]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 4}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 78]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 11}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 11]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 17}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 7, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 15]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 15]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 15}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 82]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 5}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 79]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 12}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 12]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 18}, {'src': [[2, 120]], 'dst': [[1, 55], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 8, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 16]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 16]]}]]]
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):
    src = [list(l) for l in x['src']]
    dst = [list(l) for l in x['dst']]
    q = x['qty']
    if q <= 0 or q > sum(l[0] for l in src):
        return {'error': 'invalid quantity'}
    pieces = []
    while q > 0:
        lq, lv = src[0]
        t = min(q, lq)
        val = lv if t == lq else (t * lv + lq - 1) // lq
        pieces.append([t, val])
        src[0] = [lq - t, lv - val]
        if src[0][0] == 0:
            src.pop(0)
        q -= t
    fr = x['freight']
    tq = x['qty']
    shares = [fr * p[0] // tq for p in pieces]
    shares[0] += fr - sum(shares)
    for p, s in zip(pieces, shares):
        dst.append([p[0], p[1] + s])
    return {'src': src, 'dst': dst}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 11}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 78]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 1}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 75]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 8}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 8]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 14}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 62], [3, 188]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 4, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 12]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 12]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 12}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 79]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 2}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 76]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 9}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 9]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 15}, {'src': [[2, 120]], 'dst': [[1, 53], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 5, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 13]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 13]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 13}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 80]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 3}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 77]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 10}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 10]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 16}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 6, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 14]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 14]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 14}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 81]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 4}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 78]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 11}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 11]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 17}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 7, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 15]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 15]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 15}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 82]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 5}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 79]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 12}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 12]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 18}, {'src': [[2, 120]], 'dst': [[1, 55], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 8, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 16]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 16]]}]]]
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:
Transferred value is short by a few cents on every partial draw and the residue sticks in the source.

Layers are [qty, total value]. A transfer of qty draws FIFO from src; a partial draw takes floor(t*value/qty) and leaves the residue in src; a layer is removed when its quantity reaches zero. Freight is split across the drawn pieces by quantity (floor), residual cents to the first piece, and each piece is appended to dst in draw order as the newest layers. qty<=0 or qty above src stock returns {"error"}. Return {src, dst}.

split: open-access