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

N = 1
observations = []
def solve(x):
    order = ['member', 'silver', 'gold', 'platinum']
    m = x['eval_month']
    q = sum(p for mo, p in x['events'] if m - 11 <= mo <= m)
    earned = 'member'
    for t in order[1:]:
        if q >= x['thresholds'][t]:
            earned = t
            break
    cur = order.index(x['current_tier'])
    e = order.index(earned)
    new = e if e >= cur else cur - 1
    return {'qualifying': q, 'earned': earned, 'tier': order[new]}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3501]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15501, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1001]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5001, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 7, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15999]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15999, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 1]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45001, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6001], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6101, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3502]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15502, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1002]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5002, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 8, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15998]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15998, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 2]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45002, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6002], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6102, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3503]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15503, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1003]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5003, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 9, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15997]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15997, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 3]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45003, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6003], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6103, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3504]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15504, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1004]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5004, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 10, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15996]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15996, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 4]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45004, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6004], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6104, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3505]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15505, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1005]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5005, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 11, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15995]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15995, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 5]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45005, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6005], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6105, 'earned': 'silver', 'tier': 'silver'}]]]
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):
    order = ['member', 'silver', 'gold', 'platinum']
    m = x['eval_month']
    q = sum(p for mo, p in x['events'] if m - 11 <= mo <= m)
    earned = 'member'
    for t in order[:0:-1]:
        if q >= x['thresholds'][t]:
            earned = t
    cur = order.index(x['current_tier'])
    e = order.index(earned)
    new = e if e >= cur else cur - 1
    return {'qualifying': q, 'earned': earned, 'tier': order[new]}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3501]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15501, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1001]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5001, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 7, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15999]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15999, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 1]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45001, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6001], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6101, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3502]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15502, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1002]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5002, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 8, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15998]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15998, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 2]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45002, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6002], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6102, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3503]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15503, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1003]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5003, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 9, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15997]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15997, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 3]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45003, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6003], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6103, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3504]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15504, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1004]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5004, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 10, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15996]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15996, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 4]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45004, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6004], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6104, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3505]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15505, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1005]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5005, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 11, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15995]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15995, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 5]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45005, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6005], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6105, 'earned': 'silver', 'tier': 'silver'}]]]
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 member who qualifies for gold is only granted silver.

Input {events:[[month,qualifying pts]], eval_month, current_tier, thresholds}. Qualifying points are summed over months eval-11..eval inclusive. The earned tier is the highest tier whose threshold is met (>=). Upgrades take effect fully; downgrades drop at most one level per evaluation. Tiers: member < silver < gold < platinum. Return {qualifying, earned, tier}.

split: open-access


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

