broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import datetime
import calendar
N = 1
observations = []
def solve(x):
    t = datetime.date(*x['trial_end'])
    a = x['anchor_day']
    if t.day == a:
        ny, nm = (t.year + 1, 1) if t.month == 12 else (t.year, t.month + 1)
        return [x['price'], datetime.date(ny, nm, a).isoformat()]
    if t.day < a:
        nxt = datetime.date(t.year, t.month, a)
        py, pm = (t.year, 12) if t.month == 1 else (t.year, t.month - 1)
        prev = datetime.date(py, pm, a)
    else:
        ey, em = (t.year + 1, 1) if t.month == 12 else (t.year, t.month + 1)
        nxt = datetime.date(ey, em, a)
        prev = datetime.date(t.year, t.month, a)
    days = (nxt - t).days
    cycle = (nxt - prev).days
    return [(x['price'] * days * 2 + cycle) // (2 * cycle), nxt.isoformat()]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'trial_end': [2023, 1, 5], 'anchor_day': 28, 'price': 999}, [741, '2023-01-28']), ('regression', {'trial_end': [2025, 1, 1], 'anchor_day': 13, 'price': 2900}, [1123, '2025-01-13']), ('partial-repair probe', {'trial_end': [2024, 4, 27], 'anchor_day': 28, 'price': 999}, [32, '2024-04-28']), ('partial-repair probe', {'trial_end': [2024, 12, 16], 'anchor_day': 28, 'price': 2900}, [1160, '2024-12-28']), ('normal control', {'trial_end': [2024, 12, 25], 'anchor_day': 1, 'price': 12600}, [2845, '2025-01-01']), ('normal control', {'trial_end': [2023, 4, 14], 'anchor_day': 1, 'price': 999}, [566, '2023-05-01']), ('normal control', {'trial_end': [2023, 6, 15], 'anchor_day': 15, 'price': 999}, [999, '2023-07-15']), ('normal control', {'trial_end': [2023, 12, 16], 'anchor_day': 15, 'price': 2900}, [2806, '2024-01-15'])], [('regression', {'trial_end': [2025, 1, 1], 'anchor_day': 13, 'price': 2900}, [1123, '2025-01-13']), ('regression', {'trial_end': [2024, 1, 1], 'anchor_day': 28, 'price': 2900}, [2526, '2024-01-28']), ('partial-repair probe', {'trial_end': [2024, 4, 1], 'anchor_day': 28, 'price': 78891}, [68712, '2024-04-28']), ('partial-repair probe', {'trial_end': [2024, 6, 4], 'anchor_day': 28, 'price': 999}, [773, '2024-06-28']), ('normal control', {'trial_end': [2025, 3, 15], 'anchor_day': 15, 'price': 2900}, [2900, '2025-04-15']), ('normal control', {'trial_end': [2023, 12, 15], 'anchor_day': 1, 'price': 47732}, [26176, '2024-01-01']), ('normal control', {'trial_end': [2024, 2, 8], 'anchor_day': 8, 'price': 999}, [999, '2024-03-08']), ('normal control', {'trial_end': [2024, 6, 25], 'anchor_day': 1, 'price': 2900}, [580, '2024-07-01'])], [('regression', {'trial_end': [2024, 1, 1], 'anchor_day': 28, 'price': 2900}, [2526, '2024-01-28']), ('regression', {'trial_end': [2025, 1, 2], 'anchor_day': 28, 'price': 999}, [838, '2025-01-28']), ('partial-repair probe', {'trial_end': [2024, 4, 10], 'anchor_day': 15, 'price': 2900}, [468, '2024-04-15']), ('partial-repair probe', {'trial_end': [2023, 10, 3], 'anchor_day': 16, 'price': 999}, [433, '2023-10-16']), ('normal control', {'trial_end': [2023, 12, 26], 'anchor_day': 21, 'price': 999}, [838, '2024-01-21']), ('normal control', {'trial_end': [2023, 5, 26], 'anchor_day': 1, 'price': 62316}, [12061, '2023-06-01']), ('normal control', {'trial_end': [2023, 4, 30], 'anchor_day': 15, 'price': 44760}, [22380, '2023-05-15']), ('normal control', {'trial_end': [2025, 5, 3], 'anchor_day': 1, 'price': 2900}, [2713, '2025-06-01'])], [('regression', {'trial_end': [2025, 1, 2], 'anchor_day': 28, 'price': 999}, [838, '2025-01-28']), ('regression', {'trial_end': [2024, 1, 7], 'anchor_day': 15, 'price': 28964}, [7475, '2024-01-15']), ('partial-repair probe', {'trial_end': [2023, 6, 18], 'anchor_day': 28, 'price': 2900}, [935, '2023-06-28']), ('partial-repair probe', {'trial_end': [2025, 3, 22], 'anchor_day': 28, 'price': 58121}, [12455, '2025-03-28']), ('normal control', {'trial_end': [2024, 6, 14], 'anchor_day': 1, 'price': 999}, [566, '2024-07-01']), ('normal control', {'trial_end': [2023, 5, 23], 'anchor_day': 15, 'price': 2900}, [2152, '2023-06-15']), ('normal control', {'trial_end': [2023, 5, 31], 'anchor_day': 4, 'price': 999}, [129, '2023-06-04']), ('normal control', {'trial_end': [2023, 3, 16], 'anchor_day': 15, 'price': 9250}, [8952, '2023-04-15'])], [('regression', {'trial_end': [2024, 1, 7], 'anchor_day': 15, 'price': 28964}, [7475, '2024-01-15']), ('regression', {'trial_end': [2025, 1, 2], 'anchor_day': 28, 'price': 2360}, [1979, '2025-01-28']), ('partial-repair probe', {'trial_end': [2024, 3, 15], 'anchor_day': 28, 'price': 999}, [448, '2024-03-28']), ('partial-repair probe', {'trial_end': [2024, 12, 11], 'anchor_day': 15, 'price': 2900}, [387, '2024-12-15']), ('normal control', {'trial_end': [2025, 2, 28], 'anchor_day': 1, 'price': 999}, [36, '2025-03-01']), ('normal control', {'trial_end': [2023, 8, 18], 'anchor_day': 1, 'price': 2900}, [1310, '2023-09-01']), ('normal control', {'trial_end': [2025, 1, 4], 'anchor_day': 1, 'price': 39775}, [35926, '2025-02-01']), ('normal control', {'trial_end': [2024, 7, 15], 'anchor_day': 15, 'price': 999}, [999, '2024-08-15'])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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
import datetime
import calendar
N = 1
observations = []
def solve(x):
    t = datetime.date(*x['trial_end'])
    a = x['anchor_day']
    if t.day == a:
        ny, nm = (t.year + 1, 1) if t.month == 12 else (t.year, t.month + 1)
        return [x['price'], datetime.date(ny, nm, a).isoformat()]
    if t.day < a:
        nxt = datetime.date(t.year, t.month, a)
        py, pm = divmod(t.year * 12 + t.month - 2, 12)
        prev = datetime.date(py, pm, a)
    else:
        ey, em = (t.year + 1, 1) if t.month == 12 else (t.year, t.month + 1)
        nxt = datetime.date(ey, em, a)
        prev = datetime.date(t.year, t.month, a)
    days = (nxt - t).days
    cycle = (nxt - prev).days
    return [(x['price'] * days * 2 + cycle) // (2 * cycle), nxt.isoformat()]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'trial_end': [2023, 1, 5], 'anchor_day': 28, 'price': 999}, [741, '2023-01-28']), ('regression', {'trial_end': [2025, 1, 1], 'anchor_day': 13, 'price': 2900}, [1123, '2025-01-13']), ('partial-repair probe', {'trial_end': [2024, 4, 27], 'anchor_day': 28, 'price': 999}, [32, '2024-04-28']), ('partial-repair probe', {'trial_end': [2024, 12, 16], 'anchor_day': 28, 'price': 2900}, [1160, '2024-12-28']), ('normal control', {'trial_end': [2024, 12, 25], 'anchor_day': 1, 'price': 12600}, [2845, '2025-01-01']), ('normal control', {'trial_end': [2023, 4, 14], 'anchor_day': 1, 'price': 999}, [566, '2023-05-01']), ('normal control', {'trial_end': [2023, 6, 15], 'anchor_day': 15, 'price': 999}, [999, '2023-07-15']), ('normal control', {'trial_end': [2023, 12, 16], 'anchor_day': 15, 'price': 2900}, [2806, '2024-01-15'])], [('regression', {'trial_end': [2025, 1, 1], 'anchor_day': 13, 'price': 2900}, [1123, '2025-01-13']), ('regression', {'trial_end': [2024, 1, 1], 'anchor_day': 28, 'price': 2900}, [2526, '2024-01-28']), ('partial-repair probe', {'trial_end': [2024, 4, 1], 'anchor_day': 28, 'price': 78891}, [68712, '2024-04-28']), ('partial-repair probe', {'trial_end': [2024, 6, 4], 'anchor_day': 28, 'price': 999}, [773, '2024-06-28']), ('normal control', {'trial_end': [2025, 3, 15], 'anchor_day': 15, 'price': 2900}, [2900, '2025-04-15']), ('normal control', {'trial_end': [2023, 12, 15], 'anchor_day': 1, 'price': 47732}, [26176, '2024-01-01']), ('normal control', {'trial_end': [2024, 2, 8], 'anchor_day': 8, 'price': 999}, [999, '2024-03-08']), ('normal control', {'trial_end': [2024, 6, 25], 'anchor_day': 1, 'price': 2900}, [580, '2024-07-01'])], [('regression', {'trial_end': [2024, 1, 1], 'anchor_day': 28, 'price': 2900}, [2526, '2024-01-28']), ('regression', {'trial_end': [2025, 1, 2], 'anchor_day': 28, 'price': 999}, [838, '2025-01-28']), ('partial-repair probe', {'trial_end': [2024, 4, 10], 'anchor_day': 15, 'price': 2900}, [468, '2024-04-15']), ('partial-repair probe', {'trial_end': [2023, 10, 3], 'anchor_day': 16, 'price': 999}, [433, '2023-10-16']), ('normal control', {'trial_end': [2023, 12, 26], 'anchor_day': 21, 'price': 999}, [838, '2024-01-21']), ('normal control', {'trial_end': [2023, 5, 26], 'anchor_day': 1, 'price': 62316}, [12061, '2023-06-01']), ('normal control', {'trial_end': [2023, 4, 30], 'anchor_day': 15, 'price': 44760}, [22380, '2023-05-15']), ('normal control', {'trial_end': [2025, 5, 3], 'anchor_day': 1, 'price': 2900}, [2713, '2025-06-01'])], [('regression', {'trial_end': [2025, 1, 2], 'anchor_day': 28, 'price': 999}, [838, '2025-01-28']), ('regression', {'trial_end': [2024, 1, 7], 'anchor_day': 15, 'price': 28964}, [7475, '2024-01-15']), ('partial-repair probe', {'trial_end': [2023, 6, 18], 'anchor_day': 28, 'price': 2900}, [935, '2023-06-28']), ('partial-repair probe', {'trial_end': [2025, 3, 22], 'anchor_day': 28, 'price': 58121}, [12455, '2025-03-28']), ('normal control', {'trial_end': [2024, 6, 14], 'anchor_day': 1, 'price': 999}, [566, '2024-07-01']), ('normal control', {'trial_end': [2023, 5, 23], 'anchor_day': 15, 'price': 2900}, [2152, '2023-06-15']), ('normal control', {'trial_end': [2023, 5, 31], 'anchor_day': 4, 'price': 999}, [129, '2023-06-04']), ('normal control', {'trial_end': [2023, 3, 16], 'anchor_day': 15, 'price': 9250}, [8952, '2023-04-15'])], [('regression', {'trial_end': [2024, 1, 7], 'anchor_day': 15, 'price': 28964}, [7475, '2024-01-15']), ('regression', {'trial_end': [2025, 1, 2], 'anchor_day': 28, 'price': 2360}, [1979, '2025-01-28']), ('partial-repair probe', {'trial_end': [2024, 3, 15], 'anchor_day': 28, 'price': 999}, [448, '2024-03-28']), ('partial-repair probe', {'trial_end': [2024, 12, 11], 'anchor_day': 15, 'price': 2900}, [387, '2024-12-15']), ('normal control', {'trial_end': [2025, 2, 28], 'anchor_day': 1, 'price': 999}, [36, '2025-03-01']), ('normal control', {'trial_end': [2023, 8, 18], 'anchor_day': 1, 'price': 2900}, [1310, '2023-09-01']), ('normal control', {'trial_end': [2025, 1, 4], 'anchor_day': 1, 'price': 39775}, [35926, '2025-02-01']), ('normal control', {'trial_end': [2024, 7, 15], 'anchor_day': 15, 'price': 999}, [999, '2024-08-15'])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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:
January trials that end before the anchor day produce a negative stub charge.

Input {trial_end date, anchor_day 1..28, price}. If trial_end.day equals anchor_day, charge the full price and the next anchor is one month later. Otherwise charge price*days/cycle half-up, where days runs from trial_end to the next anchor date strictly after it and cycle is the length of the anchor-to-anchor month containing trial_end. Return [amount, next anchor ISO].

split: open-access


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

N = 1
observations = []
def solve(x):
    c = x['coupon']
    active = c['duration'] == 'forever' or (c['duration'] == 'once' and x['idx'] == 0) or (c['duration'] == 'repeating' and x['idx'] <= c['months'])
    pos = [a for a in x['lines'] if a > 0]
    discount = 0
    if active:
        if c['type'] == 'percent':
            discount = sum((a * c['value'] * 2 + 100) // 200 for a in pos)
        else:
            discount = min(c['value'], sum(pos))
    total = sum(x['lines']) - discount
    return [discount, max(0, total), max(0, -total)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'lines': [791], 'coupon': {'type': 'percent', 'value': 15, 'duration': 'repeating', 'months': 1}, 'idx': 1}, [0, 791, 0]), ('regression', {'lines': [-8928, 16007, 36, 31923, -28257], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 3}, [0, 10781, 0]), ('partial-repair probe', {'lines': [27067, -9430, 18496, 15243, 566], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 4}, 'idx': 3}, [20253, 31689, 0]), ('partial-repair probe', {'lines': [463, 18993, 591, 25859, 508], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [500, 45914, 0]), ('normal control', {'lines': [-9318], 'coupon': {'type': 'amount', 'value': 29602, 'duration': 'forever', 'months': 3}, 'idx': 7}, [0, 0, 9318]), ('normal control', {'lines': [165], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'once', 'months': 3}, 'idx': 4}, [0, 165, 0]), ('normal control', {'lines': [32449, 5413], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 6}, 'idx': 4}, [2000, 35862, 0]), ('normal control', {'lines': [-19181, 10683, -19030, -24033], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'repeating', 'months': 1}, 'idx': 5}, [0, 0, 51561])], [('regression', {'lines': [-8928, 16007, 36, 31923, -28257], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 3}, [0, 10781, 0]), ('regression', {'lines': [17999, -28473, 22583, -24360, -11962], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 3}, 'idx': 3}, [0, 0, 24213]), ('partial-repair probe', {'lines': [73], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 4}, 'idx': 3}, [24, 49, 0]), ('partial-repair probe', {'lines': [-7260, 406], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 1}, 'idx': 0}, [406, 0, 7260]), ('normal control', {'lines': [765], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 6}, 'idx': 1}, [765, 0, 0]), ('normal control', {'lines': [196, 15845, -4816, 575, 15297], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 2}, 'idx': 6}, [2000, 25097, 0]), ('normal control', {'lines': [-853, 290, -24798, 884, 175], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 4}, 'idx': 4}, [1349, 0, 25651]), ('normal control', {'lines': [32333, 548], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'once', 'months': 6}, 'idx': 4}, [0, 32881, 0])], [('regression', {'lines': [17999, -28473, 22583, -24360, -11962], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 3}, 'idx': 3}, [0, 0, 24213]), ('regression', {'lines': [-25864, 995, 19540], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 6}, 'idx': 6}, [0, 0, 5329]), ('partial-repair probe', {'lines': [428, 16839], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [8634, 8633, 0]), ('partial-repair probe', {'lines': [39079, -13913, -17944, 185], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [19633, 0, 12226]), ('normal control', {'lines': [-22069, 164, -11294], 'coupon': {'type': 'amount', 'value': 58439, 'duration': 'forever', 'months': 2}, 'idx': 2}, [164, 0, 33363]), ('normal control', {'lines': [-26650, 28854], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 2}, 'idx': 0}, [14427, 0, 12223]), ('normal control', {'lines': [-25035, 26303, 827, 225, -682], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 3}, 'idx': 7}, [2000, 0, 362]), ('normal control', {'lines': [76, 28807], 'coupon': {'type': 'amount', 'value': 12832, 'duration': 'once', 'months': 3}, 'idx': 7}, [0, 28883, 0])], [('regression', {'lines': [-25864, 995, 19540], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 6}, 'idx': 6}, [0, 0, 5329]), ('regression', {'lines': [817, 2411], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 6}, 'idx': 6}, [0, 3228, 0]), ('partial-repair probe', {'lines': [223, 539], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'repeating', 'months': 6}, 'idx': 5}, [762, 0, 0]), ('partial-repair probe', {'lines': [75, 48724, 35794, -7691, 47121], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 6}, 'idx': 5}, [131714, 0, 7691]), ('normal control', {'lines': [48674], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 4}, 'idx': 6}, [0, 48674, 0]), ('normal control', {'lines': [68], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 2}, 'idx': 2}, [0, 68, 0]), ('normal control', {'lines': [41248], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'forever', 'months': 3}, 'idx': 3}, [4125, 37123, 0]), ('normal control', {'lines': [-27355, 25927], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 5}, 'idx': 7}, [10000, 0, 11428])], [('regression', {'lines': [817, 2411], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 6}, 'idx': 6}, [0, 3228, 0]), ('regression', {'lines': [43771, -19469], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 2}, 'idx': 2}, [0, 24302, 0]), ('partial-repair probe', {'lines': [-19858, 37242], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'repeating', 'months': 1}, 'idx': 0}, [500, 16884, 0]), ('partial-repair probe', {'lines': [46847, 18213, 870, 31525], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [32160, 65295, 0]), ('normal control', {'lines': [6838, 478, -23009], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 15693]), ('normal control', {'lines': [30400, 75, 28300, 48], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 3}, 'idx': 6}, [10000, 48823, 0]), ('normal control', {'lines': [-4637], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 2}, 'idx': 7}, [0, 0, 4637]), ('normal control', {'lines': [-17023, -29453, -12819, -17510], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 3}, 'idx': 2}, [0, 0, 76805])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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):
    c = x['coupon']
    active = c['duration'] == 'forever' or (c['duration'] == 'once' and x['idx'] == 0) or (c['duration'] == 'repeating' and x['idx'] < c['months'] - 1)
    pos = [a for a in x['lines'] if a > 0]
    discount = 0
    if active:
        if c['type'] == 'percent':
            discount = sum((a * c['value'] * 2 + 100) // 200 for a in pos)
        else:
            discount = min(c['value'], sum(pos))
    total = sum(x['lines']) - discount
    return [discount, max(0, total), max(0, -total)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'lines': [791], 'coupon': {'type': 'percent', 'value': 15, 'duration': 'repeating', 'months': 1}, 'idx': 1}, [0, 791, 0]), ('regression', {'lines': [-8928, 16007, 36, 31923, -28257], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 3}, [0, 10781, 0]), ('partial-repair probe', {'lines': [27067, -9430, 18496, 15243, 566], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 4}, 'idx': 3}, [20253, 31689, 0]), ('partial-repair probe', {'lines': [463, 18993, 591, 25859, 508], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [500, 45914, 0]), ('normal control', {'lines': [-9318], 'coupon': {'type': 'amount', 'value': 29602, 'duration': 'forever', 'months': 3}, 'idx': 7}, [0, 0, 9318]), ('normal control', {'lines': [165], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'once', 'months': 3}, 'idx': 4}, [0, 165, 0]), ('normal control', {'lines': [32449, 5413], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 6}, 'idx': 4}, [2000, 35862, 0]), ('normal control', {'lines': [-19181, 10683, -19030, -24033], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'repeating', 'months': 1}, 'idx': 5}, [0, 0, 51561])], [('regression', {'lines': [-8928, 16007, 36, 31923, -28257], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 3}, [0, 10781, 0]), ('regression', {'lines': [17999, -28473, 22583, -24360, -11962], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 3}, 'idx': 3}, [0, 0, 24213]), ('partial-repair probe', {'lines': [73], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 4}, 'idx': 3}, [24, 49, 0]), ('partial-repair probe', {'lines': [-7260, 406], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 1}, 'idx': 0}, [406, 0, 7260]), ('normal control', {'lines': [765], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 6}, 'idx': 1}, [765, 0, 0]), ('normal control', {'lines': [196, 15845, -4816, 575, 15297], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 2}, 'idx': 6}, [2000, 25097, 0]), ('normal control', {'lines': [-853, 290, -24798, 884, 175], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 4}, 'idx': 4}, [1349, 0, 25651]), ('normal control', {'lines': [32333, 548], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'once', 'months': 6}, 'idx': 4}, [0, 32881, 0])], [('regression', {'lines': [17999, -28473, 22583, -24360, -11962], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 3}, 'idx': 3}, [0, 0, 24213]), ('regression', {'lines': [-25864, 995, 19540], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 6}, 'idx': 6}, [0, 0, 5329]), ('partial-repair probe', {'lines': [428, 16839], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [8634, 8633, 0]), ('partial-repair probe', {'lines': [39079, -13913, -17944, 185], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [19633, 0, 12226]), ('normal control', {'lines': [-22069, 164, -11294], 'coupon': {'type': 'amount', 'value': 58439, 'duration': 'forever', 'months': 2}, 'idx': 2}, [164, 0, 33363]), ('normal control', {'lines': [-26650, 28854], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 2}, 'idx': 0}, [14427, 0, 12223]), ('normal control', {'lines': [-25035, 26303, 827, 225, -682], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 3}, 'idx': 7}, [2000, 0, 362]), ('normal control', {'lines': [76, 28807], 'coupon': {'type': 'amount', 'value': 12832, 'duration': 'once', 'months': 3}, 'idx': 7}, [0, 28883, 0])], [('regression', {'lines': [-25864, 995, 19540], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 6}, 'idx': 6}, [0, 0, 5329]), ('regression', {'lines': [817, 2411], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 6}, 'idx': 6}, [0, 3228, 0]), ('partial-repair probe', {'lines': [223, 539], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'repeating', 'months': 6}, 'idx': 5}, [762, 0, 0]), ('partial-repair probe', {'lines': [75, 48724, 35794, -7691, 47121], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 6}, 'idx': 5}, [131714, 0, 7691]), ('normal control', {'lines': [48674], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 4}, 'idx': 6}, [0, 48674, 0]), ('normal control', {'lines': [68], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 2}, 'idx': 2}, [0, 68, 0]), ('normal control', {'lines': [41248], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'forever', 'months': 3}, 'idx': 3}, [4125, 37123, 0]), ('normal control', {'lines': [-27355, 25927], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 5}, 'idx': 7}, [10000, 0, 11428])], [('regression', {'lines': [817, 2411], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 6}, 'idx': 6}, [0, 3228, 0]), ('regression', {'lines': [43771, -19469], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 2}, 'idx': 2}, [0, 24302, 0]), ('partial-repair probe', {'lines': [-19858, 37242], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'repeating', 'months': 1}, 'idx': 0}, [500, 16884, 0]), ('partial-repair probe', {'lines': [46847, 18213, 870, 31525], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [32160, 65295, 0]), ('normal control', {'lines': [6838, 478, -23009], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 15693]), ('normal control', {'lines': [30400, 75, 28300, 48], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 3}, 'idx': 6}, [10000, 48823, 0]), ('normal control', {'lines': [-4637], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 2}, 'idx': 7}, [0, 0, 4637]), ('normal control', {'lines': [-17023, -29453, -12819, -17510], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 3}, 'idx': 2}, [0, 0, 76805])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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 three-month coupon discounts a fourth invoice.

