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

N = 1
observations = []
def solve(x):
    deferred = x['issued'] * x['value_mc'] // 1000
    expected = x['issued'] * (10000 - x['breakage_bp']) // 10000
    if expected <= 0:
        return {'error': 'no expected redemptions'}
    used = min(x['redeemed'], expected)
    recognized = deferred // expected * used
    return {'deferred': deferred, 'expected': expected, 'recognized': recognized, 'liability': deferred - recognized}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['mid program', {'issued': 100000, 'redeemed': 30001, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37501, 'liability': 62499}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1001}, {'deferred': 40000, 'expected': 44995, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1112, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1112, 'liability': 1387}], ['no breakage', {'issued': 700, 'redeemed': 351, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 351, 'liability': 349}], ['all breakage', {'issued': 700, 'redeemed': 1, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 4, 'breakage_bp': 3000}, {'deferred': 399, 'expected': 69999, 'recognized': 70, 'liability': 329}]], [['mid program', {'issued': 100000, 'redeemed': 30002, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37502, 'liability': 62498}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1002}, {'deferred': 40000, 'expected': 44990, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1113, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1113, 'liability': 1386}], ['no breakage', {'issued': 700, 'redeemed': 352, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 352, 'liability': 348}], ['all breakage', {'issued': 700, 'redeemed': 2, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 5, 'breakage_bp': 3000}, {'deferred': 499, 'expected': 69999, 'recognized': 88, 'liability': 411}]], [['mid program', {'issued': 100000, 'redeemed': 30003, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37503, 'liability': 62497}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1003}, {'deferred': 40000, 'expected': 44985, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1114, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1114, 'liability': 1385}], ['no breakage', {'issued': 700, 'redeemed': 353, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 353, 'liability': 347}], ['all breakage', {'issued': 700, 'redeemed': 3, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 6, 'breakage_bp': 3000}, {'deferred': 599, 'expected': 69999, 'recognized': 105, 'liability': 494}]], [['mid program', {'issued': 100000, 'redeemed': 30004, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37505, 'liability': 62495}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1004}, {'deferred': 40000, 'expected': 44980, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1115, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1115, 'liability': 1384}], ['no breakage', {'issued': 700, 'redeemed': 354, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 354, 'liability': 346}], ['all breakage', {'issued': 700, 'redeemed': 4, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 7, 'breakage_bp': 3000}, {'deferred': 699, 'expected': 69999, 'recognized': 123, 'liability': 576}]], [['mid program', {'issued': 100000, 'redeemed': 30005, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37506, 'liability': 62494}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1005}, {'deferred': 40000, 'expected': 44975, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1116, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1116, 'liability': 1383}], ['no breakage', {'issued': 700, 'redeemed': 355, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 355, 'liability': 345}], ['all breakage', {'issued': 700, 'redeemed': 5, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 8, 'breakage_bp': 3000}, {'deferred': 799, 'expected': 69999, 'recognized': 140, 'liability': 659}]]]
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):
    deferred = x['issued'] * x['value_mc'] // 1000
    expected = x['issued'] * (10000 - x['breakage_bp']) // 10000
    if expected <= 0:
        return {'error': 'no expected redemptions'}
    used = min(x['redeemed'], expected)
    recognized = (deferred * used + expected - 1) // expected
    return {'deferred': deferred, 'expected': expected, 'recognized': recognized, 'liability': deferred - recognized}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['mid program', {'issued': 100000, 'redeemed': 30001, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37501, 'liability': 62499}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1001}, {'deferred': 40000, 'expected': 44995, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1112, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1112, 'liability': 1387}], ['no breakage', {'issued': 700, 'redeemed': 351, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 351, 'liability': 349}], ['all breakage', {'issued': 700, 'redeemed': 1, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 4, 'breakage_bp': 3000}, {'deferred': 399, 'expected': 69999, 'recognized': 70, 'liability': 329}]], [['mid program', {'issued': 100000, 'redeemed': 30002, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37502, 'liability': 62498}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1002}, {'deferred': 40000, 'expected': 44990, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1113, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1113, 'liability': 1386}], ['no breakage', {'issued': 700, 'redeemed': 352, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 352, 'liability': 348}], ['all breakage', {'issued': 700, 'redeemed': 2, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 5, 'breakage_bp': 3000}, {'deferred': 499, 'expected': 69999, 'recognized': 88, 'liability': 411}]], [['mid program', {'issued': 100000, 'redeemed': 30003, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37503, 'liability': 62497}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1003}, {'deferred': 40000, 'expected': 44985, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1114, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1114, 'liability': 1385}], ['no breakage', {'issued': 700, 'redeemed': 353, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 353, 'liability': 347}], ['all breakage', {'issued': 700, 'redeemed': 3, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 6, 'breakage_bp': 3000}, {'deferred': 599, 'expected': 69999, 'recognized': 105, 'liability': 494}]], [['mid program', {'issued': 100000, 'redeemed': 30004, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37505, 'liability': 62495}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1004}, {'deferred': 40000, 'expected': 44980, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1115, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1115, 'liability': 1384}], ['no breakage', {'issued': 700, 'redeemed': 354, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 354, 'liability': 346}], ['all breakage', {'issued': 700, 'redeemed': 4, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 7, 'breakage_bp': 3000}, {'deferred': 699, 'expected': 69999, 'recognized': 123, 'liability': 576}]], [['mid program', {'issued': 100000, 'redeemed': 30005, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37506, 'liability': 62494}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1005}, {'deferred': 40000, 'expected': 44975, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1116, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1116, 'liability': 1383}], ['no breakage', {'issued': 700, 'redeemed': 355, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 355, 'liability': 345}], ['all breakage', {'issued': 700, 'redeemed': 5, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 8, 'breakage_bp': 3000}, {'deferred': 799, 'expected': 69999, 'recognized': 140, 'liability': 659}]]]
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:
Recognized revenue lags by many cents on large programs.

