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

N = 1
observations = []
def solve(x):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q + buy_q
    if end_q < 0 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in purchases[::-1]:
            if inc == 0:
                break
            take = min(inc, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin:
            take = min(rem, bq)
            if take > 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 4], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 48}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 5], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 50}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 6], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 52}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 7], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 54}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 8], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 56}]]]
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):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q + buy_q
    if end_q < 0 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in sorted(purchases, key=lambda p: p[1]):
            if inc == 0:
                break
            take = min(inc, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin:
            take = min(rem, bq)
            if take > 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 4], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 48}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 5], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 50}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 6], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 52}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 7], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 54}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}], ['non monotonic prices', {'begin': [[1, 1]], 'purchases': [[2, 9], [2, 8], [2, 20]], 'ending_qty': 3}, {'layers': [[1, 1], [2, 18]], 'ending_value': 19, 'cogs': 56}]]]
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 year-end LIFO increment is valued at the latest purchase prices, overstating ending inventory in inflationary years.

Input {begin:[[qty,unit]] oldest first, purchases:[[qty,unit]] chronological, ending_qty}. Ending inventory keeps the oldest units: if ending_qty covers the base, all base layers stay and one increment layer is priced at the EARLIEST purchases of the year; otherwise the newest base layers are liquidated. ending_qty outside [0, available] returns {"error"}. Return {layers:[[qty,value]], ending_value, cogs}.

split: open-access


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

N = 1
observations = []
def solve(x):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q + buy_q
    if end_q < 0 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in purchases:
            if inc == 0:
                break
            take = min(inc, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin[::-1]:
            take = min(rem, bq)
            if take > 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}]]]
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):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q + buy_q
    if end_q < 0 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in purchases:
            if inc == 0:
                break
            take = min(inc, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in sorted(begin, key=lambda l: -l[1]):
            take = min(rem, bq)
            if take > 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}]]]
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 LIFO liquidation keeps the newest base layers and discards the oldest, so the surviving layers carry recent prices.

Input {begin:[[qty,unit]] oldest first, purchases:[[qty,unit]] chronological, ending_qty}. Ending inventory keeps the oldest units: if ending_qty covers the base, all base layers stay and one increment layer is priced at the EARLIEST purchases of the year; otherwise the newest base layers are liquidated. ending_qty outside [0, available] returns {"error"}. Return {layers:[[qty,value]], ending_value, cogs}.

split: open-access


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

N = 1
observations = []
def solve(x):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = buy_q
    if end_q < 0 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in purchases:
            if inc == 0:
                break
            take = min(inc, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin:
            take = min(rem, bq)
            if take > 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}], ['uses both pools', {'begin': [[6, 2]], 'purchases': [[3, 5]], 'ending_qty': 8}, {'layers': [[6, 12], [2, 10]], 'ending_value': 22, 'cogs': 5}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}], ['uses both pools', {'begin': [[6, 2]], 'purchases': [[3, 5]], 'ending_qty': 7}, {'layers': [[6, 12], [1, 5]], 'ending_value': 17, 'cogs': 10}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}], ['uses both pools', {'begin': [[6, 2]], 'purchases': [[3, 5]], 'ending_qty': 8}, {'layers': [[6, 12], [2, 10]], 'ending_value': 22, 'cogs': 5}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}], ['uses both pools', {'begin': [[6, 2]], 'purchases': [[3, 5]], 'ending_qty': 7}, {'layers': [[6, 12], [1, 5]], 'ending_value': 17, 'cogs': 10}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}], ['uses both pools', {'begin': [[6, 2]], 'purchases': [[3, 5]], 'ending_qty': 8}, {'layers': [[6, 12], [2, 10]], 'ending_value': 22, 'cogs': 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):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q
    if end_q < 0 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in purchases:
            if inc == 0:
                break
            take = min(inc, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin:
            take = min(rem, bq)
            if take > 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}], ['uses both pools', {'begin': [[6, 2]], 'purchases': [[3, 5]], 'ending_qty': 8}, {'layers': [[6, 12], [2, 10]], 'ending_value': 22, 'cogs': 5}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}], ['uses both pools', {'begin': [[6, 2]], 'purchases': [[3, 5]], 'ending_qty': 7}, {'layers': [[6, 12], [1, 5]], 'ending_value': 17, 'cogs': 10}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}], ['uses both pools', {'begin': [[6, 2]], 'purchases': [[3, 5]], 'ending_qty': 8}, {'layers': [[6, 12], [2, 10]], 'ending_value': 22, 'cogs': 5}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}], ['uses both pools', {'begin': [[6, 2]], 'purchases': [[3, 5]], 'ending_qty': 7}, {'layers': [[6, 12], [1, 5]], 'ending_value': 17, 'cogs': 10}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}], ['uses both pools', {'begin': [[6, 2]], 'purchases': [[3, 5]], 'ending_qty': 8}, {'layers': [[6, 12], [2, 10]], 'ending_value': 22, 'cogs': 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:
An impossible ending count larger than purchases alone is rejected, even though beginning units make it valid.

Input {begin:[[qty,unit]] oldest first, purchases:[[qty,unit]] chronological, ending_qty}. Ending inventory keeps the oldest units: if ending_qty covers the base, all base layers stay and one increment layer is priced at the EARLIEST purchases of the year; otherwise the newest base layers are liquidated. ending_qty outside [0, available] returns {"error"}. Return {layers:[[qty,value]], ending_value, cogs}.

split: open-access


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

N = 1
observations = []
def solve(x):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q + buy_q
    if end_q < 0 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in purchases:
            if inc == 0:
                break
            take = min(inc, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin:
            take = min(rem, bq)
            if True:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}]]]
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):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q + buy_q
    if end_q < 0 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in purchases:
            if inc == 0:
                break
            take = min(inc, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin:
            take = min(rem, bq)
            if take >= 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}]]]
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:
Liquidation output contains zero-quantity layers for fully liquidated base years.

