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

N = 1
observations = []
def solve(x):
    layers = []
    seq = 0
    sold = {}
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            seq += 1
            layers.append([seq, ev[1], ev[2]])
            out.append(seq)
        elif ev[0] == 'sale':
            q = ev[2]
            if q > sum(l[1] for l in layers):
                out.append('short')
                continue
            took = []
            cost = 0
            while q > 0:
                l = layers[0]
                t = min(q, l[1])
                took.append([l[0], t, l[2]])
                cost += t * l[2]
                l[1] -= t
                q -= t
                if l[1] == 0:
                    layers.pop(0)
            sold[ev[1]] = took
            out.append(cost)
        else:
            took = sold.get(ev[1], [])
            q = ev[2]
            if q > sum(t[1] for t in took):
                out.append('rejected')
                continue
            credit = 0
            while q > 0:
                j = 0
                t = took[j]
                r = min(q, t[1])
                credit += r * t[2]
                t[1] -= r
                q -= r
                if t[1] == 0:
                    took.pop(j)
                match = [l for l in layers if l[0] == t[0]]
                if match:
                    match[0][1] += r
                else:
                    layers.append([t[0], r, t[2]])
                    layers.sort(key=lambda l: l[0])
            out.append(credit)
    return {'results': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 9], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 19, 19, 10], 'layers': [[2, 3, 9], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 3]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['falling prices', {'events': [['recv', 2, 50], ['recv', 3, 21], ['sale', 's1', 4], ['return', 's1', 1]]}, {'results': [1, 2, 142, 21], 'layers': [[2, 2, 21]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 10], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 20, 20, 10], 'layers': [[2, 3, 10], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 4]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['falling prices', {'events': [['recv', 2, 50], ['recv', 3, 22], ['sale', 's1', 4], ['return', 's1', 1]]}, {'results': [1, 2, 144, 22], 'layers': [[2, 2, 22]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 11], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 21, 21, 10], 'layers': [[2, 3, 11], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 5]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['falling prices', {'events': [['recv', 2, 50], ['recv', 3, 23], ['sale', 's1', 4], ['return', 's1', 1]]}, {'results': [1, 2, 146, 23], 'layers': [[2, 2, 23]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 12], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 22, 22, 10], 'layers': [[2, 3, 12], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 6]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['falling prices', {'events': [['recv', 2, 50], ['recv', 3, 24], ['sale', 's1', 4], ['return', 's1', 1]]}, {'results': [1, 2, 148, 24], 'layers': [[2, 2, 24]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 13], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 23, 23, 10], 'layers': [[2, 3, 13], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 7]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['falling prices', {'events': [['recv', 2, 50], ['recv', 3, 25], ['sale', 's1', 4], ['return', 's1', 1]]}, {'results': [1, 2, 150, 25], 'layers': [[2, 2, 25]]}]]]
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 = []
    seq = 0
    sold = {}
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            seq += 1
            layers.append([seq, ev[1], ev[2]])
            out.append(seq)
        elif ev[0] == 'sale':
            q = ev[2]
            if q > sum(l[1] for l in layers):
                out.append('short')
                continue
            took = []
            cost = 0
            while q > 0:
                l = layers[0]
                t = min(q, l[1])
                took.append([l[0], t, l[2]])
                cost += t * l[2]
                l[1] -= t
                q -= t
                if l[1] == 0:
                    layers.pop(0)
            sold[ev[1]] = took
            out.append(cost)
        else:
            took = sold.get(ev[1], [])
            q = ev[2]
            if q > sum(t[1] for t in took):
                out.append('rejected')
                continue
            credit = 0
            while q > 0:
                j = max(range(len(took)), key=lambda i: took[i][2])
                t = took[j]
                r = min(q, t[1])
                credit += r * t[2]
                t[1] -= r
                q -= r
                if t[1] == 0:
                    took.pop(j)
                match = [l for l in layers if l[0] == t[0]]
                if match:
                    match[0][1] += r
                else:
                    layers.append([t[0], r, t[2]])
                    layers.sort(key=lambda l: l[0])
            out.append(credit)
    return {'results': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 9], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 19, 19, 10], 'layers': [[2, 3, 9], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 3]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['falling prices', {'events': [['recv', 2, 50], ['recv', 3, 21], ['sale', 's1', 4], ['return', 's1', 1]]}, {'results': [1, 2, 142, 21], 'layers': [[2, 2, 21]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 10], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 20, 20, 10], 'layers': [[2, 3, 10], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 4]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['falling prices', {'events': [['recv', 2, 50], ['recv', 3, 22], ['sale', 's1', 4], ['return', 's1', 1]]}, {'results': [1, 2, 144, 22], 'layers': [[2, 2, 22]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 11], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 21, 21, 10], 'layers': [[2, 3, 11], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 5]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['falling prices', {'events': [['recv', 2, 50], ['recv', 3, 23], ['sale', 's1', 4], ['return', 's1', 1]]}, {'results': [1, 2, 146, 23], 'layers': [[2, 2, 23]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 12], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 22, 22, 10], 'layers': [[2, 3, 12], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 6]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['falling prices', {'events': [['recv', 2, 50], ['recv', 3, 24], ['sale', 's1', 4], ['return', 's1', 1]]}, {'results': [1, 2, 148, 24], 'layers': [[2, 2, 24]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 13], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 23, 23, 10], 'layers': [[2, 3, 13], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 7]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['falling prices', {'events': [['recv', 2, 50], ['recv', 3, 25], ['sale', 's1', 4], ['return', 's1', 1]]}, {'results': [1, 2, 150, 25], 'layers': [[2, 2, 25]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A partial return is credited at the oldest layer cost of the sale instead of the units most recently shipped.

Events: ["recv",qty,cost] opens a layer with the next receipt sequence; ["sale",id,qty] consumes FIFO and records the pieces taken; ["return",id,qty] restocks units at the cost they left with, last-consumed piece first, into the layer with the same receipt sequence (recreated in sequence order if gone). Returns above the unreturned quantity of that sale are "rejected"; sales above stock are "short". Return {results, layers:[[seq,qty,cost]]}.

split: open-access


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

N = 1
observations = []
def solve(x):
    layers = []
    seq = 0
    sold = {}
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            seq += 1
            layers.append([seq, ev[1], ev[2]])
            out.append(seq)
        elif ev[0] == 'sale':
            q = ev[2]
            if q > sum(l[1] for l in layers):
                out.append('short')
                continue
            took = []
            cost = 0
            while q > 0:
                l = layers[0]
                t = min(q, l[1])
                took.append([l[0], t, l[2]])
                cost += t * l[2]
                l[1] -= t
                q -= t
                if l[1] == 0:
                    layers.pop(0)
            sold[ev[1]] = took
            out.append(cost)
        else:
            took = sold.get(ev[1], [])
            q = ev[2]
            if q >= sum(t[1] for t in took):
                out.append('rejected')
                continue
            credit = 0
            while q > 0:
                j = -1
                t = took[j]
                r = min(q, t[1])
                credit += r * t[2]
                t[1] -= r
                q -= r
                if t[1] == 0:
                    took.pop(j)
                match = [l for l in layers if l[0] == t[0]]
                if match:
                    match[0][1] += r
                else:
                    layers.append([t[0], r, t[2]])
                    layers.sort(key=lambda l: l[0])
            out.append(credit)
    return {'results': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 9], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 19, 19, 10], 'layers': [[2, 3, 9], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 3]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 10], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 20, 20, 10], 'layers': [[2, 3, 10], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 4]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 11], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 21, 21, 10], 'layers': [[2, 3, 11], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 5]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 12], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 22, 22, 10], 'layers': [[2, 3, 12], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 6]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 13], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 23, 23, 10], 'layers': [[2, 3, 13], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 7]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]]]
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 = []
    seq = 0
    sold = {}
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            seq += 1
            layers.append([seq, ev[1], ev[2]])
            out.append(seq)
        elif ev[0] == 'sale':
            q = ev[2]
            if q > sum(l[1] for l in layers):
                out.append('short')
                continue
            took = []
            cost = 0
            while q > 0:
                l = layers[0]
                t = min(q, l[1])
                took.append([l[0], t, l[2]])
                cost += t * l[2]
                l[1] -= t
                q -= t
                if l[1] == 0:
                    layers.pop(0)
            sold[ev[1]] = took
            out.append(cost)
        else:
            took = sold.get(ev[1], [])
            q = ev[2]
            if q > sum(t[1] for t in took) or (q == sum(t[1] for t in took) and q > 1):
                out.append('rejected')
                continue
            credit = 0
            while q > 0:
                j = -1
                t = took[j]
                r = min(q, t[1])
                credit += r * t[2]
                t[1] -= r
                q -= r
                if t[1] == 0:
                    took.pop(j)
                match = [l for l in layers if l[0] == t[0]]
                if match:
                    match[0][1] += r
                else:
                    layers.append([t[0], r, t[2]])
                    layers.sort(key=lambda l: l[0])
            out.append(credit)
    return {'results': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 9], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 19, 19, 10], 'layers': [[2, 3, 9], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 3]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 10], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 20, 20, 10], 'layers': [[2, 3, 10], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 4]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 11], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 21, 21, 10], 'layers': [[2, 3, 11], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 5]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 12], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 22, 22, 10], 'layers': [[2, 3, 12], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 6]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 13], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 23, 23, 10], 'layers': [[2, 3, 13], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 7]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]]]
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:
Returning the entire unreturned quantity of a sale is rejected.