Input {issued, redeemed, value_mc (millicents per point), breakage_bp}. Deferred revenue = floor(issued*value_mc/1000) cents. Expected redemptions = floor(issued*(10000-breakage_bp)/10000); if zero return {"error"}. Recognized = floor(deferred*min(redeemed, expected)/expected); liability = deferred - recognized. Return {deferred, expected, recognized, liability}.

split: open-access


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

N = 1
observations = []
def solve(x):
    deferred = x['issued'] * x['value_mc'] // 100
    expected = x['issued'] * (10000 - x['breakage_bp']) // 10000
    if expected <= 0:
        return {'error': 'no expected redemptions'}
    used = min(x['redeemed'], expected)
    recognized = deferred * used // expected
    return {'deferred': deferred, 'expected': expected, 'recognized': recognized, 'liability': deferred - recognized}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['mid program', {'issued': 100000, 'redeemed': 30001, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37501, 'liability': 62499}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1001}, {'deferred': 40000, 'expected': 44995, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1112, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1112, 'liability': 1387}], ['no breakage', {'issued': 700, 'redeemed': 351, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 351, 'liability': 349}], ['all breakage', {'issued': 700, 'redeemed': 1, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 4, 'breakage_bp': 3000}, {'deferred': 399, 'expected': 69999, 'recognized': 70, 'liability': 329}]], [['mid program', {'issued': 100000, 'redeemed': 30002, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37502, 'liability': 62498}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1002}, {'deferred': 40000, 'expected': 44990, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1113, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1113, 'liability': 1386}], ['no breakage', {'issued': 700, 'redeemed': 352, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 352, 'liability': 348}], ['all breakage', {'issued': 700, 'redeemed': 2, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 5, 'breakage_bp': 3000}, {'deferred': 499, 'expected': 69999, 'recognized': 88, 'liability': 411}]], [['mid program', {'issued': 100000, 'redeemed': 30003, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37503, 'liability': 62497}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1003}, {'deferred': 40000, 'expected': 44985, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1114, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1114, 'liability': 1385}], ['no breakage', {'issued': 700, 'redeemed': 353, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 353, 'liability': 347}], ['all breakage', {'issued': 700, 'redeemed': 3, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 6, 'breakage_bp': 3000}, {'deferred': 599, 'expected': 69999, 'recognized': 105, 'liability': 494}]], [['mid program', {'issued': 100000, 'redeemed': 30004, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37505, 'liability': 62495}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1004}, {'deferred': 40000, 'expected': 44980, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1115, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1115, 'liability': 1384}], ['no breakage', {'issued': 700, 'redeemed': 354, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 354, 'liability': 346}], ['all breakage', {'issued': 700, 'redeemed': 4, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 7, 'breakage_bp': 3000}, {'deferred': 699, 'expected': 69999, 'recognized': 123, 'liability': 576}]], [['mid program', {'issued': 100000, 'redeemed': 30005, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37506, 'liability': 62494}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1005}, {'deferred': 40000, 'expected': 44975, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1116, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1116, 'liability': 1383}], ['no breakage', {'issued': 700, 'redeemed': 355, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 355, 'liability': 345}], ['all breakage', {'issued': 700, 'redeemed': 5, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 8, 'breakage_bp': 3000}, {'deferred': 799, 'expected': 69999, 'recognized': 140, 'liability': 659}]]]
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):
    deferred = x['issued'] * x['value_mc'] // 10000
    expected = x['issued'] * (10000 - x['breakage_bp']) // 10000
    if expected <= 0:
        return {'error': 'no expected redemptions'}
    used = min(x['redeemed'], expected)
    recognized = deferred * used // expected
    return {'deferred': deferred, 'expected': expected, 'recognized': recognized, 'liability': deferred - recognized}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['mid program', {'issued': 100000, 'redeemed': 30001, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37501, 'liability': 62499}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1001}, {'deferred': 40000, 'expected': 44995, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1112, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1112, 'liability': 1387}], ['no breakage', {'issued': 700, 'redeemed': 351, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 351, 'liability': 349}], ['all breakage', {'issued': 700, 'redeemed': 1, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 4, 'breakage_bp': 3000}, {'deferred': 399, 'expected': 69999, 'recognized': 70, 'liability': 329}]], [['mid program', {'issued': 100000, 'redeemed': 30002, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37502, 'liability': 62498}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1002}, {'deferred': 40000, 'expected': 44990, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1113, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1113, 'liability': 1386}], ['no breakage', {'issued': 700, 'redeemed': 352, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 352, 'liability': 348}], ['all breakage', {'issued': 700, 'redeemed': 2, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 5, 'breakage_bp': 3000}, {'deferred': 499, 'expected': 69999, 'recognized': 88, 'liability': 411}]], [['mid program', {'issued': 100000, 'redeemed': 30003, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37503, 'liability': 62497}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1003}, {'deferred': 40000, 'expected': 44985, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1114, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1114, 'liability': 1385}], ['no breakage', {'issued': 700, 'redeemed': 353, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 353, 'liability': 347}], ['all breakage', {'issued': 700, 'redeemed': 3, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 6, 'breakage_bp': 3000}, {'deferred': 599, 'expected': 69999, 'recognized': 105, 'liability': 494}]], [['mid program', {'issued': 100000, 'redeemed': 30004, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37505, 'liability': 62495}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1004}, {'deferred': 40000, 'expected': 44980, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1115, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1115, 'liability': 1384}], ['no breakage', {'issued': 700, 'redeemed': 354, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 354, 'liability': 346}], ['all breakage', {'issued': 700, 'redeemed': 4, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 7, 'breakage_bp': 3000}, {'deferred': 699, 'expected': 69999, 'recognized': 123, 'liability': 576}]], [['mid program', {'issued': 100000, 'redeemed': 30005, 'value_mc': 1000, 'breakage_bp': 2000}, {'deferred': 100000, 'expected': 80000, 'recognized': 37506, 'liability': 62494}], ['fully redeemed', {'issued': 50000, 'redeemed': 45000, 'value_mc': 800, 'breakage_bp': 1005}, {'deferred': 40000, 'expected': 44975, 'recognized': 40000, 'liability': 0}], ['over redeemed', {'issued': 1000, 'redeemed': 950, 'value_mc': 1200, 'breakage_bp': 1500}, {'deferred': 1200, 'expected': 850, 'recognized': 1200, 'liability': 0}], ['fractional value', {'issued': 3333, 'redeemed': 1116, 'value_mc': 750, 'breakage_bp': 2500}, {'deferred': 2499, 'expected': 2499, 'recognized': 1116, 'liability': 1383}], ['no breakage', {'issued': 700, 'redeemed': 355, 'value_mc': 1000, 'breakage_bp': 0}, {'deferred': 700, 'expected': 700, 'recognized': 355, 'liability': 345}], ['all breakage', {'issued': 700, 'redeemed': 5, 'value_mc': 1000, 'breakage_bp': 10000}, {'error': 'no expected redemptions'}], ['tiny value', {'issued': 99999, 'redeemed': 12345, 'value_mc': 8, 'breakage_bp': 3000}, {'deferred': 799, 'expected': 69999, 'recognized': 140, 'liability': 659}]]]
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 deferred balance is ten times too large.