N = 1
observations = []
def solve(x):
    order = ['member', 'silver', 'gold', 'platinum']
    m = x['eval_month']
    q = sum(p for mo, p in x['events'] if m - 11 <= mo <= m)
    earned = 'member'
    for t in order[1:]:
        if q >= x['thresholds'][t]:
            earned = t
    cur = order.index(x['current_tier'])
    e = order.index(earned)
    new = e
    return {'qualifying': q, 'earned': earned, 'tier': order[new]}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3501]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15501, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1001]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5001, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 7, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15999]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15999, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 1]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45001, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6001], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6101, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3502]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15502, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1002]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5002, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 8, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15998]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15998, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 2]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45002, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6002], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6102, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3503]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15503, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1003]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5003, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 9, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15997]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15997, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 3]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45003, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6003], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6103, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3504]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15504, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1004]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5004, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 10, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15996]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15996, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 4]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45004, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6004], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6104, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3505]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15505, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1005]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5005, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 11, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15995]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15995, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 5]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45005, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6005], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6105, 'earned': 'silver', 'tier': 'silver'}]]]
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):
    order = ['member', 'silver', 'gold', 'platinum']
    m = x['eval_month']
    q = sum(p for mo, p in x['events'] if m - 11 <= mo <= m)
    earned = 'member'
    for t in order[1:]:
        if q >= x['thresholds'][t]:
            earned = t
    cur = order.index(x['current_tier'])
    e = order.index(earned)
    new = e if e >= cur else cur
    return {'qualifying': q, 'earned': earned, 'tier': order[new]}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3501]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15501, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1001]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5001, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 7, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15999]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15999, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 1]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45001, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6001], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6101, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3502]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15502, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1002]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5002, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 8, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15998]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15998, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 2]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45002, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6002], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6102, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3503]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15503, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1003]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5003, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 9, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15997]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15997, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 3]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45003, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6003], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6103, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3504]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15504, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1004]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5004, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 10, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15996]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15996, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 4]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45004, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6004], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6104, 'earned': 'silver', 'tier': 'silver'}]], [['upgrade', {'events': [[1, 3000], [5, 9000], [11, 3505]], 'eval_month': 12, 'current_tier': 'silver', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15505, 'earned': 'gold', 'tier': 'gold'}], ['window edges', {'events': [[0, 20000], [1, 4000], [12, 1005]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 5005, 'earned': 'silver', 'tier': 'silver'}], ['future month', {'events': [[10, 4000], [13, 20000]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 4000, 'earned': 'member', 'tier': 'member'}], ['exact threshold', {'events': [[6, 15000]], 'eval_month': 11, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15000, 'earned': 'gold', 'tier': 'gold'}], ['soft landing', {'events': [[3, 1000]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 1000, 'earned': 'member', 'tier': 'gold'}], ['one step down', {'events': [[8, 15995]], 'eval_month': 12, 'current_tier': 'platinum', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 15995, 'earned': 'gold', 'tier': 'gold'}], ['big jump', {'events': [[12, 45000], [2, 5]], 'eval_month': 12, 'current_tier': 'member', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 45005, 'earned': 'platinum', 'tier': 'platinum'}], ['current month', {'events': [[20, 6005], [9, 100]], 'eval_month': 20, 'current_tier': 'gold', 'thresholds': {'silver': 5000, 'gold': 15000, 'platinum': 40000}}, {'qualifying': 6105, 'earned': 'silver', 'tier': 'silver'}]]]
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 platinum member with a weak year falls straight to member status.

Input {events:[[month,qualifying pts]], eval_month, current_tier, thresholds}. Qualifying points are summed over months eval-11..eval inclusive. The earned tier is the highest tier whose threshold is met (>=). Upgrades take effect fully; downgrades drop at most one level per evaluation. Tiers: member < silver < gold < platinum. Return {qualifying, earned, tier}.

split: open-access


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

N = 1
observations = []
def solve(x):
    cum = 0
    clawed = 0
    bal = x['balance']
    debt = 0
    out = []
    for r in x['refunds']:
        cum = min(x['order_total'], cum + r)
        target = (2 * x['earned'] * cum + x['order_total']) // (2 * x['order_total'])
        c = (2 * x['earned'] * r + x['order_total']) // (2 * x['order_total'])
        clawed = target
        take = min(c, bal)
        bal -= take
        debt += c - take
        out.append(c)
    return {'claws': out, 'balance': bal, 'debt': debt}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2501], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 51}, {'claws': [3, 4, 3], 'balance': 41, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3001], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 31}, {'claws': [100, 100], 'balance': 0, 'debt': 169}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 1}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 89}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 11}, {'claws': [2], 'balance': 9, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [2], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2502], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 52}, {'claws': [3, 4, 3], 'balance': 42, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3002], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 32}, {'claws': [100, 100], 'balance': 0, 'debt': 168}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 2}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 88}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 12}, {'claws': [2], 'balance': 10, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [3], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2503], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 53}, {'claws': [3, 4, 3], 'balance': 43, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3003], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 33}, {'claws': [100, 100], 'balance': 0, 'debt': 167}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 3}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 87}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 13}, {'claws': [2], 'balance': 11, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [4], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2504], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 54}, {'claws': [3, 4, 3], 'balance': 44, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3004], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 34}, {'claws': [100, 100], 'balance': 0, 'debt': 166}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 4}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 86}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 14}, {'claws': [2], 'balance': 12, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [5], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2505], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 55}, {'claws': [3, 4, 3], 'balance': 45, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3005], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 35}, {'claws': [100, 100], 'balance': 0, 'debt': 165}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 5}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 85}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 15}, {'claws': [2], 'balance': 13, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [6], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]]]
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):
    cum = 0
    clawed = 0
    bal = x['balance']
    debt = 0
    out = []
    for r in x['refunds']:
        cum = min(x['order_total'], cum + r)
        target = x['earned'] * cum // x['order_total']
        c = target - clawed
        clawed = target
        take = min(c, bal)
        bal -= take
        debt += c - take
        out.append(c)
    return {'claws': out, 'balance': bal, 'debt': debt}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2501], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 51}, {'claws': [3, 4, 3], 'balance': 41, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3001], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 31}, {'claws': [100, 100], 'balance': 0, 'debt': 169}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 1}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 89}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 11}, {'claws': [2], 'balance': 9, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [2], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2502], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 52}, {'claws': [3, 4, 3], 'balance': 42, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3002], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 32}, {'claws': [100, 100], 'balance': 0, 'debt': 168}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 2}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 88}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 12}, {'claws': [2], 'balance': 10, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [3], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2503], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 53}, {'claws': [3, 4, 3], 'balance': 43, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3003], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 33}, {'claws': [100, 100], 'balance': 0, 'debt': 167}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 3}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 87}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 13}, {'claws': [2], 'balance': 11, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [4], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2504], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 54}, {'claws': [3, 4, 3], 'balance': 44, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3004], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 34}, {'claws': [100, 100], 'balance': 0, 'debt': 166}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 4}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 86}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 14}, {'claws': [2], 'balance': 12, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [5], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2505], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 55}, {'claws': [3, 4, 3], 'balance': 45, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3005], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 35}, {'claws': [100, 100], 'balance': 0, 'debt': 165}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 5}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 85}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 15}, {'claws': [2], 'balance': 13, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [6], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]]]
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:
Refunding an order in thirds claws back more or fewer points than refunding it at once.