Input {begin:[[qty,unit]] oldest first, purchases:[[qty,unit]] chronological, ending_qty}. Ending inventory keeps the oldest units: if ending_qty covers the base, all base layers stay and one increment layer is priced at the EARLIEST purchases of the year; otherwise the newest base layers are liquidated. ending_qty outside [0, available] returns {"error"}. Return {layers:[[qty,value]], ending_value, cogs}.

split: open-access


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

N = 1
observations = []
def solve(x):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q + buy_q
    if end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in purchases:
            if inc == 0:
                break
            take = min(inc, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin:
            take = min(rem, bq)
            if take > 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}]]]
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):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q + buy_q
    if end_q < -1 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in purchases:
            if inc == 0:
                break
            take = min(inc, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin:
            take = min(rem, bq)
            if take > 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}]]]
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 negative physical count is accepted and produces negative layers instead of an error.

Input {begin:[[qty,unit]] oldest first, purchases:[[qty,unit]] chronological, ending_qty}. Ending inventory keeps the oldest units: if ending_qty covers the base, all base layers stay and one increment layer is priced at the EARLIEST purchases of the year; otherwise the newest base layers are liquidated. ending_qty outside [0, available] returns {"error"}. Return {layers:[[qty,value]], ending_value, cogs}.

split: open-access


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