Input {issued, redeemed, value_mc (millicents per point), breakage_bp}. Deferred revenue = floor(issued*value_mc/1000) cents. Expected redemptions = floor(issued*(10000-breakage_bp)/10000); if zero return {"error"}. Recognized = floor(deferred*min(redeemed, expected)/expected); liability = deferred - recognized. Return {deferred, expected, recognized, liability}.

split: open-access


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

N = 1
observations = []
def solve(x):
    seen = set()
    evs = []
    for e in sorted(x['events'], key=lambda e: (e['ts'], e['id'])):
        if e['id'] in seen:
            continue
        seen.add(e['id'])
        evs.append(e)
    evs.sort(key=lambda e: (e['ts'], e['id']))
    bal = 0
    applied = {}
    undone = set()
    rej = []
    order = []
    for e in evs:
        t = e['type']
        if t == 'earn':
            bal += e['pts']
            applied[e['id']] = e['pts']
        elif t == 'redeem':
            if e['pts'] > bal:
                rej.append(e['id'])
                continue
            bal -= e['pts']
            applied[e['id']] = -e['pts']
        else:
            r = e['ref']
            if r not in applied or r in undone:
                rej.append(e['id'])
                continue
            bal -= applied[r]
            undone.add(r)
        order.append(e['id'])
    return {'balance': bal, 'rejected': rej, 'applied': order}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 101, 'ref': None}]}, {'balance': 51, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 151, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 81, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 61, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 51, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 31, 'ref': None}]}, {'balance': 1, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 5, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 5, 'rejected': ['x'], 'applied': ['e1']}], ['redelivered earlier ts', {'events': [{'id': 'e2', 'ts': 3, 'type': 'redeem', 'pts': 40, 'ref': None}, {'id': 'e1', 'ts': 5, 'type': 'earn', 'pts': 51, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 51, 'ref': None}]}, {'balance': 51, 'rejected': ['e2'], 'applied': ['e1']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 102, 'ref': None}]}, {'balance': 52, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 152, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 82, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 62, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 52, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 32, 'ref': None}]}, {'balance': 2, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 10, 'rejected': ['x'], 'applied': ['e1']}], ['redelivered earlier ts', {'events': [{'id': 'e2', 'ts': 3, 'type': 'redeem', 'pts': 40, 'ref': None}, {'id': 'e1', 'ts': 5, 'type': 'earn', 'pts': 52, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 52, 'ref': None}]}, {'balance': 52, 'rejected': ['e2'], 'applied': ['e1']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 103, 'ref': None}]}, {'balance': 53, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 153, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 83, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 63, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 53, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 33, 'ref': None}]}, {'balance': 3, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 15, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 15, 'rejected': ['x'], 'applied': ['e1']}], ['redelivered earlier ts', {'events': [{'id': 'e2', 'ts': 3, 'type': 'redeem', 'pts': 40, 'ref': None}, {'id': 'e1', 'ts': 5, 'type': 'earn', 'pts': 53, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 53, 'ref': None}]}, {'balance': 53, 'rejected': ['e2'], 'applied': ['e1']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 104, 'ref': None}]}, {'balance': 54, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 154, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 84, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 64, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 54, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 34, 'ref': None}]}, {'balance': 4, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 20, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 20, 'rejected': ['x'], 'applied': ['e1']}], ['redelivered earlier ts', {'events': [{'id': 'e2', 'ts': 3, 'type': 'redeem', 'pts': 40, 'ref': None}, {'id': 'e1', 'ts': 5, 'type': 'earn', 'pts': 54, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 54, 'ref': None}]}, {'balance': 54, 'rejected': ['e2'], 'applied': ['e1']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 105, 'ref': None}]}, {'balance': 55, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 155, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 85, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 65, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 55, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 35, 'ref': None}]}, {'balance': 5, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 25, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 25, 'rejected': ['x'], 'applied': ['e1']}], ['redelivered earlier ts', {'events': [{'id': 'e2', 'ts': 3, 'type': 'redeem', 'pts': 40, 'ref': None}, {'id': 'e1', 'ts': 5, 'type': 'earn', 'pts': 55, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 55, 'ref': None}]}, {'balance': 55, 'rejected': ['e2'], 'applied': ['e1']}]]]
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):
    seen = set()
    evs = []
    for e in x['events'][::-1]:
        if e['id'] in seen:
            continue
        seen.add(e['id'])
        evs.append(e)
    evs.sort(key=lambda e: (e['ts'], e['id']))
    bal = 0
    applied = {}
    undone = set()
    rej = []
    order = []
    for e in evs:
        t = e['type']
        if t == 'earn':
            bal += e['pts']
            applied[e['id']] = e['pts']
        elif t == 'redeem':
            if e['pts'] > bal:
                rej.append(e['id'])
                continue
            bal -= e['pts']
            applied[e['id']] = -e['pts']
        else:
            r = e['ref']
            if r not in applied or r in undone:
                rej.append(e['id'])
                continue
            bal -= applied[r]
            undone.add(r)
        order.append(e['id'])
    return {'balance': bal, 'rejected': rej, 'applied': order}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 101, 'ref': None}]}, {'balance': 51, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 151, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 81, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 61, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 51, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 31, 'ref': None}]}, {'balance': 1, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 5, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 5, 'rejected': ['x'], 'applied': ['e1']}], ['redelivered earlier ts', {'events': [{'id': 'e2', 'ts': 3, 'type': 'redeem', 'pts': 40, 'ref': None}, {'id': 'e1', 'ts': 5, 'type': 'earn', 'pts': 51, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 51, 'ref': None}]}, {'balance': 51, 'rejected': ['e2'], 'applied': ['e1']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 102, 'ref': None}]}, {'balance': 52, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 152, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 82, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 62, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 52, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 32, 'ref': None}]}, {'balance': 2, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 10, 'rejected': ['x'], 'applied': ['e1']}], ['redelivered earlier ts', {'events': [{'id': 'e2', 'ts': 3, 'type': 'redeem', 'pts': 40, 'ref': None}, {'id': 'e1', 'ts': 5, 'type': 'earn', 'pts': 52, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 52, 'ref': None}]}, {'balance': 52, 'rejected': ['e2'], 'applied': ['e1']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 103, 'ref': None}]}, {'balance': 53, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 153, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 83, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 63, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 53, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 33, 'ref': None}]}, {'balance': 3, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 15, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 15, 'rejected': ['x'], 'applied': ['e1']}], ['redelivered earlier ts', {'events': [{'id': 'e2', 'ts': 3, 'type': 'redeem', 'pts': 40, 'ref': None}, {'id': 'e1', 'ts': 5, 'type': 'earn', 'pts': 53, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 53, 'ref': None}]}, {'balance': 53, 'rejected': ['e2'], 'applied': ['e1']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 104, 'ref': None}]}, {'balance': 54, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 154, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 84, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 64, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 54, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 34, 'ref': None}]}, {'balance': 4, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 20, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 20, 'rejected': ['x'], 'applied': ['e1']}], ['redelivered earlier ts', {'events': [{'id': 'e2', 'ts': 3, 'type': 'redeem', 'pts': 40, 'ref': None}, {'id': 'e1', 'ts': 5, 'type': 'earn', 'pts': 54, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 54, 'ref': None}]}, {'balance': 54, 'rejected': ['e2'], 'applied': ['e1']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 105, 'ref': None}]}, {'balance': 55, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 155, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 85, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 65, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 55, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 35, 'ref': None}]}, {'balance': 5, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 25, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 25, 'rejected': ['x'], 'applied': ['e1']}], ['redelivered earlier ts', {'events': [{'id': 'e2', 'ts': 3, 'type': 'redeem', 'pts': 40, 'ref': None}, {'id': 'e1', 'ts': 5, 'type': 'earn', 'pts': 55, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 55, 'ref': None}]}, {'balance': 55, 'rejected': ['e2'], 'applied': ['e1']}]]]
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 redelivered earn with a later timestamp is applied at the wrong position.