Input {earned, order_total, refunds:[cents], balance}. Cumulative refunds are capped at the order total. After each refund the cumulative clawback target is round-half-up(earned*cumulative refund/order total); the refund claws target minus already clawed. A clawback is taken from the balance only down to zero; the shortfall accumulates as debt. Return {claws, balance, debt}.

split: open-access


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

N = 1
observations = []
def solve(x):
    cum = 0
    clawed = 0
    bal = x['balance']
    debt = 0
    out = []
    for r in x['refunds']:
        cum = cum + r
        target = (2 * x['earned'] * cum + x['order_total']) // (2 * x['order_total'])
        c = target - clawed
        clawed = target
        take = min(c, bal)
        bal -= take
        debt += c - take
        out.append(c)
    return {'claws': out, 'balance': bal, 'debt': debt}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2501], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 51}, {'claws': [3, 4, 3], 'balance': 41, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3001], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 31}, {'claws': [100, 100], 'balance': 0, 'debt': 169}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 1}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 89}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 11}, {'claws': [2], 'balance': 9, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [2], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2502], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 52}, {'claws': [3, 4, 3], 'balance': 42, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3002], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 32}, {'claws': [100, 100], 'balance': 0, 'debt': 168}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 2}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 88}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 12}, {'claws': [2], 'balance': 10, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [3], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2503], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 53}, {'claws': [3, 4, 3], 'balance': 43, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3003], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 33}, {'claws': [100, 100], 'balance': 0, 'debt': 167}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 3}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 87}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 13}, {'claws': [2], 'balance': 11, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [4], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2504], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 54}, {'claws': [3, 4, 3], 'balance': 44, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3004], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 34}, {'claws': [100, 100], 'balance': 0, 'debt': 166}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 4}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 86}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 14}, {'claws': [2], 'balance': 12, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [5], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2505], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 55}, {'claws': [3, 4, 3], 'balance': 45, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3005], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 35}, {'claws': [100, 100], 'balance': 0, 'debt': 165}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 5}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 85}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 15}, {'claws': [2], 'balance': 13, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [6], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]]]
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):
    cum = 0
    clawed = 0
    bal = x['balance']
    debt = 0
    out = []
    for r in x['refunds']:
        cum = cum + min(x['order_total'], r)
        target = (2 * x['earned'] * cum + x['order_total']) // (2 * x['order_total'])
        c = target - clawed
        clawed = target
        take = min(c, bal)
        bal -= take
        debt += c - take
        out.append(c)
    return {'claws': out, 'balance': bal, 'debt': debt}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2501], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 51}, {'claws': [3, 4, 3], 'balance': 41, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3001], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 31}, {'claws': [100, 100], 'balance': 0, 'debt': 169}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 1}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 89}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 11}, {'claws': [2], 'balance': 9, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [2], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2502], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 52}, {'claws': [3, 4, 3], 'balance': 42, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3002], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 32}, {'claws': [100, 100], 'balance': 0, 'debt': 168}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 2}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 88}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 12}, {'claws': [2], 'balance': 10, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [3], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2503], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 53}, {'claws': [3, 4, 3], 'balance': 43, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3003], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 33}, {'claws': [100, 100], 'balance': 0, 'debt': 167}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 3}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 87}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 13}, {'claws': [2], 'balance': 11, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [4], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2504], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 54}, {'claws': [3, 4, 3], 'balance': 44, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3004], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 34}, {'claws': [100, 100], 'balance': 0, 'debt': 166}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 4}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 86}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 14}, {'claws': [2], 'balance': 12, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [5], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2505], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 55}, {'claws': [3, 4, 3], 'balance': 45, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3005], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 35}, {'claws': [100, 100], 'balance': 0, 'debt': 165}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 5}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 85}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 15}, {'claws': [2], 'balance': 13, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [6], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]]]
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:
Refunds that exceed the order value (e.g. including a goodwill credit) claw back more points than were earned.