N = 1
observations = []
def solve(x):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q + buy_q
    if end_q < 0 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in purchases:
            if inc == 0:
                break
            take = q
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin:
            take = min(rem, bq)
            if take > 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}], ['spans purchases', {'begin': [[2, 1]], 'purchases': [[2, 3], [5, 5]], 'ending_qty': 7}, {'layers': [[2, 2], [5, 21]], 'ending_value': 23, 'cogs': 10}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}], ['spans purchases', {'begin': [[2, 1]], 'purchases': [[2, 3], [5, 6]], 'ending_qty': 7}, {'layers': [[2, 2], [5, 24]], 'ending_value': 26, 'cogs': 12}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}], ['spans purchases', {'begin': [[2, 1]], 'purchases': [[2, 3], [5, 7]], 'ending_qty': 7}, {'layers': [[2, 2], [5, 27]], 'ending_value': 29, 'cogs': 14}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}], ['spans purchases', {'begin': [[2, 1]], 'purchases': [[2, 3], [5, 8]], 'ending_qty': 7}, {'layers': [[2, 2], [5, 30]], 'ending_value': 32, 'cogs': 16}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}], ['spans purchases', {'begin': [[2, 1]], 'purchases': [[2, 3], [5, 9]], 'ending_qty': 7}, {'layers': [[2, 2], [5, 33]], 'ending_value': 35, 'cogs': 18}]]]
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):
    begin = [list(l) for l in x['begin']]
    purchases = x['purchases']
    end_q = x['ending_qty']
    base_q = sum(l[0] for l in begin)
    buy_q = sum(p[0] for p in purchases)
    avail = base_q + buy_q
    if end_q < 0 or end_q > avail:
        return {'error': 'invalid ending quantity'}
    layers = []
    if end_q >= base_q:
        layers = [[q, q * c] for q, c in begin]
        inc = end_q - base_q
        new_q = 0
        new_v = 0
        for q, c in purchases:
            if inc == 0:
                break
            take = min(end_q - base_q, q)
            new_q += take
            new_v += take * c
            inc -= take
        if new_q > 0:
            layers.append([new_q, new_v])
    else:
        rem = end_q
        for bq, bc in begin:
            take = min(rem, bq)
            if take > 0:
                layers.append([take, take * bc])
            rem -= take
    begin_value = sum(q * c for q, c in begin)
    buy_value = sum(q * c for q, c in purchases)
    ending_value = sum(v for q, v in layers)
    return {'layers': layers, 'ending_value': ending_value, 'cogs': begin_value + buy_value - ending_value}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 10]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 94}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 1}, {'layers': [[1, 2]], 'ending_value': 2, 'cogs': 50}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 6}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[3, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[1, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 35}], ['spans purchases', {'begin': [[2, 1]], 'purchases': [[2, 3], [5, 5]], 'ending_qty': 7}, {'layers': [[2, 2], [5, 21]], 'ending_value': 23, 'cogs': 10}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 11]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 95}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 2}, {'layers': [[2, 4]], 'ending_value': 4, 'cogs': 48}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 7}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -2}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[4, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[2, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 46}], ['spans purchases', {'begin': [[2, 1]], 'purchases': [[2, 3], [5, 6]], 'ending_qty': 7}, {'layers': [[2, 2], [5, 24]], 'ending_value': 26, 'cogs': 12}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 12]], 'ending_qty': 17}, {'layers': [[10, 50], [4, 24], [3, 21]], 'ending_value': 95, 'cogs': 117}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 3}, {'layers': [[3, 6]], 'ending_value': 6, 'cogs': 46}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 8}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -3}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[5, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[3, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 57}], ['spans purchases', {'begin': [[2, 1]], 'purchases': [[2, 3], [5, 7]], 'ending_qty': 7}, {'layers': [[2, 2], [5, 27]], 'ending_value': 29, 'cogs': 14}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 13]], 'ending_qty': 18}, {'layers': [[10, 50], [4, 24], [4, 28]], 'ending_value': 102, 'cogs': 118}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 11}, {'layers': [[10, 50], [1, 6]], 'ending_value': 56, 'cogs': 60}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 4}, {'layers': [[3, 6], [1, 4]], 'ending_value': 10, 'cogs': 42}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 9}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -4}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[6, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[4, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 68}], ['spans purchases', {'begin': [[2, 1]], 'purchases': [[2, 3], [5, 8]], 'ending_qty': 7}, {'layers': [[2, 2], [5, 30]], 'ending_value': 32, 'cogs': 16}]], [['increment layer', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7], [8, 14]], 'ending_qty': 19}, {'layers': [[10, 50], [4, 24], [5, 35]], 'ending_value': 109, 'cogs': 119}], ['liquidation', {'begin': [[10, 5], [4, 6]], 'purchases': [[6, 7]], 'ending_qty': 10}, {'layers': [[10, 50]], 'ending_value': 50, 'cogs': 66}], ['deep liquidation', {'begin': [[3, 2], [3, 4], [3, 6]], 'purchases': [[2, 8]], 'ending_qty': 5}, {'layers': [[3, 6], [2, 8]], 'ending_value': 14, 'cogs': 38}], ['over available', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': 10}, {'error': 'invalid ending quantity'}], ['negative ending', {'begin': [[2, 3]], 'purchases': [[3, 4]], 'ending_qty': -5}, {'error': 'invalid ending quantity'}], ['count of minus one', {'begin': [[7, 3]], 'purchases': [[3, 4]], 'ending_qty': -1}, {'error': 'invalid ending quantity'}], ['all sold', {'begin': [[5, 3]], 'purchases': [[5, 4]], 'ending_qty': 0}, {'layers': [], 'ending_value': 0, 'cogs': 35}], ['exact base', {'begin': [[4, 10]], 'purchases': [[5, 11], [2, 12]], 'ending_qty': 4}, {'layers': [[4, 40]], 'ending_value': 40, 'cogs': 79}], ['spans purchases', {'begin': [[2, 1]], 'purchases': [[2, 3], [5, 9]], 'ending_qty': 7}, {'layers': [[2, 2], [5, 33]], 'ending_value': 35, 'cogs': 18}]]]
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 increment layer absorbs units from every purchase even after the increment is satisfied.

Input {begin:[[qty,unit]] oldest first, purchases:[[qty,unit]] chronological, ending_qty}. Ending inventory keeps the oldest units: if ending_qty covers the base, all base layers stay and one increment layer is priced at the EARLIEST purchases of the year; otherwise the newest base layers are liquidated. ending_qty outside [0, available] returns {"error"}. Return {layers:[[qty,value]], ending_value, cogs}.

split: open-access