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'], 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 -= abs(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']}]], [['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']}]], [['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']}]], [['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']}]], [['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']}]]]
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['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']}]], [['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']}]], [['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']}]], [['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']}]], [['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']}]]]
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:
Reversing a redemption removes more points instead of restoring them.

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'], 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:
                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']}]], [['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']}]], [['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']}]], [['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']}]], [['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']}]]]
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):
    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'], 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(e['id'])
        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']}]], [['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']}]], [['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']}]], [['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']}]], [['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']}]]]
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 same earn can be reversed repeatedly, draining the balance.

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'], 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':
            applied[e['id']] = -e['pts']
            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']}]], [['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']}]], [['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']}]], [['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']}]], [['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']}]]]
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['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':
            applied[e['id']] = 0
            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']}]], [['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']}]], [['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']}]], [['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']}]], [['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']}]]]
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:
Reversing a redemption that was rejected for insufficient points credits points that were never taken.

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):
    usable = x['pts']
    if usable < x['min_pts']:
        return {'error': 'below minimum'}
    miles = usable * x['num'] // x['den']
    bonus = miles * x['bonus_bp'] // 10000 if usable >= x['threshold'] else 0
    return {'used': usable, 'miles': miles, 'bonus': bonus, 'leftover': x['pts'] - usable}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['normal', {'pts': 12751, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 751}], ['below minimum after flooring', {'pts': 5998, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5099, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 99}], ['bonus threshold edge', {'pts': 10010, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 10}], ['threshold by raw only', {'pts': 10401, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 401}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 8, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 4375, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3001, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60020, 'leftover': 321}]], [['normal', {'pts': 12752, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 752}], ['below minimum after flooring', {'pts': 5997, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5199, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 199}], ['bonus threshold edge', {'pts': 10020, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 20}], ['threshold by raw only', {'pts': 10402, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 402}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 9, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 3888, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3002, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60040, 'leftover': 321}]], [['normal', {'pts': 12753, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 753}], ['below minimum after flooring', {'pts': 5996, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5299, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 299}], ['bonus threshold edge', {'pts': 10030, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 30}], ['threshold by raw only', {'pts': 10403, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 403}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 10, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 3500, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3003, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60060, 'leftover': 321}]], [['normal', {'pts': 12754, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 754}], ['below minimum after flooring', {'pts': 5995, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5399, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 399}], ['bonus threshold edge', {'pts': 10040, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 40}], ['threshold by raw only', {'pts': 10404, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 404}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 11, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 3181, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3004, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60080, 'leftover': 321}]], [['normal', {'pts': 12755, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 755}], ['below minimum after flooring', {'pts': 5994, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5499, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 499}], ['bonus threshold edge', {'pts': 10050, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 50}], ['threshold by raw only', {'pts': 10405, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 405}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 12, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 2916, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3005, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60100, 'leftover': 321}]]]
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):
    usable = (x['pts'] + x['block'] // 2) // x['block'] * x['block']
    if usable < x['min_pts']:
        return {'error': 'below minimum'}
    miles = usable * x['num'] // x['den']
    bonus = miles * x['bonus_bp'] // 10000 if usable >= x['threshold'] else 0
    return {'used': usable, 'miles': miles, 'bonus': bonus, 'leftover': x['pts'] - usable}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['normal', {'pts': 12751, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 751}], ['below minimum after flooring', {'pts': 5998, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5099, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 99}], ['bonus threshold edge', {'pts': 10010, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 10}], ['threshold by raw only', {'pts': 10401, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 401}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 8, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 4375, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3001, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60020, 'leftover': 321}]], [['normal', {'pts': 12752, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 752}], ['below minimum after flooring', {'pts': 5997, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5199, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 199}], ['bonus threshold edge', {'pts': 10020, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 20}], ['threshold by raw only', {'pts': 10402, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 402}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 9, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 3888, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3002, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60040, 'leftover': 321}]], [['normal', {'pts': 12753, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 753}], ['below minimum after flooring', {'pts': 5996, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5299, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 299}], ['bonus threshold edge', {'pts': 10030, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 30}], ['threshold by raw only', {'pts': 10403, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 403}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 10, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 3500, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3003, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60060, 'leftover': 321}]], [['normal', {'pts': 12754, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 754}], ['below minimum after flooring', {'pts': 5995, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5399, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 399}], ['bonus threshold edge', {'pts': 10040, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 40}], ['threshold by raw only', {'pts': 10404, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 404}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 11, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 3181, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3004, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60080, 'leftover': 321}]], [['normal', {'pts': 12755, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 755}], ['below minimum after flooring', {'pts': 5994, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5499, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 499}], ['bonus threshold edge', {'pts': 10050, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 50}], ['threshold by raw only', {'pts': 10405, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 405}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 12, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 2916, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3005, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60100, 'leftover': 321}]]]
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:
Odd point balances are converted in full, including fractions of a transfer block.

Input {pts, block, min_pts, num, den, bonus_bp, threshold}. Usable points = pts floored to a block; usable below min_pts returns {"error"}. Miles = floor(usable*num/den). If usable >= threshold, bonus miles = floor(miles*bonus_bp/10000). Leftover points stay in the account. Return {used, miles, bonus, leftover}.

split: open-access


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