Input {earned, order_total, refunds:[cents], balance}. Cumulative refunds are capped at the order total. After each refund the cumulative clawback target is round-half-up(earned*cumulative refund/order total); the refund claws target minus already clawed. A clawback is taken from the balance only down to zero; the shortfall accumulates as debt. Return {claws, balance, debt}.

split: open-access


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

N = 1
observations = []
def solve(x):
    cum = 0
    clawed = 0
    bal = x['balance']
    debt = 0
    out = []
    for r in x['refunds']:
        cum = min(x['order_total'], cum + r)
        target = (2 * x['earned'] * cum + x['order_total']) // (2 * x['order_total'])
        c = target - clawed
        clawed = target
        take = c
        bal -= take
        debt += c - take
        out.append(c)
    return {'claws': out, 'balance': bal, 'debt': debt}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2501], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 51}, {'claws': [3, 4, 3], 'balance': 41, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3001], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 31}, {'claws': [100, 100], 'balance': 0, 'debt': 169}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 1}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 89}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 11}, {'claws': [2], 'balance': 9, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [2], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2502], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 52}, {'claws': [3, 4, 3], 'balance': 42, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3002], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 32}, {'claws': [100, 100], 'balance': 0, 'debt': 168}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 2}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 88}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 12}, {'claws': [2], 'balance': 10, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [3], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2503], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 53}, {'claws': [3, 4, 3], 'balance': 43, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3003], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 33}, {'claws': [100, 100], 'balance': 0, 'debt': 167}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 3}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 87}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 13}, {'claws': [2], 'balance': 11, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [4], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2504], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 54}, {'claws': [3, 4, 3], 'balance': 44, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3004], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 34}, {'claws': [100, 100], 'balance': 0, 'debt': 166}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 4}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 86}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 14}, {'claws': [2], 'balance': 12, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [5], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2505], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 55}, {'claws': [3, 4, 3], 'balance': 45, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3005], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 35}, {'claws': [100, 100], 'balance': 0, 'debt': 165}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 5}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 85}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 15}, {'claws': [2], 'balance': 13, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [6], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]]]
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):
    cum = 0
    clawed = 0
    bal = x['balance']
    debt = 0
    out = []
    for r in x['refunds']:
        cum = min(x['order_total'], cum + r)
        target = (2 * x['earned'] * cum + x['order_total']) // (2 * x['order_total'])
        c = target - clawed
        clawed = target
        take = min(c, bal)
        bal -= take
        debt += 0
        out.append(c)
    return {'claws': out, 'balance': bal, 'debt': debt}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2501], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 51}, {'claws': [3, 4, 3], 'balance': 41, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3001], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 31}, {'claws': [100, 100], 'balance': 0, 'debt': 169}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 1}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 89}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 11}, {'claws': [2], 'balance': 9, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [2], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2502], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 52}, {'claws': [3, 4, 3], 'balance': 42, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3002], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 32}, {'claws': [100, 100], 'balance': 0, 'debt': 168}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 2}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 88}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 12}, {'claws': [2], 'balance': 10, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [3], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2503], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 53}, {'claws': [3, 4, 3], 'balance': 43, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3003], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 33}, {'claws': [100, 100], 'balance': 0, 'debt': 167}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 3}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 87}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 13}, {'claws': [2], 'balance': 11, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [4], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2504], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 54}, {'claws': [3, 4, 3], 'balance': 44, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3004], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 34}, {'claws': [100, 100], 'balance': 0, 'debt': 166}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 4}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 86}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 14}, {'claws': [2], 'balance': 12, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [5], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2505], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 55}, {'claws': [3, 4, 3], 'balance': 45, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3005], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 35}, {'claws': [100, 100], 'balance': 0, 'debt': 165}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 5}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 85}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 15}, {'claws': [2], 'balance': 13, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [6], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]]]
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:
Clawbacks push balances negative and no debt is tracked.

