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

N = 1
observations = []
def solve(x):
    elig = sum(l['amount'] for l in x['lines'] if l['cat'] != 'giftcard')
    if elig < 500:
        return {'base': 0, 'tier': 0, 'bonus': 0, 'total': 0}
    base = elig // 100
    tier = base * {'base': 100, 'silver': 125, 'gold': 150}[x['tier']] // 100
    bonus = 0
    for cat, mult in sorted(x['promo'].items()):
        if cat == 'giftcard':
            continue
        amt = sum(l['amount'] for l in x['lines'] if l['cat'] == cat)
        bonus += (amt // 100) * mult
    bonus = min(bonus, 1000)
    return {'base': base, 'tier': tier, 'bonus': bonus, 'total': tier + bonus}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['plain order', {'lines': [{'cat': 'grocery', 'amount': 2350}, {'cat': 'home', 'amount': 1575}], 'tax': 300, 'shipping': 499, 'tier': 'base', 'promo': {}}, {'base': 39, 'tier': 39, 'bonus': 0, 'total': 39}], ['gold with promo', {'lines': [{'cat': 'electronics', 'amount': 15099}, {'cat': 'grocery', 'amount': 851}], 'tax': 1200, 'shipping': 0, 'tier': 'gold', 'promo': {'electronics': 3}}, {'base': 159, 'tier': 238, 'bonus': 300, 'total': 538}], ['gift card excluded', {'lines': [{'cat': 'giftcard', 'amount': 5000}, {'cat': 'toys', 'amount': 1399}], 'tax': 100, 'shipping': 0, 'tier': 'silver', 'promo': {'giftcard': 5}}, {'base': 13, 'tier': 16, 'bonus': 0, 'total': 16}], ['below minimum', {'lines': [{'cat': 'toys', 'amount': 499}], 'tax': 41, 'shipping': 800, 'tier': 'gold', 'promo': {}}, {'base': 0, 'tier': 0, 'bonus': 0, 'total': 0}], ['at minimum', {'lines': [{'cat': 'toys', 'amount': 500}, {'cat': 'giftcard', 'amount': 2500}], 'tax': 40, 'shipping': 0, 'tier': 'silver', 'promo': {}}, {'base': 5, 'tier': 6, 'bonus': 0, 'total': 6}], ['bonus cap', {'lines': [{'cat': 'travel', 'amount': 61000}, {'cat': 'travel', 'amount': 1050}], 'tax': 0, 'shipping': 0, 'tier': 'base', 'promo': {'travel': 3, 'food': 2}}, {'base': 620, 'tier': 620, 'bonus': 1000, 'total': 1620}], ['cents across lines', {'lines': [{'cat': 'a', 'amount': 199}, {'cat': 'b', 'amount': 199}, {'cat': 'c', 'amount': 199}, {'cat': 'd', 'amount': 200}], 'tax': 0, 'shipping': 0, 'tier': 'silver', 'promo': {'b': 2}}, {'base': 7, 'tier': 8, 'bonus': 1, 'total': 9}], ['two promos', {'lines': [{'cat': 'shoes', 'amount': 4550}, {'cat': 'bags', 'amount': 3326}], 'tax': 500, 'shipping': 700, 'tier': 'gold', 'promo': {'shoes': 2, 'bags': 4}}, {'base': 78, 'tier': 117, 'bonus': 144, 'total': 261}]], [['plain order', {'lines': [{'cat': 'grocery', 'amount': 2351}, {'cat': 'home', 'amount': 1575}], 'tax': 300, 'shipping': 499, 'tier': 'base', 'promo': {}}, {'base': 39, 'tier': 39, 'bonus': 0, 'total': 39}], ['gold with promo', {'lines': [{'cat': 'electronics', 'amount': 15099}, {'cat': 'grocery', 'amount': 852}], 'tax': 1200, 'shipping': 0, 'tier': 'gold', 'promo': {'electronics': 3}}, {'base': 159, 'tier': 238, 'bonus': 300, 'total': 538}], ['gift card excluded', {'lines': [{'cat': 'giftcard', 'amount': 5000}, {'cat': 'toys', 'amount': 1499}], 'tax': 100, 'shipping': 0, 'tier': 'silver', 'promo': {'giftcard': 5}}, {'base': 14, 'tier': 17, 'bonus': 0, 'total': 17}], ['below minimum', {'lines': [{'cat': 'toys', 'amount': 499}], 'tax': 42, 'shipping': 800, 'tier': 'gold', 'promo': {}}, {'base': 0, 'tier': 0, 'bonus': 0, 'total': 0}], ['at minimum', {'lines': [{'cat': 'toys', 'amount': 500}, {'cat': 'giftcard', 'amount': 5000}], 'tax': 40, 'shipping': 0, 'tier': 'silver', 'promo': {}}, {'base': 5, 'tier': 6, 'bonus': 0, 'total': 6}], ['bonus cap', {'lines': [{'cat': 'travel', 'amount': 62000}, {'cat': 'travel', 'amount': 1050}], 'tax': 0, 'shipping': 0, 'tier': 'base', 'promo': {'travel': 3, 'food': 2}}, {'base': 630, 'tier': 630, 'bonus': 1000, 'total': 1630}], ['cents across lines', {'lines': [{'cat': 'a', 'amount': 199}, {'cat': 'b', 'amount': 199}, {'cat': 'c', 'amount': 199}, {'cat': 'd', 'amount': 201}], 'tax': 0, 'shipping': 0, 'tier': 'silver', 'promo': {'b': 2}}, {'base': 7, 'tier': 8, 'bonus': 1, 'total': 9}], ['two promos', {'lines': [{'cat': 'shoes', 'amount': 4550}, {'cat': 'bags', 'amount': 3327}], 'tax': 500, 'shipping': 700, 'tier': 'gold', 'promo': {'shoes': 2, 'bags': 4}}, {'base': 78, 'tier': 117, 'bonus': 144, 'total': 261}]], [['plain order', {'lines': [{'cat': 'grocery', 'amount': 2352}, {'cat': 'home', 'amount': 1575}], 'tax': 300, 'shipping': 499, 'tier': 'base', 'promo': {}}, {'base': 39, 'tier': 39, 'bonus': 0, 'total': 39}], ['gold with promo', {'lines': [{'cat': 'electronics', 'amount': 15099}, {'cat': 'grocery', 'amount': 853}], 'tax': 1200, 'shipping': 0, 'tier': 'gold', 'promo': {'electronics': 3}}, {'base': 159, 'tier': 238, 'bonus': 300, 'total': 538}], ['gift card excluded', {'lines': [{'cat': 'giftcard', 'amount': 5000}, {'cat': 'toys', 'amount': 1599}], 'tax': 100, 'shipping': 0, 'tier': 'silver', 'promo': {'giftcard': 5}}, {'base': 15, 'tier': 18, 'bonus': 0, 'total': 18}], ['below minimum', {'lines': [{'cat': 'toys', 'amount': 499}], 'tax': 43, 'shipping': 800, 'tier': 'gold', 'promo': {}}, {'base': 0, 'tier': 0, 'bonus': 0, 'total': 0}], ['at minimum', {'lines': [{'cat': 'toys', 'amount': 500}, {'cat': 'giftcard', 'amount': 7500}], 'tax': 40, 'shipping': 0, 'tier': 'silver', 'promo': {}}, {'base': 5, 'tier': 6, 'bonus': 0, 'total': 6}], ['bonus cap', {'lines': [{'cat': 'travel', 'amount': 63000}, {'cat': 'travel', 'amount': 1050}], 'tax': 0, 'shipping': 0, 'tier': 'base', 'promo': {'travel': 3, 'food': 2}}, {'base': 640, 'tier': 640, 'bonus': 1000, 'total': 1640}], ['cents across lines', {'lines': [{'cat': 'a', 'amount': 199}, {'cat': 'b', 'amount': 199}, {'cat': 'c', 'amount': 199}, {'cat': 'd', 'amount': 202}], 'tax': 0, 'shipping': 0, 'tier': 'silver', 'promo': {'b': 2}}, {'base': 7, 'tier': 8, 'bonus': 1, 'total': 9}], ['two promos', {'lines': [{'cat': 'shoes', 'amount': 4550}, {'cat': 'bags', 'amount': 3328}], 'tax': 500, 'shipping': 700, 'tier': 'gold', 'promo': {'shoes': 2, 'bags': 4}}, {'base': 78, 'tier': 117, 'bonus': 144, 'total': 261}]], [['plain order', {'lines': [{'cat': 'grocery', 'amount': 2353}, {'cat': 'home', 'amount': 1575}], 'tax': 300, 'shipping': 499, 'tier': 'base', 'promo': {}}, {'base': 39, 'tier': 39, 'bonus': 0, 'total': 39}], ['gold with promo', {'lines': [{'cat': 'electronics', 'amount': 15099}, {'cat': 'grocery', 'amount': 854}], 'tax': 1200, 'shipping': 0, 'tier': 'gold', 'promo': {'electronics': 3}}, {'base': 159, 'tier': 238, 'bonus': 300, 'total': 538}], ['gift card excluded', {'lines': [{'cat': 'giftcard', 'amount': 5000}, {'cat': 'toys', 'amount': 1699}], 'tax': 100, 'shipping': 0, 'tier': 'silver', 'promo': {'giftcard': 5}}, {'base': 16, 'tier': 20, 'bonus': 0, 'total': 20}], ['below minimum', {'lines': [{'cat': 'toys', 'amount': 499}], 'tax': 44, 'shipping': 800, 'tier': 'gold', 'promo': {}}, {'base': 0, 'tier': 0, 'bonus': 0, 'total': 0}], ['at minimum', {'lines': [{'cat': 'toys', 'amount': 500}, {'cat': 'giftcard', 'amount': 10000}], 'tax': 40, 'shipping': 0, 'tier': 'silver', 'promo': {}}, {'base': 5, 'tier': 6, 'bonus': 0, 'total': 6}], ['bonus cap', {'lines': [{'cat': 'travel', 'amount': 64000}, {'cat': 'travel', 'amount': 1050}], 'tax': 0, 'shipping': 0, 'tier': 'base', 'promo': {'travel': 3, 'food': 2}}, {'base': 650, 'tier': 650, 'bonus': 1000, 'total': 1650}], ['cents across lines', {'lines': [{'cat': 'a', 'amount': 199}, {'cat': 'b', 'amount': 199}, {'cat': 'c', 'amount': 199}, {'cat': 'd', 'amount': 203}], 'tax': 0, 'shipping': 0, 'tier': 'silver', 'promo': {'b': 2}}, {'base': 8, 'tier': 10, 'bonus': 1, 'total': 11}], ['two promos', {'lines': [{'cat': 'shoes', 'amount': 4550}, {'cat': 'bags', 'amount': 3329}], 'tax': 500, 'shipping': 700, 'tier': 'gold', 'promo': {'shoes': 2, 'bags': 4}}, {'base': 78, 'tier': 117, 'bonus': 144, 'total': 261}]], [['plain order', {'lines': [{'cat': 'grocery', 'amount': 2354}, {'cat': 'home', 'amount': 1575}], 'tax': 300, 'shipping': 499, 'tier': 'base', 'promo': {}}, {'base': 39, 'tier': 39, 'bonus': 0, 'total': 39}], ['gold with promo', {'lines': [{'cat': 'electronics', 'amount': 15099}, {'cat': 'grocery', 'amount': 855}], 'tax': 1200, 'shipping': 0, 'tier': 'gold', 'promo': {'electronics': 3}}, {'base': 159, 'tier': 238, 'bonus': 300, 'total': 538}], ['gift card excluded', {'lines': [{'cat': 'giftcard', 'amount': 5000}, {'cat': 'toys', 'amount': 1799}], 'tax': 100, 'shipping': 0, 'tier': 'silver', 'promo': {'giftcard': 5}}, {'base': 17, 'tier': 21, 'bonus': 0, 'total': 21}], ['below minimum', {'lines': [{'cat': 'toys', 'amount': 499}], 'tax': 45, 'shipping': 800, 'tier': 'gold', 'promo': {}}, {'base': 0, 'tier': 0, 'bonus': 0, 'total': 0}], ['at minimum', {'lines': [{'cat': 'toys', 'amount': 500}, {'cat': 'giftcard', 'amount': 12500}], 'tax': 40, 'shipping': 0, 'tier': 'silver', 'promo': {}}, {'base': 5, 'tier': 6, 'bonus': 0, 'total': 6}], ['bonus cap', {'lines': [{'cat': 'travel', 'amount': 65000}, {'cat': 'travel', 'amount': 1050}], 'tax': 0, 'shipping': 0, 'tier': 'base', 'promo': {'travel': 3, 'food': 2}}, {'base': 660, 'tier': 660, 'bonus': 1000, 'total': 1660}], ['cents across lines', {'lines': [{'cat': 'a', 'amount': 199}, {'cat': 'b', 'amount': 199}, {'cat': 'c', 'amount': 199}, {'cat': 'd', 'amount': 204}], 'tax': 0, 'shipping': 0, 'tier': 'silver', 'promo': {'b': 2}}, {'base': 8, 'tier': 10, 'bonus': 1, 'total': 11}], ['two promos', {'lines': [{'cat': 'shoes', 'amount': 4550}, {'cat': 'bags', 'amount': 3330}], 'tax': 500, 'shipping': 700, 'tier': 'gold', 'promo': {'shoes': 2, 'bags': 4}}, {'base': 78, 'tier': 117, 'bonus': 144, 'total': 261}]]]
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):
    elig = sum(l['amount'] for l in x['lines'] if l['cat'] != 'giftcard')
    if elig < 500:
        return {'base': 0, 'tier': 0, 'bonus': 0, 'total': 0}
    base = elig // 100
    tier = base * {'base': 100, 'silver': 125, 'gold': 150}[x['tier']] // 100
    bonus = 0
    for cat, mult in sorted(x['promo'].items()):
        if cat == 'giftcard':
            continue
        amt = sum(l['amount'] for l in x['lines'] if l['cat'] == cat)
        bonus += (amt // 100) * mult - 1
    bonus = min(bonus, 1000)
    return {'base': base, 'tier': tier, 'bonus': bonus, 'total': tier + bonus}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['plain order', {'lines': [{'cat': 'grocery', 'amount': 2350}, {'cat': 'home', 'amount': 1575}], 'tax': 300, 'shipping': 499, 'tier': 'base', 'promo': {}}, {'base': 39, 'tier': 39, 'bonus': 0, 'total': 39}], ['gold with promo', {'lines': [{'cat': 'electronics', 'amount': 15099}, {'cat': 'grocery', 'amount': 851}], 'tax': 1200, 'shipping': 0, 'tier': 'gold', 'promo': {'electronics': 3}}, {'base': 159, 'tier': 238, 'bonus': 300, 'total': 538}], ['gift card excluded', {'lines': [{'cat': 'giftcard', 'amount': 5000}, {'cat': 'toys', 'amount': 1399}], 'tax': 100, 'shipping': 0, 'tier': 'silver', 'promo': {'giftcard': 5}}, {'base': 13, 'tier': 16, 'bonus': 0, 'total': 16}], ['below minimum', {'lines': [{'cat': 'toys', 'amount': 499}], 'tax': 41, 'shipping': 800, 'tier': 'gold', 'promo': {}}, {'base': 0, 'tier': 0, 'bonus': 0, 'total': 0}], ['at minimum', {'lines': [{'cat': 'toys', 'amount': 500}, {'cat': 'giftcard', 'amount': 2500}], 'tax': 40, 'shipping': 0, 'tier': 'silver', 'promo': {}}, {'base': 5, 'tier': 6, 'bonus': 0, 'total': 6}], ['bonus cap', {'lines': [{'cat': 'travel', 'amount': 61000}, {'cat': 'travel', 'amount': 1050}], 'tax': 0, 'shipping': 0, 'tier': 'base', 'promo': {'travel': 3, 'food': 2}}, {'base': 620, 'tier': 620, 'bonus': 1000, 'total': 1620}], ['cents across lines', {'lines': [{'cat': 'a', 'amount': 199}, {'cat': 'b', 'amount': 199}, {'cat': 'c', 'amount': 199}, {'cat': 'd', 'amount': 200}], 'tax': 0, 'shipping': 0, 'tier': 'silver', 'promo': {'b': 2}}, {'base': 7, 'tier': 8, 'bonus': 1, 'total': 9}], ['two promos', {'lines': [{'cat': 'shoes', 'amount': 4550}, {'cat': 'bags', 'amount': 3326}], 'tax': 500, 'shipping': 700, 'tier': 'gold', 'promo': {'shoes': 2, 'bags': 4}}, {'base': 78, 'tier': 117, 'bonus': 144, 'total': 261}]], [['plain order', {'lines': [{'cat': 'grocery', 'amount': 2351}, {'cat': 'home', 'amount': 1575}], 'tax': 300, 'shipping': 499, 'tier': 'base', 'promo': {}}, {'base': 39, 'tier': 39, 'bonus': 0, 'total': 39}], ['gold with promo', {'lines': [{'cat': 'electronics', 'amount': 15099}, {'cat': 'grocery', 'amount': 852}], 'tax': 1200, 'shipping': 0, 'tier': 'gold', 'promo': {'electronics': 3}}, {'base': 159, 'tier': 238, 'bonus': 300, 'total': 538}], ['gift card excluded', {'lines': [{'cat': 'giftcard', 'amount': 5000}, {'cat': 'toys', 'amount': 1499}], 'tax': 100, 'shipping': 0, 'tier': 'silver', 'promo': {'giftcard': 5}}, {'base': 14, 'tier': 17, 'bonus': 0, 'total': 17}], ['below minimum', {'lines': [{'cat': 'toys', 'amount': 499}], 'tax': 42, 'shipping': 800, 'tier': 'gold', 'promo': {}}, {'base': 0, 'tier': 0, 'bonus': 0, 'total': 0}], ['at minimum', {'lines': [{'cat': 'toys', 'amount': 500}, {'cat': 'giftcard', 'amount': 5000}], 'tax': 40, 'shipping': 0, 'tier': 'silver', 'promo': {}}, {'base': 5, 'tier': 6, 'bonus': 0, 'total': 6}], ['bonus cap', {'lines': [{'cat': 'travel', 'amount': 62000}, {'cat': 'travel', 'amount': 1050}], 'tax': 0, 'shipping': 0, 'tier': 'base', 'promo': {'travel': 3, 'food': 2}}, {'base': 630, 'tier': 630, 'bonus': 1000, 'total': 1630}], ['cents across lines', {'lines': [{'cat': 'a', 'amount': 199}, {'cat': 'b', 'amount': 199}, {'cat': 'c', 'amount': 199}, {'cat': 'd', 'amount': 201}], 'tax': 0, 'shipping': 0, 'tier': 'silver', 'promo': {'b': 2}}, {'base': 7, 'tier': 8, 'bonus': 1, 'total': 9}], ['two promos', {'lines': [{'cat': 'shoes', 'amount': 4550}, {'cat': 'bags', 'amount': 3327}], 'tax': 500, 'shipping': 700, 'tier': 'gold', 'promo': {'shoes': 2, 'bags': 4}}, {'base': 78, 'tier': 117, 'bonus': 144, 'total': 261}]], [['plain order', {'lines': [{'cat': 'grocery', 'amount': 2352}, {'cat': 'home', 'amount': 1575}], 'tax': 300, 'shipping': 499, 'tier': 'base', 'promo': {}}, {'base': 39, 'tier': 39, 'bonus': 0, 'total': 39}], ['gold with promo', {'lines': [{'cat': 'electronics', 'amount': 15099}, {'cat': 'grocery', 'amount': 853}], 'tax': 1200, 'shipping': 0, 'tier': 'gold', 'promo': {'electronics': 3}}, {'base': 159, 'tier': 238, 'bonus': 300, 'total': 538}], ['gift card excluded', {'lines': [{'cat': 'giftcard', 'amount': 5000}, {'cat': 'toys', 'amount': 1599}], 'tax': 100, 'shipping': 0, 'tier': 'silver', 'promo': {'giftcard': 5}}, {'base': 15, 'tier': 18, 'bonus': 0, 'total': 18}], ['below minimum', {'lines': [{'cat': 'toys', 'amount': 499}], 'tax': 43, 'shipping': 800, 'tier': 'gold', 'promo': {}}, {'base': 0, 'tier': 0, 'bonus': 0, 'total': 0}], ['at minimum', {'lines': [{'cat': 'toys', 'amount': 500}, {'cat': 'giftcard', 'amount': 7500}], 'tax': 40, 'shipping': 0, 'tier': 'silver', 'promo': {}}, {'base': 5, 'tier': 6, 'bonus': 0, 'total': 6}], ['bonus cap', {'lines': [{'cat': 'travel', 'amount': 63000}, {'cat': 'travel', 'amount': 1050}], 'tax': 0, 'shipping': 0, 'tier': 'base', 'promo': {'travel': 3, 'food': 2}}, {'base': 640, 'tier': 640, 'bonus': 1000, 'total': 1640}], ['cents across lines', {'lines': [{'cat': 'a', 'amount': 199}, {'cat': 'b', 'amount': 199}, {'cat': 'c', 'amount': 199}, {'cat': 'd', 'amount': 202}], 'tax': 0, 'shipping': 0, 'tier': 'silver', 'promo': {'b': 2}}, {'base': 7, 'tier': 8, 'bonus': 1, 'total': 9}], ['two promos', {'lines': [{'cat': 'shoes', 'amount': 4550}, {'cat': 'bags', 'amount': 3328}], 'tax': 500, 'shipping': 700, 'tier': 'gold', 'promo': {'shoes': 2, 'bags': 4}}, {'base': 78, 'tier': 117, 'bonus': 144, 'total': 261}]], [['plain order', {'lines': [{'cat': 'grocery', 'amount': 2353}, {'cat': 'home', 'amount': 1575}], 'tax': 300, 'shipping': 499, 'tier': 'base', 'promo': {}}, {'base': 39, 'tier': 39, 'bonus': 0, 'total': 39}], ['gold with promo', {'lines': [{'cat': 'electronics', 'amount': 15099}, {'cat': 'grocery', 'amount': 854}], 'tax': 1200, 'shipping': 0, 'tier': 'gold', 'promo': {'electronics': 3}}, {'base': 159, 'tier': 238, 'bonus': 300, 'total': 538}], ['gift card excluded', {'lines': [{'cat': 'giftcard', 'amount': 5000}, {'cat': 'toys', 'amount': 1699}], 'tax': 100, 'shipping': 0, 'tier': 'silver', 'promo': {'giftcard': 5}}, {'base': 16, 'tier': 20, 'bonus': 0, 'total': 20}], ['below minimum', {'lines': [{'cat': 'toys', 'amount': 499}], 'tax': 44, 'shipping': 800, 'tier': 'gold', 'promo': {}}, {'base': 0, 'tier': 0, 'bonus': 0, 'total': 0}], ['at minimum', {'lines': [{'cat': 'toys', 'amount': 500}, {'cat': 'giftcard', 'amount': 10000}], 'tax': 40, 'shipping': 0, 'tier': 'silver', 'promo': {}}, {'base': 5, 'tier': 6, 'bonus': 0, 'total': 6}], ['bonus cap', {'lines': [{'cat': 'travel', 'amount': 64000}, {'cat': 'travel', 'amount': 1050}], 'tax': 0, 'shipping': 0, 'tier': 'base', 'promo': {'travel': 3, 'food': 2}}, {'base': 650, 'tier': 650, 'bonus': 1000, 'total': 1650}], ['cents across lines', {'lines': [{'cat': 'a', 'amount': 199}, {'cat': 'b', 'amount': 199}, {'cat': 'c', 'amount': 199}, {'cat': 'd', 'amount': 203}], 'tax': 0, 'shipping': 0, 'tier': 'silver', 'promo': {'b': 2}}, {'base': 8, 'tier': 10, 'bonus': 1, 'total': 11}], ['two promos', {'lines': [{'cat': 'shoes', 'amount': 4550}, {'cat': 'bags', 'amount': 3329}], 'tax': 500, 'shipping': 700, 'tier': 'gold', 'promo': {'shoes': 2, 'bags': 4}}, {'base': 78, 'tier': 117, 'bonus': 144, 'total': 261}]], [['plain order', {'lines': [{'cat': 'grocery', 'amount': 2354}, {'cat': 'home', 'amount': 1575}], 'tax': 300, 'shipping': 499, 'tier': 'base', 'promo': {}}, {'base': 39, 'tier': 39, 'bonus': 0, 'total': 39}], ['gold with promo', {'lines': [{'cat': 'electronics', 'amount': 15099}, {'cat': 'grocery', 'amount': 855}], 'tax': 1200, 'shipping': 0, 'tier': 'gold', 'promo': {'electronics': 3}}, {'base': 159, 'tier': 238, 'bonus': 300, 'total': 538}], ['gift card excluded', {'lines': [{'cat': 'giftcard', 'amount': 5000}, {'cat': 'toys', 'amount': 1799}], 'tax': 100, 'shipping': 0, 'tier': 'silver', 'promo': {'giftcard': 5}}, {'base': 17, 'tier': 21, 'bonus': 0, 'total': 21}], ['below minimum', {'lines': [{'cat': 'toys', 'amount': 499}], 'tax': 45, 'shipping': 800, 'tier': 'gold', 'promo': {}}, {'base': 0, 'tier': 0, 'bonus': 0, 'total': 0}], ['at minimum', {'lines': [{'cat': 'toys', 'amount': 500}, {'cat': 'giftcard', 'amount': 12500}], 'tax': 40, 'shipping': 0, 'tier': 'silver', 'promo': {}}, {'base': 5, 'tier': 6, 'bonus': 0, 'total': 6}], ['bonus cap', {'lines': [{'cat': 'travel', 'amount': 65000}, {'cat': 'travel', 'amount': 1050}], 'tax': 0, 'shipping': 0, 'tier': 'base', 'promo': {'travel': 3, 'food': 2}}, {'base': 660, 'tier': 660, 'bonus': 1000, 'total': 1660}], ['cents across lines', {'lines': [{'cat': 'a', 'amount': 199}, {'cat': 'b', 'amount': 199}, {'cat': 'c', 'amount': 199}, {'cat': 'd', 'amount': 204}], 'tax': 0, 'shipping': 0, 'tier': 'silver', 'promo': {'b': 2}}, {'base': 8, 'tier': 10, 'bonus': 1, 'total': 11}], ['two promos', {'lines': [{'cat': 'shoes', 'amount': 4550}, {'cat': 'bags', 'amount': 3330}], 'tax': 500, 'shipping': 700, 'tier': 'gold', 'promo': {'shoes': 2, 'bags': 4}}, {'base': 78, 'tier': 117, 'bonus': 144, 'total': 261}]]]
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:
Promotional categories earn their base points twice.