N = 1
observations = []
def solve(x):
    usable = x['pts'] // x['block'] * x['block']
    if x['pts'] < x['min_pts']:
        return {'error': 'below minimum'}
    miles = usable * x['num'] // x['den']
    bonus = miles * x['bonus_bp'] // 10000 if usable >= x['threshold'] else 0
    return {'used': usable, 'miles': miles, 'bonus': bonus, 'leftover': x['pts'] - usable}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['normal', {'pts': 12751, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 751}], ['below minimum after flooring', {'pts': 5998, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5099, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 99}], ['bonus threshold edge', {'pts': 10010, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 10}], ['threshold by raw only', {'pts': 10401, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 401}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 8, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 4375, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3001, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60020, 'leftover': 321}]], [['normal', {'pts': 12752, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 752}], ['below minimum after flooring', {'pts': 5997, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5199, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 199}], ['bonus threshold edge', {'pts': 10020, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 20}], ['threshold by raw only', {'pts': 10402, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 402}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 9, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 3888, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3002, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60040, 'leftover': 321}]], [['normal', {'pts': 12753, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 753}], ['below minimum after flooring', {'pts': 5996, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5299, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 299}], ['bonus threshold edge', {'pts': 10030, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 30}], ['threshold by raw only', {'pts': 10403, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 403}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 10, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 3500, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3003, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60060, 'leftover': 321}]], [['normal', {'pts': 12754, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 754}], ['below minimum after flooring', {'pts': 5995, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5399, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 399}], ['bonus threshold edge', {'pts': 10040, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 40}], ['threshold by raw only', {'pts': 10404, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 404}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 11, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 3181, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3004, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60080, 'leftover': 321}]], [['normal', {'pts': 12755, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 755}], ['below minimum after flooring', {'pts': 5994, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5499, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 499}], ['bonus threshold edge', {'pts': 10050, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 50}], ['threshold by raw only', {'pts': 10405, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 405}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 12, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 2916, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3005, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60100, 'leftover': 321}]]]
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):
    usable = x['pts'] // x['block'] * x['block']
    if usable <= x['min_pts']:
        return {'error': 'below minimum'}
    miles = usable * x['num'] // x['den']
    bonus = miles * x['bonus_bp'] // 10000 if usable >= x['threshold'] else 0
    return {'used': usable, 'miles': miles, 'bonus': bonus, 'leftover': x['pts'] - usable}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['normal', {'pts': 12751, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 751}], ['below minimum after flooring', {'pts': 5998, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5099, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 99}], ['bonus threshold edge', {'pts': 10010, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 10}], ['threshold by raw only', {'pts': 10401, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 401}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 8, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 4375, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3001, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60020, 'leftover': 321}]], [['normal', {'pts': 12752, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 752}], ['below minimum after flooring', {'pts': 5997, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5199, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 199}], ['bonus threshold edge', {'pts': 10020, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 20}], ['threshold by raw only', {'pts': 10402, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 402}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 9, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 3888, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3002, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60040, 'leftover': 321}]], [['normal', {'pts': 12753, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 753}], ['below minimum after flooring', {'pts': 5996, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5299, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 299}], ['bonus threshold edge', {'pts': 10030, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 30}], ['threshold by raw only', {'pts': 10403, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 403}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 10, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 3500, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3003, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60060, 'leftover': 321}]], [['normal', {'pts': 12754, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 754}], ['below minimum after flooring', {'pts': 5995, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5399, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 399}], ['bonus threshold edge', {'pts': 10040, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 40}], ['threshold by raw only', {'pts': 10404, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 404}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 11, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 3181, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3004, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60080, 'leftover': 321}]], [['normal', {'pts': 12755, 'block': 1000, 'min_pts': 5000, 'num': 3, 'den': 4, 'bonus_bp': 2500, 'threshold': 10000}, {'used': 12000, 'miles': 9000, 'bonus': 2250, 'leftover': 755}], ['below minimum after flooring', {'pts': 5994, 'block': 1000, 'min_pts': 5500, 'num': 1, 'den': 1, 'bonus_bp': 0, 'threshold': 10000}, {'error': 'below minimum'}], ['raw above minimum', {'pts': 5499, 'block': 1000, 'min_pts': 5000, 'num': 2, 'den': 3, 'bonus_bp': 1000, 'threshold': 5000}, {'used': 5000, 'miles': 3333, 'bonus': 333, 'leftover': 499}], ['bonus threshold edge', {'pts': 10050, 'block': 500, 'min_pts': 1000, 'num': 1, 'den': 2, 'bonus_bp': 1500, 'threshold': 10000}, {'used': 10000, 'miles': 5000, 'bonus': 750, 'leftover': 50}], ['threshold by raw only', {'pts': 10405, 'block': 1000, 'min_pts': 1000, 'num': 1, 'den': 1, 'bonus_bp': 2000, 'threshold': 10200}, {'used': 10000, 'miles': 10000, 'bonus': 0, 'leftover': 405}], ['awkward ratio', {'pts': 7000, 'block': 250, 'min_pts': 2500, 'num': 5, 'den': 12, 'bonus_bp': 0, 'threshold': 99999}, {'used': 7000, 'miles': 2916, 'bonus': 0, 'leftover': 0}], ['large', {'pts': 250321, 'block': 1000, 'min_pts': 5000, 'num': 4, 'den': 5, 'bonus_bp': 3005, 'threshold': 50000}, {'used': 250000, 'miles': 200000, 'bonus': 60100, 'leftover': 321}]]]
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 conversion goes through with fewer points than the partner minimum after block flooring.

Input {pts, block, min_pts, num, den, bonus_bp, threshold}. Usable points = pts floored to a block; usable below min_pts returns {"error"}. Miles = floor(usable*num/den). If usable >= threshold, bonus miles = floor(miles*bonus_bp/10000). Leftover points stay in the account. Return {used, miles, bonus, leftover}.

split: open-access