Input {earned, order_total, refunds:[cents], balance}. Cumulative refunds are capped at the order total. After each refund the cumulative clawback target is round-half-up(earned*cumulative refund/order total); the refund claws target minus already clawed. A clawback is taken from the balance only down to zero; the shortfall accumulates as debt. Return {claws, balance, debt}.

split: open-access


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

N = 1
observations = []
def solve(x):
    cum = 0
    clawed = 0
    bal = x['balance']
    debt = 0
    out = []
    for r in x['refunds']:
        cum = min(x['order_total'], cum + r)
        target = (2 * x['earned'] * cum + x['order_total']) // (2 * x['order_total'])
        c = target - clawed
        clawed = target
        take = min(c, bal)
        bal -= take
        debt = c - take
        out.append(c)
    return {'claws': out, 'balance': bal, 'debt': debt}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2501], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 51}, {'claws': [3, 4, 3], 'balance': 41, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3001], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 31}, {'claws': [100, 100], 'balance': 0, 'debt': 169}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 1}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 89}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 11}, {'claws': [2], 'balance': 9, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [2], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2502], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 52}, {'claws': [3, 4, 3], 'balance': 42, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3002], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 32}, {'claws': [100, 100], 'balance': 0, 'debt': 168}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 2}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 88}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 12}, {'claws': [2], 'balance': 10, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [3], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2503], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 53}, {'claws': [3, 4, 3], 'balance': 43, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3003], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 33}, {'claws': [100, 100], 'balance': 0, 'debt': 167}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 3}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 87}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 13}, {'claws': [2], 'balance': 11, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [4], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2504], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 54}, {'claws': [3, 4, 3], 'balance': 44, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3004], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 34}, {'claws': [100, 100], 'balance': 0, 'debt': 166}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 4}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 86}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 14}, {'claws': [2], 'balance': 12, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [5], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2505], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 55}, {'claws': [3, 4, 3], 'balance': 45, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3005], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 35}, {'claws': [100, 100], 'balance': 0, 'debt': 165}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 5}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 85}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 15}, {'claws': [2], 'balance': 13, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [6], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]]]
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):
    cum = 0
    clawed = 0
    bal = x['balance']
    debt = 0
    out = []
    for r in x['refunds']:
        cum = min(x['order_total'], cum + r)
        target = (2 * x['earned'] * cum + x['order_total']) // (2 * x['order_total'])
        c = target - clawed
        clawed = target
        take = min(c, bal)
        bal -= take
        debt = max(debt, c - take)
        out.append(c)
    return {'claws': out, 'balance': bal, 'debt': debt}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2501], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 51}, {'claws': [3, 4, 3], 'balance': 41, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3001], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 31}, {'claws': [100, 100], 'balance': 0, 'debt': 169}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 1}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 89}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 11}, {'claws': [2], 'balance': 9, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [2], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2502], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 52}, {'claws': [3, 4, 3], 'balance': 42, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3002], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 32}, {'claws': [100, 100], 'balance': 0, 'debt': 168}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 2}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 88}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 12}, {'claws': [2], 'balance': 10, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [3], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2503], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 53}, {'claws': [3, 4, 3], 'balance': 43, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3003], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 33}, {'claws': [100, 100], 'balance': 0, 'debt': 167}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 3}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 87}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 13}, {'claws': [2], 'balance': 11, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [4], 'balance': 5}, {'claws': [0], 'balance': 5, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2504], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 54}, {'claws': [3, 4, 3], 'balance': 44, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3004], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 34}, {'claws': [100, 100], 'balance': 0, 'debt': 166}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 4}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 86}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 14}, {'claws': [2], 'balance': 12, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [5], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]], [['two partial refunds', {'earned': 125, 'order_total': 10000, 'refunds': [2500, 2505], 'balance': 1000}, {'claws': [31, 32], 'balance': 937, 'debt': 0}], ['rounding drift', {'earned': 10, 'order_total': 300, 'refunds': [100, 100, 100], 'balance': 55}, {'claws': [3, 4, 3], 'balance': 45, 'debt': 0}], ['over refund', {'earned': 80, 'order_total': 4000, 'refunds': [3000, 3005], 'balance': 500}, {'claws': [60, 20], 'balance': 420, 'debt': 0}], ['spent points', {'earned': 200, 'order_total': 2000, 'refunds': [1000, 1000], 'balance': 35}, {'claws': [100, 100], 'balance': 0, 'debt': 165}], ['debt grows', {'earned': 90, 'order_total': 900, 'refunds': [300, 300, 300], 'balance': 5}, {'claws': [30, 30, 30], 'balance': 0, 'debt': 85}], ['single half', {'earned': 3, 'order_total': 2, 'refunds': [1], 'balance': 15}, {'claws': [2], 'balance': 13, 'debt': 0}], ['refund one of many', {'earned': 999, 'order_total': 9990, 'refunds': [6], 'balance': 5}, {'claws': [1], 'balance': 4, 'debt': 0}]]]
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:
Only the most recent shortfall is shown as debt after several refunds.

Input {earned, order_total, refunds:[cents], balance}. Cumulative refunds are capped at the order total. After each refund the cumulative clawback target is round-half-up(earned*cumulative refund/order total); the refund claws target minus already clawed. A clawback is taken from the balance only down to zero; the shortfall accumulates as debt. Return {claws, balance, debt}.

split: open-access