Input {events:[{id,ts,type:"earn"|"redeem"|"reverse",pts,ref}]}. Duplicate ids are dropped keeping the first delivery in input order; survivors are applied sorted by (ts,id). Redeem requires balance >= pts or is rejected. Reverse undoes an applied earn or redeem once; unknown, rejected, already reversed, or reverse targets are rejected. Return {balance, rejected, applied}.

split: open-access


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

N = 1
observations = []
def solve(x):
    seen = set()
    evs = []
    for e in x['events']:
        if e['id'] in seen:
            continue
        seen.add(e['id'])
        evs.append(e)
    evs.sort(key=lambda e: e['ts'])
    bal = 0
    applied = {}
    undone = set()
    rej = []
    order = []
    for e in evs:
        t = e['type']
        if t == 'earn':
            bal += e['pts']
            applied[e['id']] = e['pts']
        elif t == 'redeem':
            if e['pts'] > bal:
                rej.append(e['id'])
                continue
            bal -= e['pts']
            applied[e['id']] = -e['pts']
        else:
            r = e['ref']
            if r not in applied or r in undone:
                rej.append(e['id'])
                continue
            bal -= applied[r]
            undone.add(r)
        order.append(e['id'])
    return {'balance': bal, 'rejected': rej, 'applied': order}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 101, 'ref': None}]}, {'balance': 51, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 151, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 81, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 61, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 51, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 31, 'ref': None}]}, {'balance': 1, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 5, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 5, 'rejected': ['x'], 'applied': ['e1']}], ['ids against time', {'events': [{'id': 'a', 'ts': 9, 'type': 'redeem', 'pts': 20, 'ref': None}, {'id': 'b', 'ts': 2, 'type': 'earn', 'pts': 31, 'ref': None}]}, {'balance': 11, 'rejected': [], 'applied': ['b', 'a']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 102, 'ref': None}]}, {'balance': 52, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 152, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 82, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 62, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 52, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 32, 'ref': None}]}, {'balance': 2, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 10, 'rejected': ['x'], 'applied': ['e1']}], ['ids against time', {'events': [{'id': 'a', 'ts': 9, 'type': 'redeem', 'pts': 20, 'ref': None}, {'id': 'b', 'ts': 2, 'type': 'earn', 'pts': 32, 'ref': None}]}, {'balance': 12, 'rejected': [], 'applied': ['b', 'a']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 103, 'ref': None}]}, {'balance': 53, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 153, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 83, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 63, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 53, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 33, 'ref': None}]}, {'balance': 3, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 15, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 15, 'rejected': ['x'], 'applied': ['e1']}], ['ids against time', {'events': [{'id': 'a', 'ts': 9, 'type': 'redeem', 'pts': 20, 'ref': None}, {'id': 'b', 'ts': 2, 'type': 'earn', 'pts': 33, 'ref': None}]}, {'balance': 13, 'rejected': [], 'applied': ['b', 'a']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 104, 'ref': None}]}, {'balance': 54, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 154, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 84, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 64, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 54, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 34, 'ref': None}]}, {'balance': 4, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 20, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 20, 'rejected': ['x'], 'applied': ['e1']}], ['ids against time', {'events': [{'id': 'a', 'ts': 9, 'type': 'redeem', 'pts': 20, 'ref': None}, {'id': 'b', 'ts': 2, 'type': 'earn', 'pts': 34, 'ref': None}]}, {'balance': 14, 'rejected': [], 'applied': ['b', 'a']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 105, 'ref': None}]}, {'balance': 55, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 155, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 85, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 65, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 55, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 35, 'ref': None}]}, {'balance': 5, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 25, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 25, 'rejected': ['x'], 'applied': ['e1']}], ['ids against time', {'events': [{'id': 'a', 'ts': 9, 'type': 'redeem', 'pts': 20, 'ref': None}, {'id': 'b', 'ts': 2, 'type': 'earn', 'pts': 35, 'ref': None}]}, {'balance': 15, 'rejected': [], 'applied': ['b', 'a']}]]]
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):
    seen = set()
    evs = []
    for e in x['events']:
        if e['id'] in seen:
            continue
        seen.add(e['id'])
        evs.append(e)
    evs.sort(key=lambda e: e['id'])
    bal = 0
    applied = {}
    undone = set()
    rej = []
    order = []
    for e in evs:
        t = e['type']
        if t == 'earn':
            bal += e['pts']
            applied[e['id']] = e['pts']
        elif t == 'redeem':
            if e['pts'] > bal:
                rej.append(e['id'])
                continue
            bal -= e['pts']
            applied[e['id']] = -e['pts']
        else:
            r = e['ref']
            if r not in applied or r in undone:
                rej.append(e['id'])
                continue
            bal -= applied[r]
            undone.add(r)
        order.append(e['id'])
    return {'balance': bal, 'rejected': rej, 'applied': order}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 101, 'ref': None}]}, {'balance': 51, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 151, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 81, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 61, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 51, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 31, 'ref': None}]}, {'balance': 1, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 5, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 5, 'rejected': ['x'], 'applied': ['e1']}], ['ids against time', {'events': [{'id': 'a', 'ts': 9, 'type': 'redeem', 'pts': 20, 'ref': None}, {'id': 'b', 'ts': 2, 'type': 'earn', 'pts': 31, 'ref': None}]}, {'balance': 11, 'rejected': [], 'applied': ['b', 'a']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 102, 'ref': None}]}, {'balance': 52, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 152, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 82, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 62, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 52, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 32, 'ref': None}]}, {'balance': 2, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 10, 'rejected': ['x'], 'applied': ['e1']}], ['ids against time', {'events': [{'id': 'a', 'ts': 9, 'type': 'redeem', 'pts': 20, 'ref': None}, {'id': 'b', 'ts': 2, 'type': 'earn', 'pts': 32, 'ref': None}]}, {'balance': 12, 'rejected': [], 'applied': ['b', 'a']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 103, 'ref': None}]}, {'balance': 53, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 153, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 83, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 63, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 53, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 33, 'ref': None}]}, {'balance': 3, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 15, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 15, 'rejected': ['x'], 'applied': ['e1']}], ['ids against time', {'events': [{'id': 'a', 'ts': 9, 'type': 'redeem', 'pts': 20, 'ref': None}, {'id': 'b', 'ts': 2, 'type': 'earn', 'pts': 33, 'ref': None}]}, {'balance': 13, 'rejected': [], 'applied': ['b', 'a']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 104, 'ref': None}]}, {'balance': 54, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 154, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 84, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 64, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 54, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 34, 'ref': None}]}, {'balance': 4, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 20, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 20, 'rejected': ['x'], 'applied': ['e1']}], ['ids against time', {'events': [{'id': 'a', 'ts': 9, 'type': 'redeem', 'pts': 20, 'ref': None}, {'id': 'b', 'ts': 2, 'type': 'earn', 'pts': 34, 'ref': None}]}, {'balance': 14, 'rejected': [], 'applied': ['b', 'a']}]], [['out of order', {'events': [{'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 50, 'ref': None}, {'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 105, 'ref': None}]}, {'balance': 55, 'rejected': [], 'applied': ['e1', 'e2']}], ['duplicate delivery', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e1', 'ts': 9, 'type': 'earn', 'pts': 100, 'ref': None}, {'id': 'e2', 'ts': 5, 'type': 'redeem', 'pts': 155, 'ref': None}]}, {'balance': 100, 'rejected': ['e2'], 'applied': ['e1']}], ['reverse redemption', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 200, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 85, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 200, 'rejected': [], 'applied': ['e1', 'e2', 'e3']}], ['double reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 65, 'ref': None}, {'id': 'r1', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r2', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e1'}, {'id': 'r3', 'ts': 4, 'type': 'reverse', 'pts': 0, 'ref': 'r1'}]}, {'balance': 0, 'rejected': ['r2', 'r3'], 'applied': ['e1', 'r1']}], ['reverse rejected redeem', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 10, 'ref': None}, {'id': 'e2', 'ts': 2, 'type': 'redeem', 'pts': 55, 'ref': None}, {'id': 'e3', 'ts': 3, 'type': 'reverse', 'pts': 0, 'ref': 'e2'}]}, {'balance': 10, 'rejected': ['e2', 'e3'], 'applied': ['e1']}], ['same timestamp', {'events': [{'id': 'b', 'ts': 7, 'type': 'redeem', 'pts': 30, 'ref': None}, {'id': 'a', 'ts': 7, 'type': 'earn', 'pts': 35, 'ref': None}]}, {'balance': 5, 'rejected': [], 'applied': ['a', 'b']}], ['unknown reverse', {'events': [{'id': 'e1', 'ts': 1, 'type': 'earn', 'pts': 25, 'ref': None}, {'id': 'x', 'ts': 2, 'type': 'reverse', 'pts': 0, 'ref': 'nope'}]}, {'balance': 25, 'rejected': ['x'], 'applied': ['e1']}], ['ids against time', {'events': [{'id': 'a', 'ts': 9, 'type': 'redeem', 'pts': 20, 'ref': None}, {'id': 'b', 'ts': 2, 'type': 'earn', 'pts': 35, 'ref': None}]}, {'balance': 15, 'rejected': [], 'applied': ['b', 'a']}]]]
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:
Events with equal timestamps are applied in delivery order, so a redeem can be rejected before its funding earn.

Input {events:[{id,ts,type:"earn"|"redeem"|"reverse",pts,ref}]}. Duplicate ids are dropped keeping the first delivery in input order; survivors are applied sorted by (ts,id). Redeem requires balance >= pts or is rejected. Reverse undoes an applied earn or redeem once; unknown, rejected, already reversed, or reverse targets are rejected. Return {balance, rejected, applied}.

split: open-access