Events: ["recv",qty,cost] opens a layer with the next receipt sequence; ["sale",id,qty] consumes FIFO and records the pieces taken; ["return",id,qty] restocks units at the cost they left with, last-consumed piece first, into the layer with the same receipt sequence (recreated in sequence order if gone). Returns above the unreturned quantity of that sale are "rejected"; sales above stock are "short". Return {results, layers:[[seq,qty,cost]]}.

split: open-access


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

N = 1
observations = []
def solve(x):
    layers = []
    seq = 0
    sold = {}
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            seq += 1
            layers.append([seq, ev[1], ev[2]])
            out.append(seq)
        elif ev[0] == 'sale':
            q = ev[2]
            if q > sum(l[1] for l in layers):
                out.append('short')
                continue
            took = []
            cost = 0
            while q > 0:
                l = layers[0]
                t = min(q, l[1])
                took.append([l[0], t, l[2]])
                cost += t * l[2]
                l[1] -= t
                q -= t
                if l[1] == 0:
                    layers.pop(0)
            sold[ev[1]] = took
            out.append(cost)
        else:
            took = sold.get(ev[1], [])
            q = ev[2]
            if q > sum(t[1] for t in took):
                out.append('rejected')
                continue
            credit = 0
            while q > 0:
                j = -1
                t = took[j]
                r = min(q, t[1])
                credit += r * t[2]
                t[1] -= r
                q -= r
                if t[1] == 0:
                    took.pop(j)
                match = [l for l in layers if l[0] == t[0]]
                if match:
                    match[0][1] += r
                else:
                    layers.append([t[0], r, t[2]])
            out.append(credit)
    return {'results': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 9], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 19, 19, 10], 'layers': [[2, 3, 9], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 3]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['restore with falling prices', {'events': [['recv', 2, 10], ['recv', 3, 5], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 1]]}, {'results': [1, 2, 25, 25, 10], 'layers': [[1, 1, 10], [2, 3, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 10], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 20, 20, 10], 'layers': [[2, 3, 10], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 4]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['restore with falling prices', {'events': [['recv', 2, 11], ['recv', 3, 5], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 1]]}, {'results': [1, 2, 27, 27, 11], 'layers': [[1, 1, 11], [2, 3, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 11], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 21, 21, 10], 'layers': [[2, 3, 11], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 5]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['restore with falling prices', {'events': [['recv', 2, 12], ['recv', 3, 5], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 1]]}, {'results': [1, 2, 29, 29, 12], 'layers': [[1, 1, 12], [2, 3, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 12], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 22, 22, 10], 'layers': [[2, 3, 12], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 6]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['restore with falling prices', {'events': [['recv', 2, 13], ['recv', 3, 5], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 1]]}, {'results': [1, 2, 31, 31, 13], 'layers': [[1, 1, 13], [2, 3, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 13], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 23, 23, 10], 'layers': [[2, 3, 13], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 7]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['restore with falling prices', {'events': [['recv', 2, 14], ['recv', 3, 5], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 1]]}, {'results': [1, 2, 33, 33, 14], 'layers': [[1, 1, 14], [2, 3, 5]]}]]]
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 = []
    seq = 0
    sold = {}
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            seq += 1
            layers.append([seq, ev[1], ev[2]])
            out.append(seq)
        elif ev[0] == 'sale':
            q = ev[2]
            if q > sum(l[1] for l in layers):
                out.append('short')
                continue
            took = []
            cost = 0
            while q > 0:
                l = layers[0]
                t = min(q, l[1])
                took.append([l[0], t, l[2]])
                cost += t * l[2]
                l[1] -= t
                q -= t
                if l[1] == 0:
                    layers.pop(0)
            sold[ev[1]] = took
            out.append(cost)
        else:
            took = sold.get(ev[1], [])
            q = ev[2]
            if q > sum(t[1] for t in took):
                out.append('rejected')
                continue
            credit = 0
            while q > 0:
                j = -1
                t = took[j]
                r = min(q, t[1])
                credit += r * t[2]
                t[1] -= r
                q -= r
                if t[1] == 0:
                    took.pop(j)
                match = [l for l in layers if l[0] == t[0]]
                if match:
                    match[0][1] += r
                else:
                    layers.append([t[0], r, t[2]])
                    layers.sort(key=lambda l: l[2])
            out.append(credit)
    return {'results': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 9], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 19, 19, 10], 'layers': [[2, 3, 9], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 3]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['restore with falling prices', {'events': [['recv', 2, 10], ['recv', 3, 5], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 1]]}, {'results': [1, 2, 25, 25, 10], 'layers': [[1, 1, 10], [2, 3, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 10], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 20, 20, 10], 'layers': [[2, 3, 10], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 4]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['restore with falling prices', {'events': [['recv', 2, 11], ['recv', 3, 5], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 1]]}, {'results': [1, 2, 27, 27, 11], 'layers': [[1, 1, 11], [2, 3, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 11], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 21, 21, 10], 'layers': [[2, 3, 11], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 5]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['restore with falling prices', {'events': [['recv', 2, 12], ['recv', 3, 5], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 1]]}, {'results': [1, 2, 29, 29, 12], 'layers': [[1, 1, 12], [2, 3, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 12], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 22, 22, 10], 'layers': [[2, 3, 12], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 6]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['restore with falling prices', {'events': [['recv', 2, 13], ['recv', 3, 5], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 1]]}, {'results': [1, 2, 31, 31, 13], 'layers': [[1, 1, 13], [2, 3, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 13], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 23, 23, 10], 'layers': [[2, 3, 13], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 7]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['restore with falling prices', {'events': [['recv', 2, 14], ['recv', 3, 5], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 1]]}, {'results': [1, 2, 33, 33, 14], 'layers': [[1, 1, 14], [2, 3, 5]]}]]]
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:
Units restored to an exhausted receipt layer are issued after newer receipts.

Events: ["recv",qty,cost] opens a layer with the next receipt sequence; ["sale",id,qty] consumes FIFO and records the pieces taken; ["return",id,qty] restocks units at the cost they left with, last-consumed piece first, into the layer with the same receipt sequence (recreated in sequence order if gone). Returns above the unreturned quantity of that sale are "rejected"; sales above stock are "short". Return {results, layers:[[seq,qty,cost]]}.

split: open-access


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

N = 1
observations = []
def solve(x):
    layers = []
    seq = 0
    sold = {}
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            seq += 1
            layers.append([seq, ev[1], ev[2]])
            out.append(seq)
        elif ev[0] == 'sale':
            q = ev[2]
            if q > sum(l[1] for l in layers):
                out.append('short')
                continue
            took = []
            cost = 0
            while q > 0:
                l = layers[0]
                t = min(q, l[1])
                took.append([l[0], t, l[2]])
                cost += t * l[2]
                l[1] -= t
                q -= t
                if l[1] == 0:
                    layers.pop(0)
            sold[ev[1]] = took
            out.append(cost)
        else:
            took = sold.get(ev[1], [])
            q = ev[2]
            if q > sum(t[1] for t in took):
                out.append('rejected')
                continue
            credit = 0
            while q > 0:
                j = -1
                t = took[j]
                r = min(q, t[1])
                credit += r * t[2]
                t[1] -= r
                q -= r
                if t[1] == 0:
                    took.pop(j)
                match = []
                if match:
                    match[0][1] += r
                else:
                    layers.append([t[0], r, t[2]])
                    layers.sort(key=lambda l: l[0])
            out.append(credit)
    return {'results': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 9], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 19, 19, 10], 'layers': [[2, 3, 9], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 3]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['merge into second layer', {'events': [['recv', 2, 3], ['recv', 4, 7], ['sale', 's1', 3], ['return', 's1', 1]]}, {'results': [1, 2, 13, 7], 'layers': [[2, 4, 7]]}], ['same price receipts', {'events': [['recv', 2, 5], ['recv', 4, 5], ['sale', 's1', 2], ['return', 's1', 1]]}, {'results': [1, 2, 10, 5], 'layers': [[1, 1, 5], [2, 4, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 10], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 20, 20, 10], 'layers': [[2, 3, 10], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 4]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['merge into second layer', {'events': [['recv', 2, 3], ['recv', 4, 8], ['sale', 's1', 3], ['return', 's1', 1]]}, {'results': [1, 2, 14, 8], 'layers': [[2, 4, 8]]}], ['same price receipts', {'events': [['recv', 2, 5], ['recv', 5, 5], ['sale', 's1', 2], ['return', 's1', 1]]}, {'results': [1, 2, 10, 5], 'layers': [[1, 1, 5], [2, 5, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 11], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 21, 21, 10], 'layers': [[2, 3, 11], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 5]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['merge into second layer', {'events': [['recv', 2, 3], ['recv', 4, 9], ['sale', 's1', 3], ['return', 's1', 1]]}, {'results': [1, 2, 15, 9], 'layers': [[2, 4, 9]]}], ['same price receipts', {'events': [['recv', 2, 5], ['recv', 6, 5], ['sale', 's1', 2], ['return', 's1', 1]]}, {'results': [1, 2, 10, 5], 'layers': [[1, 1, 5], [2, 6, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 12], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 22, 22, 10], 'layers': [[2, 3, 12], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 6]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['merge into second layer', {'events': [['recv', 2, 3], ['recv', 4, 10], ['sale', 's1', 3], ['return', 's1', 1]]}, {'results': [1, 2, 16, 10], 'layers': [[2, 4, 10]]}], ['same price receipts', {'events': [['recv', 2, 5], ['recv', 7, 5], ['sale', 's1', 2], ['return', 's1', 1]]}, {'results': [1, 2, 10, 5], 'layers': [[1, 1, 5], [2, 7, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 13], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 23, 23, 10], 'layers': [[2, 3, 13], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 7]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['merge into second layer', {'events': [['recv', 2, 3], ['recv', 4, 11], ['sale', 's1', 3], ['return', 's1', 1]]}, {'results': [1, 2, 17, 11], 'layers': [[2, 4, 11]]}], ['same price receipts', {'events': [['recv', 2, 5], ['recv', 8, 5], ['sale', 's1', 2], ['return', 's1', 1]]}, {'results': [1, 2, 10, 5], 'layers': [[1, 1, 5], [2, 8, 5]]}]]]
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 = []
    seq = 0
    sold = {}
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            seq += 1
            layers.append([seq, ev[1], ev[2]])
            out.append(seq)
        elif ev[0] == 'sale':
            q = ev[2]
            if q > sum(l[1] for l in layers):
                out.append('short')
                continue
            took = []
            cost = 0
            while q > 0:
                l = layers[0]
                t = min(q, l[1])
                took.append([l[0], t, l[2]])
                cost += t * l[2]
                l[1] -= t
                q -= t
                if l[1] == 0:
                    layers.pop(0)
            sold[ev[1]] = took
            out.append(cost)
        else:
            took = sold.get(ev[1], [])
            q = ev[2]
            if q > sum(t[1] for t in took):
                out.append('rejected')
                continue
            credit = 0
            while q > 0:
                j = -1
                t = took[j]
                r = min(q, t[1])
                credit += r * t[2]
                t[1] -= r
                q -= r
                if t[1] == 0:
                    took.pop(j)
                match = [l for l in layers if l[2] == t[2]]
                if match:
                    match[0][1] += r
                else:
                    layers.append([t[0], r, t[2]])
                    layers.sort(key=lambda l: l[0])
            out.append(credit)
    return {'results': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 9], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 19, 19, 10], 'layers': [[2, 3, 9], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 3]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['merge into second layer', {'events': [['recv', 2, 3], ['recv', 4, 7], ['sale', 's1', 3], ['return', 's1', 1]]}, {'results': [1, 2, 13, 7], 'layers': [[2, 4, 7]]}], ['same price receipts', {'events': [['recv', 2, 5], ['recv', 4, 5], ['sale', 's1', 2], ['return', 's1', 1]]}, {'results': [1, 2, 10, 5], 'layers': [[1, 1, 5], [2, 4, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 10], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 20, 20, 10], 'layers': [[2, 3, 10], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 4]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['merge into second layer', {'events': [['recv', 2, 3], ['recv', 4, 8], ['sale', 's1', 3], ['return', 's1', 1]]}, {'results': [1, 2, 14, 8], 'layers': [[2, 4, 8]]}], ['same price receipts', {'events': [['recv', 2, 5], ['recv', 5, 5], ['sale', 's1', 2], ['return', 's1', 1]]}, {'results': [1, 2, 10, 5], 'layers': [[1, 1, 5], [2, 5, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 11], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 21, 21, 10], 'layers': [[2, 3, 11], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 5]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['merge into second layer', {'events': [['recv', 2, 3], ['recv', 4, 9], ['sale', 's1', 3], ['return', 's1', 1]]}, {'results': [1, 2, 15, 9], 'layers': [[2, 4, 9]]}], ['same price receipts', {'events': [['recv', 2, 5], ['recv', 6, 5], ['sale', 's1', 2], ['return', 's1', 1]]}, {'results': [1, 2, 10, 5], 'layers': [[1, 1, 5], [2, 6, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 12], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 22, 22, 10], 'layers': [[2, 3, 12], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 6]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['merge into second layer', {'events': [['recv', 2, 3], ['recv', 4, 10], ['sale', 's1', 3], ['return', 's1', 1]]}, {'results': [1, 2, 16, 10], 'layers': [[2, 4, 10]]}], ['same price receipts', {'events': [['recv', 2, 5], ['recv', 7, 5], ['sale', 's1', 2], ['return', 's1', 1]]}, {'results': [1, 2, 10, 5], 'layers': [[1, 1, 5], [2, 7, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 13], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 23, 23, 10], 'layers': [[2, 3, 13], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 7]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}], ['merge into second layer', {'events': [['recv', 2, 3], ['recv', 4, 11], ['sale', 's1', 3], ['return', 's1', 1]]}, {'results': [1, 2, 17, 11], 'layers': [[2, 4, 11]]}], ['same price receipts', {'events': [['recv', 2, 5], ['recv', 8, 5], ['sale', 's1', 2], ['return', 's1', 1]]}, {'results': [1, 2, 10, 5], 'layers': [[1, 1, 5], [2, 8, 5]]}]]]
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:
Returned units create a duplicate layer with the same receipt sequence as an open layer.

Events: ["recv",qty,cost] opens a layer with the next receipt sequence; ["sale",id,qty] consumes FIFO and records the pieces taken; ["return",id,qty] restocks units at the cost they left with, last-consumed piece first, into the layer with the same receipt sequence (recreated in sequence order if gone). Returns above the unreturned quantity of that sale are "rejected"; sales above stock are "short". Return {results, layers:[[seq,qty,cost]]}.

split: open-access


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

N = 1
observations = []
def solve(x):
    layers = []
    seq = 0
    sold = {}
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            seq += 1
            layers.append([seq, ev[1], ev[2]])
            out.append(seq)
        elif ev[0] == 'sale':
            q = ev[2]
            if q > sum(l[1] for l in layers):
                out.append('short')
                continue
            took = []
            cost = 0
            while q > 0:
                l = layers[0]
                t = min(q, l[1])
                took.append([l[0], t, l[2]])
                cost += t * l[2]
                l[1] -= t
                q -= t
                if l[1] == 0:
                    layers.pop(0)
            sold[ev[1]] = took
            out.append(cost)
        else:
            took = sold.get(ev[1], [])
            q = ev[2]
            if q > sum(t[1] for t in took):
                out.append('rejected')
                continue
            credit = 0
            while q > 0:
                j = -1
                t = took[j]
                r = min(q, t[1])
                credit += r * (layers[-1][2] if layers else t[2])
                t[1] -= r
                q -= r
                if t[1] == 0:
                    took.pop(j)
                match = [l for l in layers if l[0] == t[0]]
                if match:
                    match[0][1] += r
                else:
                    layers.append([t[0], r, t[2]])
                    layers.sort(key=lambda l: l[0])
            out.append(credit)
    return {'results': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 9], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 19, 19, 10], 'layers': [[2, 3, 9], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 3]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 10], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 20, 20, 10], 'layers': [[2, 3, 10], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 4]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 11], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 21, 21, 10], 'layers': [[2, 3, 11], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 5]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 12], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 22, 22, 10], 'layers': [[2, 3, 12], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 6]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 13], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 23, 23, 10], 'layers': [[2, 3, 13], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 7]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]]]
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 = []
    seq = 0
    sold = {}
    out = []
    for ev in x['events']:
        if ev[0] == 'recv':
            seq += 1
            layers.append([seq, ev[1], ev[2]])
            out.append(seq)
        elif ev[0] == 'sale':
            q = ev[2]
            if q > sum(l[1] for l in layers):
                out.append('short')
                continue
            took = []
            cost = 0
            while q > 0:
                l = layers[0]
                t = min(q, l[1])
                took.append([l[0], t, l[2]])
                cost += t * l[2]
                l[1] -= t
                q -= t
                if l[1] == 0:
                    layers.pop(0)
            sold[ev[1]] = took
            out.append(cost)
        else:
            took = sold.get(ev[1], [])
            q = ev[2]
            if q > sum(t[1] for t in took):
                out.append('rejected')
                continue
            credit = 0
            while q > 0:
                j = -1
                t = took[j]
                r = min(q, t[1])
                credit += r * (layers[0][2] if layers else t[2])
                t[1] -= r
                q -= r
                if t[1] == 0:
                    took.pop(j)
                match = [l for l in layers if l[0] == t[0]]
                if match:
                    match[0][1] += r
                else:
                    layers.append([t[0], r, t[2]])
                    layers.sort(key=lambda l: l[0])
            out.append(credit)
    return {'results': out, 'layers': layers}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 9], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 19, 19, 10], 'layers': [[2, 3, 9], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 3]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 10], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 20, 20, 10], 'layers': [[2, 3, 10], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 4]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 11], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 21, 21, 10], 'layers': [[2, 3, 11], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 5]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 1]]}, {'results': [1, 2, 58, 14], 'layers': [[2, 4, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 12], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 22, 22, 10], 'layers': [[2, 3, 12], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 6]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]], [['return last consumed', {'events': [['recv', 3, 10], ['recv', 5, 14], ['sale', 's1', 5], ['return', 's1', 2]]}, {'results': [1, 2, 58, 28], 'layers': [[2, 5, 14]]}], ['full return', {'events': [['recv', 2, 20], ['recv', 2, 30], ['sale', 's1', 4], ['return', 's1', 4]]}, {'results': [1, 2, 100, 100], 'layers': [[1, 2, 20], [2, 2, 30]]}], ['over return', {'events': [['recv', 4, 7], ['sale', 's1', 2], ['return', 's1', 1], ['return', 's1', 2]]}, {'results': [1, 14, 7, 'rejected'], 'layers': [[1, 3, 7]]}], ['restore into existing', {'events': [['recv', 4, 11], ['sale', 's1', 1], ['return', 's1', 1], ['sale', 's2', 4]]}, {'results': [1, 11, 11, 44], 'layers': []}], ['restore gone layer', {'events': [['recv', 2, 5], ['recv', 3, 13], ['recv', 1, 9], ['sale', 's1', 3], ['return', 's1', 3], ['sale', 's2', 2]]}, {'results': [1, 2, 3, 23, 23, 10], 'layers': [[2, 3, 13], [3, 1, 9]]}], ['unknown sale', {'events': [['recv', 2, 5], ['return', 'zz', 1]]}, {'results': [1, 'rejected'], 'layers': [[1, 2, 5]]}], ['short sale', {'events': [['recv', 1, 5], ['sale', 's1', 7]]}, {'results': [1, 'short'], 'layers': [[1, 1, 5]]}]]]
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:
Customer returns are credited at the latest receipt price rather than the cost the units shipped at.

Events: ["recv",qty,cost] opens a layer with the next receipt sequence; ["sale",id,qty] consumes FIFO and records the pieces taken; ["return",id,qty] restocks units at the cost they left with, last-consumed piece first, into the layer with the same receipt sequence (recreated in sequence order if gone). Returns above the unreturned quantity of that sale are "rejected"; sales above stock are "short". Return {results, layers:[[seq,qty,cost]]}.

split: open-access