Input {lines: signed cents, coupon {type percent|amount, value, duration once|repeating|forever, months}, idx (0-based invoice number since redemption)}. Coupon active: forever always; once only at idx 0; repeating while idx < months. Percent coupons discount each positive line separately (half-up); credit lines are not discounted. Amount coupons subtract value capped at the sum of positive lines. Total = sum(lines) - discount. Return [discount, amount_due = max(0,total), credit = max(0,-total)].

split: open-access


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

N = 1
observations = []
def solve(x):
    c = x['coupon']
    active = c['duration'] == 'forever' or (c['duration'] == 'once' and x['idx'] == 0) or (c['duration'] == 'repeating' and x['idx'] < c['months'])
    pos = [a for a in x['lines'] if a > 0]
    discount = 0
    if active:
        if c['type'] == 'percent':
            discount = sum((a * c['value'] * 2 + 100) // 200 for a in x['lines'])
        else:
            discount = min(c['value'], sum(pos))
    total = sum(x['lines']) - discount
    return [discount, max(0, total), max(0, -total)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'lines': [34418, -28452, 122, -11117, 20609], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'forever', 'months': 6}, 'idx': 3}, [13788, 1792, 0]), ('regression', {'lines': [27067, -9430, 18496, 15243, 566], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 4}, 'idx': 3}, [20253, 31689, 0]), ('partial-repair probe', {'lines': [-18478, 325, 327, 961], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 4}, 'idx': 3}, [808, 0, 17673]), ('partial-repair probe', {'lines': [604, -21326, 425, 559], 'coupon': {'type': 'percent', 'value': 15, 'duration': 'forever', 'months': 5}, 'idx': 0}, [239, 0, 19977]), ('normal control', {'lines': [-9318], 'coupon': {'type': 'amount', 'value': 29602, 'duration': 'forever', 'months': 3}, 'idx': 7}, [0, 0, 9318]), ('normal control', {'lines': [165], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'once', 'months': 3}, 'idx': 4}, [0, 165, 0]), ('normal control', {'lines': [32449, 5413], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 6}, 'idx': 4}, [2000, 35862, 0]), ('normal control', {'lines': [-19181, 10683, -19030, -24033], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'repeating', 'months': 1}, 'idx': 5}, [0, 0, 51561])], [('regression', {'lines': [-18478, 325, 327, 961], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 4}, 'idx': 3}, [808, 0, 17673]), ('regression', {'lines': [-26650, 28854], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 2}, 'idx': 0}, [14427, 0, 12223]), ('partial-repair probe', {'lines': [604, -21326, 425, 559], 'coupon': {'type': 'percent', 'value': 15, 'duration': 'forever', 'months': 5}, 'idx': 0}, [239, 0, 19977]), ('partial-repair probe', {'lines': [177, 18447, 17030, 6135], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'once', 'months': 3}, 'idx': 0}, [4180, 37609, 0]), ('normal control', {'lines': [765], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 6}, 'idx': 1}, [765, 0, 0]), ('normal control', {'lines': [196, 15845, -4816, 575, 15297], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 2}, 'idx': 6}, [2000, 25097, 0]), ('normal control', {'lines': [-853, 290, -24798, 884, 175], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 4}, 'idx': 4}, [1349, 0, 25651]), ('normal control', {'lines': [32333, 548], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'once', 'months': 6}, 'idx': 4}, [0, 32881, 0])], [('regression', {'lines': [604, -21326, 425, 559], 'coupon': {'type': 'percent', 'value': 15, 'duration': 'forever', 'months': 5}, 'idx': 0}, [239, 0, 19977]), ('regression', {'lines': [826, -6739], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'forever', 'months': 2}, 'idx': 4}, [826, 0, 6739]), ('partial-repair probe', {'lines': [19775, 995, 34, 565, 7362], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'forever', 'months': 2}, 'idx': 1}, [7184, 21547, 0]), ('partial-repair probe', {'lines': [179, 483, 38004], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'forever', 'months': 3}, 'idx': 4}, [12759, 25907, 0]), ('normal control', {'lines': [-22069, 164, -11294], 'coupon': {'type': 'amount', 'value': 58439, 'duration': 'forever', 'months': 2}, 'idx': 2}, [164, 0, 33363]), ('normal control', {'lines': [-25035, 26303, 827, 225, -682], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 3}, 'idx': 7}, [2000, 0, 362]), ('normal control', {'lines': [76, 28807], 'coupon': {'type': 'amount', 'value': 12832, 'duration': 'once', 'months': 3}, 'idx': 7}, [0, 28883, 0]), ('normal control', {'lines': [48674], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 4}, 'idx': 6}, [0, 48674, 0])], [('regression', {'lines': [16954, 995, 903, -9733, -17174], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'forever', 'months': 5}, 'idx': 1}, [4714, 0, 12769]), ('regression', {'lines': [712, 400, -16030, -16976, 34842], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'once', 'months': 1}, 'idx': 0}, [8989, 0, 6041]), ('partial-repair probe', {'lines': [329, 678, 5814, 715], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 3}, 'idx': 1}, [3769, 3767, 0]), ('partial-repair probe', {'lines': [5166, 44509, 203, 8957, -22438], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 1}, [29419, 6978, 0]), ('normal control', {'lines': [68], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 2}, 'idx': 2}, [0, 68, 0]), ('normal control', {'lines': [41248], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'forever', 'months': 3}, 'idx': 3}, [4125, 37123, 0]), ('normal control', {'lines': [-27355, 25927], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 5}, 'idx': 7}, [10000, 0, 11428]), ('normal control', {'lines': [6838, 478, -23009], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 15693])], [('regression', {'lines': [5166, 44509, 203, 8957, -22438], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 1}, [29419, 6978, 0]), ('regression', {'lines': [969, -9502, 954, -11218, 2611], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'forever', 'months': 1}, 'idx': 4}, [1134, 0, 17320]), ('partial-repair probe', {'lines': [44567, 48078], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 4}, 'idx': 2}, [23162, 69483, 0]), ('partial-repair probe', {'lines': [39079, -13913, -17944, 185], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [19633, 0, 12226]), ('normal control', {'lines': [463, 18993, 591, 25859, 508], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [500, 45914, 0]), ('normal control', {'lines': [30400, 75, 28300, 48], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 3}, 'idx': 6}, [10000, 48823, 0]), ('normal control', {'lines': [-4637], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 2}, 'idx': 7}, [0, 0, 4637]), ('normal control', {'lines': [-17023, -29453, -12819, -17510], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 3}, 'idx': 2}, [0, 0, 76805])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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):
    c = x['coupon']
    active = c['duration'] == 'forever' or (c['duration'] == 'once' and x['idx'] == 0) or (c['duration'] == 'repeating' and x['idx'] < c['months'])
    pos = [a for a in x['lines'] if a > 0]
    discount = 0
    if active:
        if c['type'] == 'percent':
            discount = (sum(pos) * c['value'] * 2 + 100) // 200
        else:
            discount = min(c['value'], sum(pos))
    total = sum(x['lines']) - discount
    return [discount, max(0, total), max(0, -total)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'lines': [34418, -28452, 122, -11117, 20609], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'forever', 'months': 6}, 'idx': 3}, [13788, 1792, 0]), ('regression', {'lines': [27067, -9430, 18496, 15243, 566], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 4}, 'idx': 3}, [20253, 31689, 0]), ('partial-repair probe', {'lines': [-18478, 325, 327, 961], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 4}, 'idx': 3}, [808, 0, 17673]), ('partial-repair probe', {'lines': [604, -21326, 425, 559], 'coupon': {'type': 'percent', 'value': 15, 'duration': 'forever', 'months': 5}, 'idx': 0}, [239, 0, 19977]), ('normal control', {'lines': [-9318], 'coupon': {'type': 'amount', 'value': 29602, 'duration': 'forever', 'months': 3}, 'idx': 7}, [0, 0, 9318]), ('normal control', {'lines': [165], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'once', 'months': 3}, 'idx': 4}, [0, 165, 0]), ('normal control', {'lines': [32449, 5413], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 6}, 'idx': 4}, [2000, 35862, 0]), ('normal control', {'lines': [-19181, 10683, -19030, -24033], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'repeating', 'months': 1}, 'idx': 5}, [0, 0, 51561])], [('regression', {'lines': [-18478, 325, 327, 961], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 4}, 'idx': 3}, [808, 0, 17673]), ('regression', {'lines': [-26650, 28854], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 2}, 'idx': 0}, [14427, 0, 12223]), ('partial-repair probe', {'lines': [604, -21326, 425, 559], 'coupon': {'type': 'percent', 'value': 15, 'duration': 'forever', 'months': 5}, 'idx': 0}, [239, 0, 19977]), ('partial-repair probe', {'lines': [177, 18447, 17030, 6135], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'once', 'months': 3}, 'idx': 0}, [4180, 37609, 0]), ('normal control', {'lines': [765], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 6}, 'idx': 1}, [765, 0, 0]), ('normal control', {'lines': [196, 15845, -4816, 575, 15297], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 2}, 'idx': 6}, [2000, 25097, 0]), ('normal control', {'lines': [-853, 290, -24798, 884, 175], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 4}, 'idx': 4}, [1349, 0, 25651]), ('normal control', {'lines': [32333, 548], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'once', 'months': 6}, 'idx': 4}, [0, 32881, 0])], [('regression', {'lines': [604, -21326, 425, 559], 'coupon': {'type': 'percent', 'value': 15, 'duration': 'forever', 'months': 5}, 'idx': 0}, [239, 0, 19977]), ('regression', {'lines': [826, -6739], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'forever', 'months': 2}, 'idx': 4}, [826, 0, 6739]), ('partial-repair probe', {'lines': [19775, 995, 34, 565, 7362], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'forever', 'months': 2}, 'idx': 1}, [7184, 21547, 0]), ('partial-repair probe', {'lines': [179, 483, 38004], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'forever', 'months': 3}, 'idx': 4}, [12759, 25907, 0]), ('normal control', {'lines': [-22069, 164, -11294], 'coupon': {'type': 'amount', 'value': 58439, 'duration': 'forever', 'months': 2}, 'idx': 2}, [164, 0, 33363]), ('normal control', {'lines': [-25035, 26303, 827, 225, -682], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 3}, 'idx': 7}, [2000, 0, 362]), ('normal control', {'lines': [76, 28807], 'coupon': {'type': 'amount', 'value': 12832, 'duration': 'once', 'months': 3}, 'idx': 7}, [0, 28883, 0]), ('normal control', {'lines': [48674], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 4}, 'idx': 6}, [0, 48674, 0])], [('regression', {'lines': [16954, 995, 903, -9733, -17174], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'forever', 'months': 5}, 'idx': 1}, [4714, 0, 12769]), ('regression', {'lines': [712, 400, -16030, -16976, 34842], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'once', 'months': 1}, 'idx': 0}, [8989, 0, 6041]), ('partial-repair probe', {'lines': [329, 678, 5814, 715], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 3}, 'idx': 1}, [3769, 3767, 0]), ('partial-repair probe', {'lines': [5166, 44509, 203, 8957, -22438], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 1}, [29419, 6978, 0]), ('normal control', {'lines': [68], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 2}, 'idx': 2}, [0, 68, 0]), ('normal control', {'lines': [41248], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'forever', 'months': 3}, 'idx': 3}, [4125, 37123, 0]), ('normal control', {'lines': [-27355, 25927], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 5}, 'idx': 7}, [10000, 0, 11428]), ('normal control', {'lines': [6838, 478, -23009], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 15693])], [('regression', {'lines': [5166, 44509, 203, 8957, -22438], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 1}, [29419, 6978, 0]), ('regression', {'lines': [969, -9502, 954, -11218, 2611], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'forever', 'months': 1}, 'idx': 4}, [1134, 0, 17320]), ('partial-repair probe', {'lines': [44567, 48078], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 4}, 'idx': 2}, [23162, 69483, 0]), ('partial-repair probe', {'lines': [39079, -13913, -17944, 185], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [19633, 0, 12226]), ('normal control', {'lines': [463, 18993, 591, 25859, 508], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [500, 45914, 0]), ('normal control', {'lines': [30400, 75, 28300, 48], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 3}, 'idx': 6}, [10000, 48823, 0]), ('normal control', {'lines': [-4637], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 2}, 'idx': 7}, [0, 0, 4637]), ('normal control', {'lines': [-17023, -29453, -12819, -17510], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 3}, 'idx': 2}, [0, 0, 76805])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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:
Invoices with an unused-time credit line receive a smaller percentage discount than their charges warrant.

Input {lines: signed cents, coupon {type percent|amount, value, duration once|repeating|forever, months}, idx (0-based invoice number since redemption)}. Coupon active: forever always; once only at idx 0; repeating while idx < months. Percent coupons discount each positive line separately (half-up); credit lines are not discounted. Amount coupons subtract value capped at the sum of positive lines. Total = sum(lines) - discount. Return [discount, amount_due = max(0,total), credit = max(0,-total)].

split: open-access


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

N = 1
observations = []
def solve(x):
    c = x['coupon']
    active = c['duration'] == 'forever' or (c['duration'] == 'once' and x['idx'] == 0) or (c['duration'] == 'repeating' and x['idx'] < c['months'])
    pos = [a for a in x['lines'] if a > 0]
    discount = 0
    if active:
        if c['type'] == 'percent':
            discount = sum((a * c['value'] * 2 + 100) // 200 for a in pos)
        else:
            discount = c['value']
    total = sum(x['lines']) - discount
    return [discount, max(0, total), max(0, -total)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'lines': [-9318], 'coupon': {'type': 'amount', 'value': 29602, 'duration': 'forever', 'months': 3}, 'idx': 7}, [0, 0, 9318]), ('regression', {'lines': [-853, 290, -24798, 884, 175], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 4}, 'idx': 4}, [1349, 0, 25651]), ('partial-repair probe', {'lines': [-22069, 164, -11294], 'coupon': {'type': 'amount', 'value': 58439, 'duration': 'forever', 'months': 2}, 'idx': 2}, [164, 0, 33363]), ('partial-repair probe', {'lines': [-25035, 26303, 827, 225, -682], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 3}, 'idx': 7}, [2000, 0, 362]), ('normal control', {'lines': [165], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'once', 'months': 3}, 'idx': 4}, [0, 165, 0]), ('normal control', {'lines': [32449, 5413], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 6}, 'idx': 4}, [2000, 35862, 0]), ('normal control', {'lines': [-19181, 10683, -19030, -24033], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'repeating', 'months': 1}, 'idx': 5}, [0, 0, 51561]), ('normal control', {'lines': [765], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 6}, 'idx': 1}, [765, 0, 0])], [('regression', {'lines': [-853, 290, -24798, 884, 175], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 4}, 'idx': 4}, [1349, 0, 25651]), ('regression', {'lines': [-22069, 164, -11294], 'coupon': {'type': 'amount', 'value': 58439, 'duration': 'forever', 'months': 2}, 'idx': 2}, [164, 0, 33363]), ('partial-repair probe', {'lines': [-25035, 26303, 827, 225, -682], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 3}, 'idx': 7}, [2000, 0, 362]), ('partial-repair probe', {'lines': [-27355, 25927], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 5}, 'idx': 7}, [10000, 0, 11428]), ('normal control', {'lines': [196, 15845, -4816, 575, 15297], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 2}, 'idx': 6}, [2000, 25097, 0]), ('normal control', {'lines': [27067, -9430, 18496, 15243, 566], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 4}, 'idx': 3}, [20253, 31689, 0]), ('normal control', {'lines': [32333, 548], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'once', 'months': 6}, 'idx': 4}, [0, 32881, 0]), ('normal control', {'lines': [-26650, 28854], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 2}, 'idx': 0}, [14427, 0, 12223])], [('regression', {'lines': [-22069, 164, -11294], 'coupon': {'type': 'amount', 'value': 58439, 'duration': 'forever', 'months': 2}, 'idx': 2}, [164, 0, 33363]), ('regression', {'lines': [18582], 'coupon': {'type': 'amount', 'value': 57905, 'duration': 'forever', 'months': 2}, 'idx': 7}, [18582, 0, 0]), ('partial-repair probe', {'lines': [-27355, 25927], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 5}, 'idx': 7}, [10000, 0, 11428]), ('partial-repair probe', {'lines': [31, -23012], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'forever', 'months': 1}, 'idx': 4}, [31, 0, 23012]), ('normal control', {'lines': [76, 28807], 'coupon': {'type': 'amount', 'value': 12832, 'duration': 'once', 'months': 3}, 'idx': 7}, [0, 28883, 0]), ('normal control', {'lines': [48674], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 4}, 'idx': 6}, [0, 48674, 0]), ('normal control', {'lines': [68], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 2}, 'idx': 2}, [0, 68, 0]), ('normal control', {'lines': [41248], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'forever', 'months': 3}, 'idx': 3}, [4125, 37123, 0])], [('regression', {'lines': [31, -23012], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'forever', 'months': 1}, 'idx': 4}, [31, 0, 23012]), ('regression', {'lines': [18582], 'coupon': {'type': 'amount', 'value': 57905, 'duration': 'forever', 'months': 2}, 'idx': 7}, [18582, 0, 0]), ('partial-repair probe', {'lines': [-14691], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 4}, 'idx': 2}, [0, 0, 14691]), ('partial-repair probe', {'lines': [-29340], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 1}, 'idx': 6}, [0, 0, 29340]), ('normal control', {'lines': [6838, 478, -23009], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 15693]), ('normal control', {'lines': [463, 18993, 591, 25859, 508], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [500, 45914, 0]), ('normal control', {'lines': [30400, 75, 28300, 48], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 3}, 'idx': 6}, [10000, 48823, 0]), ('normal control', {'lines': [-4637], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 2}, 'idx': 7}, [0, 0, 4637])], [('regression', {'lines': [-14691], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 4}, 'idx': 2}, [0, 0, 14691]), ('regression', {'lines': [31, -23012], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'forever', 'months': 1}, 'idx': 4}, [31, 0, 23012]), ('partial-repair probe', {'lines': [-28388], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'forever', 'months': 1}, 'idx': 2}, [0, 0, 28388]), ('partial-repair probe', {'lines': [-13444, 920, -14508, 27899], 'coupon': {'type': 'amount', 'value': 44850, 'duration': 'repeating', 'months': 5}, 'idx': 2}, [28819, 0, 27952]), ('normal control', {'lines': [-17023, -29453, -12819, -17510], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 3}, 'idx': 2}, [0, 0, 76805]), ('normal control', {'lines': [593, 9, 782, 21111], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'forever', 'months': 6}, 'idx': 2}, [500, 21995, 0]), ('normal control', {'lines': [826, -6739], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'forever', 'months': 2}, 'idx': 4}, [826, 0, 6739]), ('normal control', {'lines': [478], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'once', 'months': 4}, 'idx': 4}, [0, 478, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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):
    c = x['coupon']
    active = c['duration'] == 'forever' or (c['duration'] == 'once' and x['idx'] == 0) or (c['duration'] == 'repeating' and x['idx'] < c['months'])
    pos = [a for a in x['lines'] if a > 0]
    discount = 0
    if active:
        if c['type'] == 'percent':
            discount = sum((a * c['value'] * 2 + 100) // 200 for a in pos)
        else:
            discount = min(c['value'], sum(x['lines']))
    total = sum(x['lines']) - discount
    return [discount, max(0, total), max(0, -total)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'lines': [-9318], 'coupon': {'type': 'amount', 'value': 29602, 'duration': 'forever', 'months': 3}, 'idx': 7}, [0, 0, 9318]), ('regression', {'lines': [-853, 290, -24798, 884, 175], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 4}, 'idx': 4}, [1349, 0, 25651]), ('partial-repair probe', {'lines': [-22069, 164, -11294], 'coupon': {'type': 'amount', 'value': 58439, 'duration': 'forever', 'months': 2}, 'idx': 2}, [164, 0, 33363]), ('partial-repair probe', {'lines': [-25035, 26303, 827, 225, -682], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 3}, 'idx': 7}, [2000, 0, 362]), ('normal control', {'lines': [165], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'once', 'months': 3}, 'idx': 4}, [0, 165, 0]), ('normal control', {'lines': [32449, 5413], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 6}, 'idx': 4}, [2000, 35862, 0]), ('normal control', {'lines': [-19181, 10683, -19030, -24033], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'repeating', 'months': 1}, 'idx': 5}, [0, 0, 51561]), ('normal control', {'lines': [765], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 6}, 'idx': 1}, [765, 0, 0])], [('regression', {'lines': [-853, 290, -24798, 884, 175], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 4}, 'idx': 4}, [1349, 0, 25651]), ('regression', {'lines': [-22069, 164, -11294], 'coupon': {'type': 'amount', 'value': 58439, 'duration': 'forever', 'months': 2}, 'idx': 2}, [164, 0, 33363]), ('partial-repair probe', {'lines': [-25035, 26303, 827, 225, -682], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 3}, 'idx': 7}, [2000, 0, 362]), ('partial-repair probe', {'lines': [-27355, 25927], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 5}, 'idx': 7}, [10000, 0, 11428]), ('normal control', {'lines': [196, 15845, -4816, 575, 15297], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 2}, 'idx': 6}, [2000, 25097, 0]), ('normal control', {'lines': [27067, -9430, 18496, 15243, 566], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 4}, 'idx': 3}, [20253, 31689, 0]), ('normal control', {'lines': [32333, 548], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'once', 'months': 6}, 'idx': 4}, [0, 32881, 0]), ('normal control', {'lines': [-26650, 28854], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 2}, 'idx': 0}, [14427, 0, 12223])], [('regression', {'lines': [-22069, 164, -11294], 'coupon': {'type': 'amount', 'value': 58439, 'duration': 'forever', 'months': 2}, 'idx': 2}, [164, 0, 33363]), ('regression', {'lines': [18582], 'coupon': {'type': 'amount', 'value': 57905, 'duration': 'forever', 'months': 2}, 'idx': 7}, [18582, 0, 0]), ('partial-repair probe', {'lines': [-27355, 25927], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 5}, 'idx': 7}, [10000, 0, 11428]), ('partial-repair probe', {'lines': [31, -23012], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'forever', 'months': 1}, 'idx': 4}, [31, 0, 23012]), ('normal control', {'lines': [76, 28807], 'coupon': {'type': 'amount', 'value': 12832, 'duration': 'once', 'months': 3}, 'idx': 7}, [0, 28883, 0]), ('normal control', {'lines': [48674], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 4}, 'idx': 6}, [0, 48674, 0]), ('normal control', {'lines': [68], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 2}, 'idx': 2}, [0, 68, 0]), ('normal control', {'lines': [41248], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'forever', 'months': 3}, 'idx': 3}, [4125, 37123, 0])], [('regression', {'lines': [31, -23012], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'forever', 'months': 1}, 'idx': 4}, [31, 0, 23012]), ('regression', {'lines': [18582], 'coupon': {'type': 'amount', 'value': 57905, 'duration': 'forever', 'months': 2}, 'idx': 7}, [18582, 0, 0]), ('partial-repair probe', {'lines': [-14691], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 4}, 'idx': 2}, [0, 0, 14691]), ('partial-repair probe', {'lines': [-29340], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 1}, 'idx': 6}, [0, 0, 29340]), ('normal control', {'lines': [6838, 478, -23009], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 15693]), ('normal control', {'lines': [463, 18993, 591, 25859, 508], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [500, 45914, 0]), ('normal control', {'lines': [30400, 75, 28300, 48], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 3}, 'idx': 6}, [10000, 48823, 0]), ('normal control', {'lines': [-4637], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 2}, 'idx': 7}, [0, 0, 4637])], [('regression', {'lines': [-14691], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 4}, 'idx': 2}, [0, 0, 14691]), ('regression', {'lines': [31, -23012], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'forever', 'months': 1}, 'idx': 4}, [31, 0, 23012]), ('partial-repair probe', {'lines': [-28388], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'forever', 'months': 1}, 'idx': 2}, [0, 0, 28388]), ('partial-repair probe', {'lines': [-13444, 920, -14508, 27899], 'coupon': {'type': 'amount', 'value': 44850, 'duration': 'repeating', 'months': 5}, 'idx': 2}, [28819, 0, 27952]), ('normal control', {'lines': [-17023, -29453, -12819, -17510], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 3}, 'idx': 2}, [0, 0, 76805]), ('normal control', {'lines': [593, 9, 782, 21111], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'forever', 'months': 6}, 'idx': 2}, [500, 21995, 0]), ('normal control', {'lines': [826, -6739], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'forever', 'months': 2}, 'idx': 4}, [826, 0, 6739]), ('normal control', {'lines': [478], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'once', 'months': 4}, 'idx': 4}, [0, 478, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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:
Small invoices with a large amount-off coupon produce a credit balance out of the coupon.

Input {lines: signed cents, coupon {type percent|amount, value, duration once|repeating|forever, months}, idx (0-based invoice number since redemption)}. Coupon active: forever always; once only at idx 0; repeating while idx < months. Percent coupons discount each positive line separately (half-up); credit lines are not discounted. Amount coupons subtract value capped at the sum of positive lines. Total = sum(lines) - discount. Return [discount, amount_due = max(0,total), credit = max(0,-total)].

split: open-access


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

N = 1
observations = []
def solve(x):
    c = x['coupon']
    active = c['duration'] == 'forever' or c['duration'] == 'once' or (c['duration'] == 'repeating' and x['idx'] < c['months'])
    pos = [a for a in x['lines'] if a > 0]
    discount = 0
    if active:
        if c['type'] == 'percent':
            discount = sum((a * c['value'] * 2 + 100) // 200 for a in pos)
        else:
            discount = min(c['value'], sum(pos))
    total = sum(x['lines']) - discount
    return [discount, max(0, total), max(0, -total)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'lines': [6838, 478, -23009], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 15693]), ('regression', {'lines': [165], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'once', 'months': 3}, 'idx': 4}, [0, 165, 0]), ('partial-repair probe', {'lines': [78, 40473], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 40551, 0]), ('partial-repair probe', {'lines': [-29167, 446, 149, 750, -18108], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 45930]), ('normal control', {'lines': [-9318], 'coupon': {'type': 'amount', 'value': 29602, 'duration': 'forever', 'months': 3}, 'idx': 7}, [0, 0, 9318]), ('normal control', {'lines': [32449, 5413], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 6}, 'idx': 4}, [2000, 35862, 0]), ('normal control', {'lines': [-19181, 10683, -19030, -24033], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'repeating', 'months': 1}, 'idx': 5}, [0, 0, 51561]), ('normal control', {'lines': [765], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 6}, 'idx': 1}, [765, 0, 0])], [('regression', {'lines': [78, 40473], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 40551, 0]), ('regression', {'lines': [32333, 548], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'once', 'months': 6}, 'idx': 4}, [0, 32881, 0]), ('partial-repair probe', {'lines': [-29167, 446, 149, 750, -18108], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 45930]), ('partial-repair probe', {'lines': [-10316, 373, 582], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 9361]), ('normal control', {'lines': [196, 15845, -4816, 575, 15297], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 2}, 'idx': 6}, [2000, 25097, 0]), ('normal control', {'lines': [27067, -9430, 18496, 15243, 566], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 4}, 'idx': 3}, [20253, 31689, 0]), ('normal control', {'lines': [-853, 290, -24798, 884, 175], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 4}, 'idx': 4}, [1349, 0, 25651]), ('normal control', {'lines': [-22069, 164, -11294], 'coupon': {'type': 'amount', 'value': 58439, 'duration': 'forever', 'months': 2}, 'idx': 2}, [164, 0, 33363])], [('regression', {'lines': [-29167, 446, 149, 750, -18108], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 45930]), ('regression', {'lines': [76, 28807], 'coupon': {'type': 'amount', 'value': 12832, 'duration': 'once', 'months': 3}, 'idx': 7}, [0, 28883, 0]), ('partial-repair probe', {'lines': [24745, 579], 'coupon': {'type': 'amount', 'value': 51822, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 25324, 0]), ('partial-repair probe', {'lines': [-2033, 984], 'coupon': {'type': 'amount', 'value': 50384, 'duration': 'once', 'months': 6}, 'idx': 1}, [0, 0, 1049]), ('normal control', {'lines': [-26650, 28854], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 2}, 'idx': 0}, [14427, 0, 12223]), ('normal control', {'lines': [-25035, 26303, 827, 225, -682], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 3}, 'idx': 7}, [2000, 0, 362]), ('normal control', {'lines': [48674], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 4}, 'idx': 6}, [0, 48674, 0]), ('normal control', {'lines': [41248], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'forever', 'months': 3}, 'idx': 3}, [4125, 37123, 0])], [('regression', {'lines': [-10316, 373, 582], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 9361]), ('regression', {'lines': [68], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 2}, 'idx': 2}, [0, 68, 0]), ('partial-repair probe', {'lines': [39395, 41743], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'once', 'months': 3}, 'idx': 1}, [0, 81138, 0]), ('partial-repair probe', {'lines': [20065, 9102], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'once', 'months': 2}, 'idx': 1}, [0, 29167, 0]), ('normal control', {'lines': [-27355, 25927], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 5}, 'idx': 7}, [10000, 0, 11428]), ('normal control', {'lines': [463, 18993, 591, 25859, 508], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [500, 45914, 0]), ('normal control', {'lines': [30400, 75, 28300, 48], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 3}, 'idx': 6}, [10000, 48823, 0]), ('normal control', {'lines': [-4637], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 2}, 'idx': 7}, [0, 0, 4637])], [('regression', {'lines': [24745, 579], 'coupon': {'type': 'amount', 'value': 51822, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 25324, 0]), ('regression', {'lines': [6838, 478, -23009], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 15693]), ('partial-repair probe', {'lines': [11985, 21511, 33, -27642, 20322], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 26209, 0]), ('partial-repair probe', {'lines': [868, 21034, -29569], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 7667]), ('normal control', {'lines': [-17023, -29453, -12819, -17510], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 3}, 'idx': 2}, [0, 0, 76805]), ('normal control', {'lines': [593, 9, 782, 21111], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'forever', 'months': 6}, 'idx': 2}, [500, 21995, 0]), ('normal control', {'lines': [826, -6739], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'forever', 'months': 2}, 'idx': 4}, [826, 0, 6739]), ('normal control', {'lines': [18582], 'coupon': {'type': 'amount', 'value': 57905, 'duration': 'forever', 'months': 2}, 'idx': 7}, [18582, 0, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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):
    c = x['coupon']
    active = c['duration'] == 'forever' or (c['duration'] == 'once' and x['idx'] <= 1) or (c['duration'] == 'repeating' and x['idx'] < c['months'])
    pos = [a for a in x['lines'] if a > 0]
    discount = 0
    if active:
        if c['type'] == 'percent':
            discount = sum((a * c['value'] * 2 + 100) // 200 for a in pos)
        else:
            discount = min(c['value'], sum(pos))
    total = sum(x['lines']) - discount
    return [discount, max(0, total), max(0, -total)]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'lines': [6838, 478, -23009], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 15693]), ('regression', {'lines': [165], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'once', 'months': 3}, 'idx': 4}, [0, 165, 0]), ('partial-repair probe', {'lines': [78, 40473], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 40551, 0]), ('partial-repair probe', {'lines': [-29167, 446, 149, 750, -18108], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 45930]), ('normal control', {'lines': [-9318], 'coupon': {'type': 'amount', 'value': 29602, 'duration': 'forever', 'months': 3}, 'idx': 7}, [0, 0, 9318]), ('normal control', {'lines': [32449, 5413], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'repeating', 'months': 6}, 'idx': 4}, [2000, 35862, 0]), ('normal control', {'lines': [-19181, 10683, -19030, -24033], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'repeating', 'months': 1}, 'idx': 5}, [0, 0, 51561]), ('normal control', {'lines': [765], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 6}, 'idx': 1}, [765, 0, 0])], [('regression', {'lines': [78, 40473], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 40551, 0]), ('regression', {'lines': [32333, 548], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'once', 'months': 6}, 'idx': 4}, [0, 32881, 0]), ('partial-repair probe', {'lines': [-29167, 446, 149, 750, -18108], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 45930]), ('partial-repair probe', {'lines': [-10316, 373, 582], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 9361]), ('normal control', {'lines': [196, 15845, -4816, 575, 15297], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 2}, 'idx': 6}, [2000, 25097, 0]), ('normal control', {'lines': [27067, -9430, 18496, 15243, 566], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'repeating', 'months': 4}, 'idx': 3}, [20253, 31689, 0]), ('normal control', {'lines': [-853, 290, -24798, 884, 175], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 4}, 'idx': 4}, [1349, 0, 25651]), ('normal control', {'lines': [-22069, 164, -11294], 'coupon': {'type': 'amount', 'value': 58439, 'duration': 'forever', 'months': 2}, 'idx': 2}, [164, 0, 33363])], [('regression', {'lines': [-29167, 446, 149, 750, -18108], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 45930]), ('regression', {'lines': [76, 28807], 'coupon': {'type': 'amount', 'value': 12832, 'duration': 'once', 'months': 3}, 'idx': 7}, [0, 28883, 0]), ('partial-repair probe', {'lines': [24745, 579], 'coupon': {'type': 'amount', 'value': 51822, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 25324, 0]), ('partial-repair probe', {'lines': [-2033, 984], 'coupon': {'type': 'amount', 'value': 50384, 'duration': 'once', 'months': 6}, 'idx': 1}, [0, 0, 1049]), ('normal control', {'lines': [-26650, 28854], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'forever', 'months': 2}, 'idx': 0}, [14427, 0, 12223]), ('normal control', {'lines': [-25035, 26303, 827, 225, -682], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'forever', 'months': 3}, 'idx': 7}, [2000, 0, 362]), ('normal control', {'lines': [48674], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'repeating', 'months': 4}, 'idx': 6}, [0, 48674, 0]), ('normal control', {'lines': [41248], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'forever', 'months': 3}, 'idx': 3}, [4125, 37123, 0])], [('regression', {'lines': [-10316, 373, 582], 'coupon': {'type': 'percent', 'value': 10, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 9361]), ('regression', {'lines': [68], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 2}, 'idx': 2}, [0, 68, 0]), ('partial-repair probe', {'lines': [39395, 41743], 'coupon': {'type': 'percent', 'value': 50, 'duration': 'once', 'months': 3}, 'idx': 1}, [0, 81138, 0]), ('partial-repair probe', {'lines': [20065, 9102], 'coupon': {'type': 'percent', 'value': 25, 'duration': 'once', 'months': 2}, 'idx': 1}, [0, 29167, 0]), ('normal control', {'lines': [-27355, 25927], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 5}, 'idx': 7}, [10000, 0, 11428]), ('normal control', {'lines': [463, 18993, 591, 25859, 508], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'repeating', 'months': 3}, 'idx': 2}, [500, 45914, 0]), ('normal control', {'lines': [30400, 75, 28300, 48], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'forever', 'months': 3}, 'idx': 6}, [10000, 48823, 0]), ('normal control', {'lines': [-4637], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'repeating', 'months': 2}, 'idx': 7}, [0, 0, 4637])], [('regression', {'lines': [24745, 579], 'coupon': {'type': 'amount', 'value': 51822, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 25324, 0]), ('regression', {'lines': [6838, 478, -23009], 'coupon': {'type': 'amount', 'value': 10000, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 15693]), ('partial-repair probe', {'lines': [11985, 21511, 33, -27642, 20322], 'coupon': {'type': 'percent', 'value': 33, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 26209, 0]), ('partial-repair probe', {'lines': [868, 21034, -29569], 'coupon': {'type': 'amount', 'value': 2000, 'duration': 'once', 'months': 1}, 'idx': 1}, [0, 0, 7667]), ('normal control', {'lines': [-17023, -29453, -12819, -17510], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'once', 'months': 3}, 'idx': 2}, [0, 0, 76805]), ('normal control', {'lines': [593, 9, 782, 21111], 'coupon': {'type': 'amount', 'value': 500, 'duration': 'forever', 'months': 6}, 'idx': 2}, [500, 21995, 0]), ('normal control', {'lines': [826, -6739], 'coupon': {'type': 'percent', 'value': 100, 'duration': 'forever', 'months': 2}, 'idx': 4}, [826, 0, 6739]), ('normal control', {'lines': [18582], 'coupon': {'type': 'amount', 'value': 57905, 'duration': 'forever', 'months': 2}, 'idx': 7}, [18582, 0, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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 once coupon keeps discounting every renewal.

Input {lines: signed cents, coupon {type percent|amount, value, duration once|repeating|forever, months}, idx (0-based invoice number since redemption)}. Coupon active: forever always; once only at idx 0; repeating while idx < months. Percent coupons discount each positive line separately (half-up); credit lines are not discounted. Amount coupons subtract value capped at the sum of positive lines. Total = sum(lines) - discount. Return [discount, amount_due = max(0,total), credit = max(0,-total)].

split: open-access


prompt:
Pauses that straddle the renewal extend the period by days that belong to the next period.

Input {start, end (exclusive) day numbers, pauses: [[from, to)]}. Pause intervals are clipped to the period, empty ones dropped, then overlapping ones merged. The period end moves out by the number of distinct paused days. Return [new_end, paused_days].

split: open-access


prompt:
A short pause nested inside a longer one shortens the extension.

Input {start, end (exclusive) day numbers, pauses: [[from, to)]}. Pause intervals are clipped to the period, empty ones dropped, then overlapping ones merged. The period end moves out by the number of distinct paused days. Return [new_end, paused_days].

split: open-access


prompt:
Pauses entirely outside the period shorten the subscription.

Input {start, end (exclusive) day numbers, pauses: [[from, to)]}. Pause intervals are clipped to the period, empty ones dropped, then overlapping ones merged. The period end moves out by the number of distinct paused days. Return [new_end, paused_days].

split: open-access


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

N = 1
observations = []
def solve(x):
    unused = max(0, x['period_days'] - x['used_days'])
    if x['mode'] == 'period_end':
        return [0, unused]
    charged_days = max(x['used_days'], x['min_days'])
    refundable_days = max(0, x['period_days'] - charged_days)
    refund = (x['price'] + x['setup']) * refundable_days // x['period_days']
    return [refund, 0]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'price': 4900, 'setup': 2500, 'period_days': 90, 'used_days': 1, 'min_days': 7, 'mode': 'immediate'}, [4518, 0]), ('regression', {'price': 999, 'setup': 2500, 'period_days': 30, 'used_days': 18, 'min_days': 14, 'mode': 'immediate'}, [399, 0]), ('partial-repair probe', {'price': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 0]), ('partial-repair probe', {'price': 4900, 'setup': 1208, 'period_days': 365, 'used_days': 78, 'min_days': 14, 'mode': 'immediate'}, [3852, 0]), ('normal control', {'price': 4900, 'setup': 9483, 'period_days': 31, 'used_days': 13, 'min_days': 0, 'mode': 'period_end'}, [0, 18]), ('normal control', {'price': 32440, 'setup': 0, 'period_days': 90, 'used_days': 25, 'min_days': 14, 'mode': 'period_end'}, [0, 65]), ('normal control', {'price': 28584, 'setup': 2500, 'period_days': 90, 'used_days': 93, 'min_days': 14, 'mode': 'immediate'}, [0, 0]), ('normal control', {'price': 4970, 'setup': 0, 'period_days': 90, 'used_days': 0, 'min_days': 7, 'mode': 'immediate'}, [4583, 0])], [('regression', {'price': 999, 'setup': 2500, 'period_days': 30, 'used_days': 18, 'min_days': 14, 'mode': 'immediate'}, [399, 0]), ('regression', {'price': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 0]), ('partial-repair probe', {'price': 4900, 'setup': 1208, 'period_days': 365, 'used_days': 78, 'min_days': 14, 'mode': 'immediate'}, [3852, 0]), ('partial-repair probe', {'price': 119900, 'setup': 1279, 'period_days': 30, 'used_days': 14, 'min_days': 7, 'mode': 'immediate'}, [63946, 0]), ('normal control', {'price': 119900, 'setup': 0, 'period_days': 30, 'used_days': 22, 'min_days': 14, 'mode': 'immediate'}, [31973, 0]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 66, 'min_days': 0, 'mode': 'period_end'}, [0, 24]), ('normal control', {'price': 86963, 'setup': 0, 'period_days': 90, 'used_days': 71, 'min_days': 14, 'mode': 'immediate'}, [18358, 0]), ('normal control', {'price': 999, 'setup': 0, 'period_days': 365, 'used_days': 45, 'min_days': 30, 'mode': 'period_end'}, [0, 320])], [('regression', {'price': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 0]), ('regression', {'price': 4900, 'setup': 1208, 'period_days': 365, 'used_days': 78, 'min_days': 14, 'mode': 'immediate'}, [3852, 0]), ('partial-repair probe', {'price': 119900, 'setup': 1279, 'period_days': 30, 'used_days': 14, 'min_days': 7, 'mode': 'immediate'}, [63946, 0]), ('partial-repair probe', {'price': 4900, 'setup': 2500, 'period_days': 365, 'used_days': 232, 'min_days': 0, 'mode': 'immediate'}, [1785, 0]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'period_end'}, [0, 21]), ('normal control', {'price': 999, 'setup': 0, 'period_days': 30, 'used_days': 19, 'min_days': 0, 'mode': 'immediate'}, [366, 0]), ('normal control', {'price': 119900, 'setup': 0, 'period_days': 365, 'used_days': 32, 'min_days': 0, 'mode': 'immediate'}, [109388, 0]), ('normal control', {'price': 93530, 'setup': 0, 'period_days': 30, 'used_days': 29, 'min_days': 14, 'mode': 'immediate'}, [3117, 0])], [('regression', {'price': 4900, 'setup': 1208, 'period_days': 365, 'used_days': 78, 'min_days': 14, 'mode': 'immediate'}, [3852, 0]), ('regression', {'price': 119900, 'setup': 1279, 'period_days': 30, 'used_days': 14, 'min_days': 7, 'mode': 'immediate'}, [63946, 0]), ('partial-repair probe', {'price': 119900, 'setup': 3409, 'period_days': 31, 'used_days': 27, 'min_days': 7, 'mode': 'immediate'}, [15470, 0]), ('partial-repair probe', {'price': 119900, 'setup': 2500, 'period_days': 30, 'used_days': 6, 'min_days': 14, 'mode': 'immediate'}, [63946, 0]), ('normal control', {'price': 119900, 'setup': 2500, 'period_days': 31, 'used_days': 17, 'min_days': 14, 'mode': 'period_end'}, [0, 14]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 53, 'min_days': 30, 'mode': 'immediate'}, [2014, 0]), ('normal control', {'price': 119900, 'setup': 0, 'period_days': 90, 'used_days': 22, 'min_days': 7, 'mode': 'period_end'}, [0, 68]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 55, 'min_days': 14, 'mode': 'period_end'}, [0, 35])], [('regression', {'price': 119900, 'setup': 1279, 'period_days': 30, 'used_days': 14, 'min_days': 7, 'mode': 'immediate'}, [63946, 0]), ('regression', {'price': 4900, 'setup': 2500, 'period_days': 365, 'used_days': 232, 'min_days': 0, 'mode': 'immediate'}, [1785, 0]), ('partial-repair probe', {'price': 4900, 'setup': 7, 'period_days': 90, 'used_days': 46, 'min_days': 0, 'mode': 'immediate'}, [2395, 0]), ('partial-repair probe', {'price': 24966, 'setup': 5091, 'period_days': 31, 'used_days': 26, 'min_days': 0, 'mode': 'immediate'}, [4026, 0]), ('normal control', {'price': 999, 'setup': 0, 'period_days': 90, 'used_days': 3, 'min_days': 14, 'mode': 'period_end'}, [0, 87]), ('normal control', {'price': 119900, 'setup': 5059, 'period_days': 90, 'used_days': 7, 'min_days': 0, 'mode': 'period_end'}, [0, 83]), ('normal control', {'price': 119900, 'setup': 2500, 'period_days': 365, 'used_days': 323, 'min_days': 7, 'mode': 'period_end'}, [0, 42]), ('normal control', {'price': 67850, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'immediate'}, [2188, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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):
    unused = max(0, x['period_days'] - x['used_days'])
    if x['mode'] == 'period_end':
        return [0, unused]
    charged_days = max(x['used_days'], x['min_days'])
    refundable_days = max(0, x['period_days'] - charged_days)
    refund = max(0, x['price'] * refundable_days // x['period_days'] - x['setup'])
    return [refund, 0]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'price': 4900, 'setup': 2500, 'period_days': 90, 'used_days': 1, 'min_days': 7, 'mode': 'immediate'}, [4518, 0]), ('regression', {'price': 999, 'setup': 2500, 'period_days': 30, 'used_days': 18, 'min_days': 14, 'mode': 'immediate'}, [399, 0]), ('partial-repair probe', {'price': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 0]), ('partial-repair probe', {'price': 4900, 'setup': 1208, 'period_days': 365, 'used_days': 78, 'min_days': 14, 'mode': 'immediate'}, [3852, 0]), ('normal control', {'price': 4900, 'setup': 9483, 'period_days': 31, 'used_days': 13, 'min_days': 0, 'mode': 'period_end'}, [0, 18]), ('normal control', {'price': 32440, 'setup': 0, 'period_days': 90, 'used_days': 25, 'min_days': 14, 'mode': 'period_end'}, [0, 65]), ('normal control', {'price': 28584, 'setup': 2500, 'period_days': 90, 'used_days': 93, 'min_days': 14, 'mode': 'immediate'}, [0, 0]), ('normal control', {'price': 4970, 'setup': 0, 'period_days': 90, 'used_days': 0, 'min_days': 7, 'mode': 'immediate'}, [4583, 0])], [('regression', {'price': 999, 'setup': 2500, 'period_days': 30, 'used_days': 18, 'min_days': 14, 'mode': 'immediate'}, [399, 0]), ('regression', {'price': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 0]), ('partial-repair probe', {'price': 4900, 'setup': 1208, 'period_days': 365, 'used_days': 78, 'min_days': 14, 'mode': 'immediate'}, [3852, 0]), ('partial-repair probe', {'price': 119900, 'setup': 1279, 'period_days': 30, 'used_days': 14, 'min_days': 7, 'mode': 'immediate'}, [63946, 0]), ('normal control', {'price': 119900, 'setup': 0, 'period_days': 30, 'used_days': 22, 'min_days': 14, 'mode': 'immediate'}, [31973, 0]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 66, 'min_days': 0, 'mode': 'period_end'}, [0, 24]), ('normal control', {'price': 86963, 'setup': 0, 'period_days': 90, 'used_days': 71, 'min_days': 14, 'mode': 'immediate'}, [18358, 0]), ('normal control', {'price': 999, 'setup': 0, 'period_days': 365, 'used_days': 45, 'min_days': 30, 'mode': 'period_end'}, [0, 320])], [('regression', {'price': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 0]), ('regression', {'price': 4900, 'setup': 1208, 'period_days': 365, 'used_days': 78, 'min_days': 14, 'mode': 'immediate'}, [3852, 0]), ('partial-repair probe', {'price': 119900, 'setup': 1279, 'period_days': 30, 'used_days': 14, 'min_days': 7, 'mode': 'immediate'}, [63946, 0]), ('partial-repair probe', {'price': 4900, 'setup': 2500, 'period_days': 365, 'used_days': 232, 'min_days': 0, 'mode': 'immediate'}, [1785, 0]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'period_end'}, [0, 21]), ('normal control', {'price': 999, 'setup': 0, 'period_days': 30, 'used_days': 19, 'min_days': 0, 'mode': 'immediate'}, [366, 0]), ('normal control', {'price': 119900, 'setup': 0, 'period_days': 365, 'used_days': 32, 'min_days': 0, 'mode': 'immediate'}, [109388, 0]), ('normal control', {'price': 93530, 'setup': 0, 'period_days': 30, 'used_days': 29, 'min_days': 14, 'mode': 'immediate'}, [3117, 0])], [('regression', {'price': 4900, 'setup': 1208, 'period_days': 365, 'used_days': 78, 'min_days': 14, 'mode': 'immediate'}, [3852, 0]), ('regression', {'price': 119900, 'setup': 1279, 'period_days': 30, 'used_days': 14, 'min_days': 7, 'mode': 'immediate'}, [63946, 0]), ('partial-repair probe', {'price': 119900, 'setup': 3409, 'period_days': 31, 'used_days': 27, 'min_days': 7, 'mode': 'immediate'}, [15470, 0]), ('partial-repair probe', {'price': 119900, 'setup': 2500, 'period_days': 30, 'used_days': 6, 'min_days': 14, 'mode': 'immediate'}, [63946, 0]), ('normal control', {'price': 119900, 'setup': 2500, 'period_days': 31, 'used_days': 17, 'min_days': 14, 'mode': 'period_end'}, [0, 14]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 53, 'min_days': 30, 'mode': 'immediate'}, [2014, 0]), ('normal control', {'price': 119900, 'setup': 0, 'period_days': 90, 'used_days': 22, 'min_days': 7, 'mode': 'period_end'}, [0, 68]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 55, 'min_days': 14, 'mode': 'period_end'}, [0, 35])], [('regression', {'price': 119900, 'setup': 1279, 'period_days': 30, 'used_days': 14, 'min_days': 7, 'mode': 'immediate'}, [63946, 0]), ('regression', {'price': 4900, 'setup': 2500, 'period_days': 365, 'used_days': 232, 'min_days': 0, 'mode': 'immediate'}, [1785, 0]), ('partial-repair probe', {'price': 4900, 'setup': 7, 'period_days': 90, 'used_days': 46, 'min_days': 0, 'mode': 'immediate'}, [2395, 0]), ('partial-repair probe', {'price': 24966, 'setup': 5091, 'period_days': 31, 'used_days': 26, 'min_days': 0, 'mode': 'immediate'}, [4026, 0]), ('normal control', {'price': 999, 'setup': 0, 'period_days': 90, 'used_days': 3, 'min_days': 14, 'mode': 'period_end'}, [0, 87]), ('normal control', {'price': 119900, 'setup': 5059, 'period_days': 90, 'used_days': 7, 'min_days': 0, 'mode': 'period_end'}, [0, 83]), ('normal control', {'price': 119900, 'setup': 2500, 'period_days': 365, 'used_days': 323, 'min_days': 7, 'mode': 'period_end'}, [0, 42]), ('normal control', {'price': 67850, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'immediate'}, [2188, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), 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:
Customers who cancel get part of the one-time setup fee back.

Input {price, setup, period_days, used_days, min_days, mode immediate|period_end}. Unused = max(0, period_days - used_days). period_end: no refund, access continues for the unused days. immediate: charged days = max(used_days, min_days); refund = price*max(0, period_days - charged)/period_days floored (never over-refund); the setup fee is never refunded; access ends. Return [refund, access_days].

split: open-access