Input {lines:[{cat,amount cents}], tax, shipping, tier, promo:{cat:multiplier}}. Eligible spend = non-giftcard line amounts (tax and shipping never earn). Eligible spend below 500 cents earns nothing. Base = floor(eligible/100) computed once per order. Tier points = floor(base*pct/100) with pct base 100, silver 125, gold 150. Promo bonus per non-giftcard category = floor(category spend/100)*(multiplier-1); the order bonus is capped at 1000. Return {base, tier, bonus, total}.

split: open-access


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

N = 1
observations = []
def solve(x):
    lots = []
    out = []
    for ev in x['events']:
        kind, day = ev[0], ev[1]
        if kind == 'earn':
            exp = day + ev[3]
            lots.append([exp, day, ev[2]])
            out.append(exp)
        elif kind == 'sweep':
            gone = sum(l[2] for l in lots if l[0] < day)
            lots = [l for l in lots if l[0] >= day]
            out.append(gone)
        else:
            need = ev[2]
            live = sorted([l for l in lots if l[0] > day], key=lambda l: (l[0], l[1]))
            if need > sum(l[2] for l in live):
                out.append('insufficient')
                continue
            used = []
            for l in live:
                t = min(need, l[2])
                if t > 0:
                    l[2] -= t
                    need -= t
                    used.append([l[0], t])
            lots = [l for l in lots if l[2] > 0]
            out.append(used)
    return {'results': out, 'lots': lots}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 31], ['redeem', 20, 400]]}, {'results': [366, 41, [[41, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 151]]}, {'results': [30, [[30, 151]]], 'lots': [[30, 0, 49]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 101]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 81, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 201], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 11], ['sweep', 11], ['sweep', 12]]}, {'results': [11, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 31]]}, {'results': [50, 61, [[50, 100]], [[61, 31]]], 'lots': [[61, 1, 69]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 151], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 51]]}, {'results': [25, 25, [[25, 40], [25, 11]]], 'lots': [[25, 5, 29]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 32], ['redeem', 20, 400]]}, {'results': [366, 42, [[42, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 152]]}, {'results': [30, [[30, 152]]], 'lots': [[30, 0, 48]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 102]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 82, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 202], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 12], ['sweep', 12], ['sweep', 13]]}, {'results': [12, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 32]]}, {'results': [50, 61, [[50, 100]], [[61, 32]]], 'lots': [[61, 1, 68]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 152], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 52]]}, {'results': [25, 25, [[25, 40], [25, 12]]], 'lots': [[25, 5, 28]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 33], ['redeem', 20, 400]]}, {'results': [366, 43, [[43, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 153]]}, {'results': [30, [[30, 153]]], 'lots': [[30, 0, 47]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 103]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 83, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 203], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 13], ['sweep', 13], ['sweep', 14]]}, {'results': [13, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 33]]}, {'results': [50, 61, [[50, 100]], [[61, 33]]], 'lots': [[61, 1, 67]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 153], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 53]]}, {'results': [25, 25, [[25, 40], [25, 13]]], 'lots': [[25, 5, 27]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 34], ['redeem', 20, 400]]}, {'results': [366, 44, [[44, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 154]]}, {'results': [30, [[30, 154]]], 'lots': [[30, 0, 46]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 104]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 84, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 204], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 14], ['sweep', 14], ['sweep', 15]]}, {'results': [14, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 34]]}, {'results': [50, 61, [[50, 100]], [[61, 34]]], 'lots': [[61, 1, 66]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 154], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 54]]}, {'results': [25, 25, [[25, 40], [25, 14]]], 'lots': [[25, 5, 26]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 35], ['redeem', 20, 400]]}, {'results': [366, 45, [[45, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 155]]}, {'results': [30, [[30, 155]]], 'lots': [[30, 0, 45]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 105]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 85, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 205], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 15], ['sweep', 15], ['sweep', 16]]}, {'results': [15, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 35]]}, {'results': [50, 61, [[50, 100]], [[61, 35]]], 'lots': [[61, 1, 65]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 155], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 55]]}, {'results': [25, 25, [[25, 40], [25, 15]]], 'lots': [[25, 5, 25]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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):
    lots = []
    out = []
    for ev in x['events']:
        kind, day = ev[0], ev[1]
        if kind == 'earn':
            exp = day + ev[3]
            lots.append([exp, day, ev[2]])
            out.append(exp)
        elif kind == 'sweep':
            gone = sum(l[2] for l in lots if l[0] < day)
            lots = [l for l in lots if l[0] >= day]
            out.append(gone)
        else:
            need = ev[2]
            live = sorted([l for l in lots if l[0] >= day - 1], key=lambda l: (l[0], l[1]))
            if need > sum(l[2] for l in live):
                out.append('insufficient')
                continue
            used = []
            for l in live:
                t = min(need, l[2])
                if t > 0:
                    l[2] -= t
                    need -= t
                    used.append([l[0], t])
            lots = [l for l in lots if l[2] > 0]
            out.append(used)
    return {'results': out, 'lots': lots}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 31], ['redeem', 20, 400]]}, {'results': [366, 41, [[41, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 151]]}, {'results': [30, [[30, 151]]], 'lots': [[30, 0, 49]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 101]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 81, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 201], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 11], ['sweep', 11], ['sweep', 12]]}, {'results': [11, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 31]]}, {'results': [50, 61, [[50, 100]], [[61, 31]]], 'lots': [[61, 1, 69]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 151], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 51]]}, {'results': [25, 25, [[25, 40], [25, 11]]], 'lots': [[25, 5, 29]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 32], ['redeem', 20, 400]]}, {'results': [366, 42, [[42, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 152]]}, {'results': [30, [[30, 152]]], 'lots': [[30, 0, 48]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 102]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 82, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 202], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 12], ['sweep', 12], ['sweep', 13]]}, {'results': [12, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 32]]}, {'results': [50, 61, [[50, 100]], [[61, 32]]], 'lots': [[61, 1, 68]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 152], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 52]]}, {'results': [25, 25, [[25, 40], [25, 12]]], 'lots': [[25, 5, 28]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 33], ['redeem', 20, 400]]}, {'results': [366, 43, [[43, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 153]]}, {'results': [30, [[30, 153]]], 'lots': [[30, 0, 47]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 103]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 83, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 203], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 13], ['sweep', 13], ['sweep', 14]]}, {'results': [13, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 33]]}, {'results': [50, 61, [[50, 100]], [[61, 33]]], 'lots': [[61, 1, 67]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 153], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 53]]}, {'results': [25, 25, [[25, 40], [25, 13]]], 'lots': [[25, 5, 27]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 34], ['redeem', 20, 400]]}, {'results': [366, 44, [[44, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 154]]}, {'results': [30, [[30, 154]]], 'lots': [[30, 0, 46]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 104]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 84, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 204], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 14], ['sweep', 14], ['sweep', 15]]}, {'results': [14, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 34]]}, {'results': [50, 61, [[50, 100]], [[61, 34]]], 'lots': [[61, 1, 66]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 154], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 54]]}, {'results': [25, 25, [[25, 40], [25, 14]]], 'lots': [[25, 5, 26]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 35], ['redeem', 20, 400]]}, {'results': [366, 45, [[45, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 155]]}, {'results': [30, [[30, 155]]], 'lots': [[30, 0, 45]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 105]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 85, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 205], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 15], ['sweep', 15], ['sweep', 16]]}, {'results': [15, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 35]]}, {'results': [50, 61, [[50, 100]], [[61, 35]]], 'lots': [[61, 1, 65]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 155], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 55]]}, {'results': [25, 25, [[25, 40], [25, 15]]], 'lots': [[25, 5, 25]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Points cannot be redeemed on the day printed as their expiry date.

Events ["earn",day,pts,ttl] create a lot expiring at day+ttl (usable through that day); ["redeem",day,pts] draws only unexpired lots, soonest expiry first (ties by earn day), rejecting with "insufficient" when unexpired points are short; ["sweep",day] removes lots whose expiry is before day and reports the points removed. Emptied lots are dropped. Return {results, lots:[[expiry, earn_day, pts]]}.

split: open-access


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

N = 1
observations = []
def solve(x):
    lots = []
    out = []
    for ev in x['events']:
        kind, day = ev[0], ev[1]
        if kind == 'earn':
            exp = day + ev[3]
            lots.append([exp, day, ev[2]])
            out.append(exp)
        elif kind == 'sweep':
            gone = sum(l[2] for l in lots if l[0] <= day)
            lots = [l for l in lots if l[0] > day]
            out.append(gone)
        else:
            need = ev[2]
            live = sorted([l for l in lots if l[0] >= day], key=lambda l: (l[0], l[1]))
            if need > sum(l[2] for l in live):
                out.append('insufficient')
                continue
            used = []
            for l in live:
                t = min(need, l[2])
                if t > 0:
                    l[2] -= t
                    need -= t
                    used.append([l[0], t])
            lots = [l for l in lots if l[2] > 0]
            out.append(used)
    return {'results': out, 'lots': lots}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 31], ['redeem', 20, 400]]}, {'results': [366, 41, [[41, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 151]]}, {'results': [30, [[30, 151]]], 'lots': [[30, 0, 49]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 101]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 81, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 201], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 11], ['sweep', 11], ['sweep', 12]]}, {'results': [11, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 31]]}, {'results': [50, 61, [[50, 100]], [[61, 31]]], 'lots': [[61, 1, 69]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 151], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 51]]}, {'results': [25, 25, [[25, 40], [25, 11]]], 'lots': [[25, 5, 29]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 32], ['redeem', 20, 400]]}, {'results': [366, 42, [[42, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 152]]}, {'results': [30, [[30, 152]]], 'lots': [[30, 0, 48]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 102]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 82, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 202], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 12], ['sweep', 12], ['sweep', 13]]}, {'results': [12, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 32]]}, {'results': [50, 61, [[50, 100]], [[61, 32]]], 'lots': [[61, 1, 68]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 152], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 52]]}, {'results': [25, 25, [[25, 40], [25, 12]]], 'lots': [[25, 5, 28]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 33], ['redeem', 20, 400]]}, {'results': [366, 43, [[43, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 153]]}, {'results': [30, [[30, 153]]], 'lots': [[30, 0, 47]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 103]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 83, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 203], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 13], ['sweep', 13], ['sweep', 14]]}, {'results': [13, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 33]]}, {'results': [50, 61, [[50, 100]], [[61, 33]]], 'lots': [[61, 1, 67]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 153], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 53]]}, {'results': [25, 25, [[25, 40], [25, 13]]], 'lots': [[25, 5, 27]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 34], ['redeem', 20, 400]]}, {'results': [366, 44, [[44, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 154]]}, {'results': [30, [[30, 154]]], 'lots': [[30, 0, 46]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 104]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 84, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 204], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 14], ['sweep', 14], ['sweep', 15]]}, {'results': [14, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 34]]}, {'results': [50, 61, [[50, 100]], [[61, 34]]], 'lots': [[61, 1, 66]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 154], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 54]]}, {'results': [25, 25, [[25, 40], [25, 14]]], 'lots': [[25, 5, 26]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 35], ['redeem', 20, 400]]}, {'results': [366, 45, [[45, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 155]]}, {'results': [30, [[30, 155]]], 'lots': [[30, 0, 45]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 105]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 85, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 205], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 15], ['sweep', 15], ['sweep', 16]]}, {'results': [15, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 35]]}, {'results': [50, 61, [[50, 100]], [[61, 35]]], 'lots': [[61, 1, 65]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 155], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 55]]}, {'results': [25, 25, [[25, 40], [25, 15]]], 'lots': [[25, 5, 25]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    lots = []
    out = []
    for ev in x['events']:
        kind, day = ev[0], ev[1]
        if kind == 'earn':
            exp = day + ev[3]
            lots.append([exp, day, ev[2]])
            out.append(exp)
        elif kind == 'sweep':
            gone = sum(l[2] for l in lots if l[0] + 1 < day)
            lots = [l for l in lots if l[0] + 1 >= day]
            out.append(gone)
        else:
            need = ev[2]
            live = sorted([l for l in lots if l[0] >= day], key=lambda l: (l[0], l[1]))
            if need > sum(l[2] for l in live):
                out.append('insufficient')
                continue
            used = []
            for l in live:
                t = min(need, l[2])
                if t > 0:
                    l[2] -= t
                    need -= t
                    used.append([l[0], t])
            lots = [l for l in lots if l[2] > 0]
            out.append(used)
    return {'results': out, 'lots': lots}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 31], ['redeem', 20, 400]]}, {'results': [366, 41, [[41, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 151]]}, {'results': [30, [[30, 151]]], 'lots': [[30, 0, 49]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 101]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 81, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 201], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 11], ['sweep', 11], ['sweep', 12]]}, {'results': [11, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 31]]}, {'results': [50, 61, [[50, 100]], [[61, 31]]], 'lots': [[61, 1, 69]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 151], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 51]]}, {'results': [25, 25, [[25, 40], [25, 11]]], 'lots': [[25, 5, 29]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 32], ['redeem', 20, 400]]}, {'results': [366, 42, [[42, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 152]]}, {'results': [30, [[30, 152]]], 'lots': [[30, 0, 48]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 102]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 82, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 202], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 12], ['sweep', 12], ['sweep', 13]]}, {'results': [12, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 32]]}, {'results': [50, 61, [[50, 100]], [[61, 32]]], 'lots': [[61, 1, 68]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 152], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 52]]}, {'results': [25, 25, [[25, 40], [25, 12]]], 'lots': [[25, 5, 28]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 33], ['redeem', 20, 400]]}, {'results': [366, 43, [[43, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 153]]}, {'results': [30, [[30, 153]]], 'lots': [[30, 0, 47]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 103]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 83, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 203], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 13], ['sweep', 13], ['sweep', 14]]}, {'results': [13, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 33]]}, {'results': [50, 61, [[50, 100]], [[61, 33]]], 'lots': [[61, 1, 67]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 153], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 53]]}, {'results': [25, 25, [[25, 40], [25, 13]]], 'lots': [[25, 5, 27]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 34], ['redeem', 20, 400]]}, {'results': [366, 44, [[44, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 154]]}, {'results': [30, [[30, 154]]], 'lots': [[30, 0, 46]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 104]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 84, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 204], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 14], ['sweep', 14], ['sweep', 15]]}, {'results': [14, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 34]]}, {'results': [50, 61, [[50, 100]], [[61, 34]]], 'lots': [[61, 1, 66]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 154], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 54]]}, {'results': [25, 25, [[25, 40], [25, 14]]], 'lots': [[25, 5, 26]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 35], ['redeem', 20, 400]]}, {'results': [366, 45, [[45, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 155]]}, {'results': [30, [[30, 155]]], 'lots': [[30, 0, 45]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 105]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 85, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 205], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 15], ['sweep', 15], ['sweep', 16]]}, {'results': [15, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 35]]}, {'results': [50, 61, [[50, 100]], [[61, 35]]], 'lots': [[61, 1, 65]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 155], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 55]]}, {'results': [25, 25, [[25, 40], [25, 15]]], 'lots': [[25, 5, 25]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The nightly sweep removes points on their last valid day.

Events ["earn",day,pts,ttl] create a lot expiring at day+ttl (usable through that day); ["redeem",day,pts] draws only unexpired lots, soonest expiry first (ties by earn day), rejecting with "insufficient" when unexpired points are short; ["sweep",day] removes lots whose expiry is before day and reports the points removed. Emptied lots are dropped. Return {results, lots:[[expiry, earn_day, pts]]}.

split: open-access


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

N = 1
observations = []
def solve(x):
    lots = []
    out = []
    for ev in x['events']:
        kind, day = ev[0], ev[1]
        if kind == 'earn':
            exp = day + ev[3]
            lots.append([exp, day, ev[2]])
            out.append(exp)
        elif kind == 'sweep':
            gone = sum(l[2] for l in lots if l[0] < day)
            lots = [l for l in lots if l[0] >= day]
            out.append(gone)
        else:
            need = ev[2]
            live = sorted([l for l in lots if l[0] >= day], key=lambda l: (l[1], l[0]))
            if need > sum(l[2] for l in live):
                out.append('insufficient')
                continue
            used = []
            for l in live:
                t = min(need, l[2])
                if t > 0:
                    l[2] -= t
                    need -= t
                    used.append([l[0], t])
            lots = [l for l in lots if l[2] > 0]
            out.append(used)
    return {'results': out, 'lots': lots}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 31], ['redeem', 20, 400]]}, {'results': [366, 41, [[41, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 151]]}, {'results': [30, [[30, 151]]], 'lots': [[30, 0, 49]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 101]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 81, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 201], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 11], ['sweep', 11], ['sweep', 12]]}, {'results': [11, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 31]]}, {'results': [50, 61, [[50, 100]], [[61, 31]]], 'lots': [[61, 1, 69]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 151], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 51]]}, {'results': [25, 25, [[25, 40], [25, 11]]], 'lots': [[25, 5, 29]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 32], ['redeem', 20, 400]]}, {'results': [366, 42, [[42, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 152]]}, {'results': [30, [[30, 152]]], 'lots': [[30, 0, 48]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 102]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 82, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 202], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 12], ['sweep', 12], ['sweep', 13]]}, {'results': [12, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 32]]}, {'results': [50, 61, [[50, 100]], [[61, 32]]], 'lots': [[61, 1, 68]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 152], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 52]]}, {'results': [25, 25, [[25, 40], [25, 12]]], 'lots': [[25, 5, 28]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 33], ['redeem', 20, 400]]}, {'results': [366, 43, [[43, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 153]]}, {'results': [30, [[30, 153]]], 'lots': [[30, 0, 47]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 103]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 83, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 203], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 13], ['sweep', 13], ['sweep', 14]]}, {'results': [13, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 33]]}, {'results': [50, 61, [[50, 100]], [[61, 33]]], 'lots': [[61, 1, 67]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 153], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 53]]}, {'results': [25, 25, [[25, 40], [25, 13]]], 'lots': [[25, 5, 27]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 34], ['redeem', 20, 400]]}, {'results': [366, 44, [[44, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 154]]}, {'results': [30, [[30, 154]]], 'lots': [[30, 0, 46]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 104]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 84, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 204], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 14], ['sweep', 14], ['sweep', 15]]}, {'results': [14, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 34]]}, {'results': [50, 61, [[50, 100]], [[61, 34]]], 'lots': [[61, 1, 66]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 154], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 54]]}, {'results': [25, 25, [[25, 40], [25, 14]]], 'lots': [[25, 5, 26]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 35], ['redeem', 20, 400]]}, {'results': [366, 45, [[45, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 155]]}, {'results': [30, [[30, 155]]], 'lots': [[30, 0, 45]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 105]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 85, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 205], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 15], ['sweep', 15], ['sweep', 16]]}, {'results': [15, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 35]]}, {'results': [50, 61, [[50, 100]], [[61, 35]]], 'lots': [[61, 1, 65]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 155], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 55]]}, {'results': [25, 25, [[25, 40], [25, 15]]], 'lots': [[25, 5, 25]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    lots = []
    out = []
    for ev in x['events']:
        kind, day = ev[0], ev[1]
        if kind == 'earn':
            exp = day + ev[3]
            lots.append([exp, day, ev[2]])
            out.append(exp)
        elif kind == 'sweep':
            gone = sum(l[2] for l in lots if l[0] < day)
            lots = [l for l in lots if l[0] >= day]
            out.append(gone)
        else:
            need = ev[2]
            live = sorted([l for l in lots if l[0] >= day], key=lambda l: (l[2], l[0]))
            if need > sum(l[2] for l in live):
                out.append('insufficient')
                continue
            used = []
            for l in live:
                t = min(need, l[2])
                if t > 0:
                    l[2] -= t
                    need -= t
                    used.append([l[0], t])
            lots = [l for l in lots if l[2] > 0]
            out.append(used)
    return {'results': out, 'lots': lots}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 31], ['redeem', 20, 400]]}, {'results': [366, 41, [[41, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 151]]}, {'results': [30, [[30, 151]]], 'lots': [[30, 0, 49]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 101]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 81, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 201], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 11], ['sweep', 11], ['sweep', 12]]}, {'results': [11, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 31]]}, {'results': [50, 61, [[50, 100]], [[61, 31]]], 'lots': [[61, 1, 69]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 151], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 51]]}, {'results': [25, 25, [[25, 40], [25, 11]]], 'lots': [[25, 5, 29]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 32], ['redeem', 20, 400]]}, {'results': [366, 42, [[42, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 152]]}, {'results': [30, [[30, 152]]], 'lots': [[30, 0, 48]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 102]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 82, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 202], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 12], ['sweep', 12], ['sweep', 13]]}, {'results': [12, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 32]]}, {'results': [50, 61, [[50, 100]], [[61, 32]]], 'lots': [[61, 1, 68]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 152], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 52]]}, {'results': [25, 25, [[25, 40], [25, 12]]], 'lots': [[25, 5, 28]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 33], ['redeem', 20, 400]]}, {'results': [366, 43, [[43, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 153]]}, {'results': [30, [[30, 153]]], 'lots': [[30, 0, 47]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 103]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 83, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 203], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 13], ['sweep', 13], ['sweep', 14]]}, {'results': [13, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 33]]}, {'results': [50, 61, [[50, 100]], [[61, 33]]], 'lots': [[61, 1, 67]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 153], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 53]]}, {'results': [25, 25, [[25, 40], [25, 13]]], 'lots': [[25, 5, 27]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 34], ['redeem', 20, 400]]}, {'results': [366, 44, [[44, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 154]]}, {'results': [30, [[30, 154]]], 'lots': [[30, 0, 46]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 104]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 84, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 204], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 14], ['sweep', 14], ['sweep', 15]]}, {'results': [14, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 34]]}, {'results': [50, 61, [[50, 100]], [[61, 34]]], 'lots': [[61, 1, 66]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 154], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 54]]}, {'results': [25, 25, [[25, 40], [25, 14]]], 'lots': [[25, 5, 26]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 35], ['redeem', 20, 400]]}, {'results': [366, 45, [[45, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 155]]}, {'results': [30, [[30, 155]]], 'lots': [[30, 0, 45]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 105]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 85, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 205], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 15], ['sweep', 15], ['sweep', 16]]}, {'results': [15, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 35]]}, {'results': [50, 61, [[50, 100]], [[61, 35]]], 'lots': [[61, 1, 65]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 155], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 55]]}, {'results': [25, 25, [[25, 40], [25, 15]]], 'lots': [[25, 5, 25]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Short-lived promotional points expire unused while long-lived points are spent first.

Events ["earn",day,pts,ttl] create a lot expiring at day+ttl (usable through that day); ["redeem",day,pts] draws only unexpired lots, soonest expiry first (ties by earn day), rejecting with "insufficient" when unexpired points are short; ["sweep",day] removes lots whose expiry is before day and reports the points removed. Emptied lots are dropped. Return {results, lots:[[expiry, earn_day, pts]]}.

split: open-access


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

N = 1
observations = []
def solve(x):
    lots = []
    out = []
    for ev in x['events']:
        kind, day = ev[0], ev[1]
        if kind == 'earn':
            exp = ev[3]
            lots.append([exp, day, ev[2]])
            out.append(exp)
        elif kind == 'sweep':
            gone = sum(l[2] for l in lots if l[0] < day)
            lots = [l for l in lots if l[0] >= day]
            out.append(gone)
        else:
            need = ev[2]
            live = sorted([l for l in lots if l[0] >= day], key=lambda l: (l[0], l[1]))
            if need > sum(l[2] for l in live):
                out.append('insufficient')
                continue
            used = []
            for l in live:
                t = min(need, l[2])
                if t > 0:
                    l[2] -= t
                    need -= t
                    used.append([l[0], t])
            lots = [l for l in lots if l[2] > 0]
            out.append(used)
    return {'results': out, 'lots': lots}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 31], ['redeem', 20, 400]]}, {'results': [366, 41, [[41, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 151]]}, {'results': [30, [[30, 151]]], 'lots': [[30, 0, 49]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 101]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 81, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 201], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 11], ['sweep', 11], ['sweep', 12]]}, {'results': [11, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 31]]}, {'results': [50, 61, [[50, 100]], [[61, 31]]], 'lots': [[61, 1, 69]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 151], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 51]]}, {'results': [25, 25, [[25, 40], [25, 11]]], 'lots': [[25, 5, 29]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 32], ['redeem', 20, 400]]}, {'results': [366, 42, [[42, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 152]]}, {'results': [30, [[30, 152]]], 'lots': [[30, 0, 48]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 102]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 82, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 202], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 12], ['sweep', 12], ['sweep', 13]]}, {'results': [12, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 32]]}, {'results': [50, 61, [[50, 100]], [[61, 32]]], 'lots': [[61, 1, 68]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 152], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 52]]}, {'results': [25, 25, [[25, 40], [25, 12]]], 'lots': [[25, 5, 28]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 33], ['redeem', 20, 400]]}, {'results': [366, 43, [[43, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 153]]}, {'results': [30, [[30, 153]]], 'lots': [[30, 0, 47]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 103]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 83, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 203], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 13], ['sweep', 13], ['sweep', 14]]}, {'results': [13, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 33]]}, {'results': [50, 61, [[50, 100]], [[61, 33]]], 'lots': [[61, 1, 67]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 153], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 53]]}, {'results': [25, 25, [[25, 40], [25, 13]]], 'lots': [[25, 5, 27]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 34], ['redeem', 20, 400]]}, {'results': [366, 44, [[44, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 154]]}, {'results': [30, [[30, 154]]], 'lots': [[30, 0, 46]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 104]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 84, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 204], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 14], ['sweep', 14], ['sweep', 15]]}, {'results': [14, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 34]]}, {'results': [50, 61, [[50, 100]], [[61, 34]]], 'lots': [[61, 1, 66]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 154], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 54]]}, {'results': [25, 25, [[25, 40], [25, 14]]], 'lots': [[25, 5, 26]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 35], ['redeem', 20, 400]]}, {'results': [366, 45, [[45, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 155]]}, {'results': [30, [[30, 155]]], 'lots': [[30, 0, 45]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 105]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 85, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 205], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 15], ['sweep', 15], ['sweep', 16]]}, {'results': [15, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 35]]}, {'results': [50, 61, [[50, 100]], [[61, 35]]], 'lots': [[61, 1, 65]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 155], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 55]]}, {'results': [25, 25, [[25, 40], [25, 15]]], 'lots': [[25, 5, 25]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    lots = []
    out = []
    for ev in x['events']:
        kind, day = ev[0], ev[1]
        if kind == 'earn':
            exp = day + ev[3] - 1
            lots.append([exp, day, ev[2]])
            out.append(exp)
        elif kind == 'sweep':
            gone = sum(l[2] for l in lots if l[0] < day)
            lots = [l for l in lots if l[0] >= day]
            out.append(gone)
        else:
            need = ev[2]
            live = sorted([l for l in lots if l[0] >= day], key=lambda l: (l[0], l[1]))
            if need > sum(l[2] for l in live):
                out.append('insufficient')
                continue
            used = []
            for l in live:
                t = min(need, l[2])
                if t > 0:
                    l[2] -= t
                    need -= t
                    used.append([l[0], t])
            lots = [l for l in lots if l[2] > 0]
            out.append(used)
    return {'results': out, 'lots': lots}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 31], ['redeem', 20, 400]]}, {'results': [366, 41, [[41, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 151]]}, {'results': [30, [[30, 151]]], 'lots': [[30, 0, 49]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 101]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 81, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 201], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 11], ['sweep', 11], ['sweep', 12]]}, {'results': [11, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 31]]}, {'results': [50, 61, [[50, 100]], [[61, 31]]], 'lots': [[61, 1, 69]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 151], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 51]]}, {'results': [25, 25, [[25, 40], [25, 11]]], 'lots': [[25, 5, 29]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 32], ['redeem', 20, 400]]}, {'results': [366, 42, [[42, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 152]]}, {'results': [30, [[30, 152]]], 'lots': [[30, 0, 48]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 102]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 82, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 202], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 12], ['sweep', 12], ['sweep', 13]]}, {'results': [12, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 32]]}, {'results': [50, 61, [[50, 100]], [[61, 32]]], 'lots': [[61, 1, 68]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 152], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 52]]}, {'results': [25, 25, [[25, 40], [25, 12]]], 'lots': [[25, 5, 28]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 33], ['redeem', 20, 400]]}, {'results': [366, 43, [[43, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 153]]}, {'results': [30, [[30, 153]]], 'lots': [[30, 0, 47]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 103]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 83, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 203], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 13], ['sweep', 13], ['sweep', 14]]}, {'results': [13, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 33]]}, {'results': [50, 61, [[50, 100]], [[61, 33]]], 'lots': [[61, 1, 67]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 153], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 53]]}, {'results': [25, 25, [[25, 40], [25, 13]]], 'lots': [[25, 5, 27]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 34], ['redeem', 20, 400]]}, {'results': [366, 44, [[44, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 154]]}, {'results': [30, [[30, 154]]], 'lots': [[30, 0, 46]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 104]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 84, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 204], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 14], ['sweep', 14], ['sweep', 15]]}, {'results': [14, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 34]]}, {'results': [50, 61, [[50, 100]], [[61, 34]]], 'lots': [[61, 1, 66]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 154], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 54]]}, {'results': [25, 25, [[25, 40], [25, 14]]], 'lots': [[25, 5, 26]]}]], [['soonest expiry first', {'events': [['earn', 1, 500, 365], ['earn', 10, 300, 35], ['redeem', 20, 400]]}, {'results': [366, 45, [[45, 300], [366, 100]]], 'lots': [[366, 1, 400]]}], ['use on expiry day', {'events': [['earn', 0, 200, 30], ['redeem', 30, 155]]}, {'results': [30, [[30, 155]]], 'lots': [[30, 0, 45]]}], ['expired unusable', {'events': [['earn', 0, 200, 30], ['earn', 5, 50, 90], ['redeem', 31, 105]]}, {'results': [30, 95, 'insufficient'], 'lots': [[30, 0, 200], [95, 5, 50]]}], ['sweep reports points', {'events': [['earn', 0, 120, 10], ['earn', 0, 85, 10], ['earn', 3, 60, 100], ['sweep', 11]]}, {'results': [10, 10, 103, 205], 'lots': [[103, 3, 60]]}], ['sweep boundary', {'events': [['earn', 0, 70, 15], ['sweep', 15], ['sweep', 16]]}, {'results': [15, 0, 70], 'lots': []}], ['partial lot cleanup', {'events': [['earn', 0, 100, 50], ['earn', 1, 100, 60], ['redeem', 2, 100], ['redeem', 3, 35]]}, {'results': [50, 61, [[50, 100]], [[61, 35]]], 'lots': [[61, 1, 65]]}], ['insufficient', {'events': [['earn', 0, 100, 50], ['redeem', 1, 155], ['redeem', 2, 100]]}, {'results': [50, 'insufficient', [[50, 100]]], 'lots': []}], ['ties by earn day', {'events': [['earn', 5, 40, 20], ['earn', 1, 40, 24], ['redeem', 6, 55]]}, {'results': [25, 25, [[25, 40], [25, 15]]], 'lots': [[25, 5, 25]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Lots expire on an absolute day equal to their time-to-live, long before or after the intended date.

Events ["earn",day,pts,ttl] create a lot expiring at day+ttl (usable through that day); ["redeem",day,pts] draws only unexpired lots, soonest expiry first (ties by earn day), rejecting with "insufficient" when unexpired points are short; ["sweep",day] removes lots whose expiry is before day and reports the points removed. Emptied lots are dropped. Return {results, lots:[[expiry, earn_day, pts]]}.

split: open-access