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 = x['used_days']
    refundable_days = max(0, x['period_days'] - charged_days)
    refund = x['price'] * 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': 4970, 'setup': 0, 'period_days': 90, 'used_days': 0, 'min_days': 7, 'mode': 'immediate'}, [4583, 0]), ('partial-repair probe', {'price': 999, 'setup': 2500, 'period_days': 30, 'used_days': 18, 'min_days': 14, 'mode': 'immediate'}, [399, 0]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 30, 'used_days': 22, 'min_days': 14, 'mode': 'immediate'}, [31973, 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': 4900, 'setup': 0, 'period_days': 90, 'used_days': 66, 'min_days': 0, 'mode': 'period_end'}, [0, 24])], [('regression', {'price': 119900, 'setup': 2500, 'period_days': 30, 'used_days': 6, 'min_days': 14, 'mode': 'immediate'}, [63946, 0]), ('regression', {'price': 4970, 'setup': 0, 'period_days': 90, 'used_days': 0, 'min_days': 7, 'mode': 'immediate'}, [4583, 0]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 30, 'used_days': 22, 'min_days': 14, 'mode': 'immediate'}, [31973, 0]), ('partial-repair probe', {'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]), ('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])], [('regression', {'price': 4900, 'setup': 6691, 'period_days': 31, 'used_days': 19, 'min_days': 30, 'mode': 'immediate'}, [158, 0]), ('regression', {'price': 119900, 'setup': 2500, 'period_days': 30, 'used_days': 6, 'min_days': 14, 'mode': 'immediate'}, [63946, 0]), ('partial-repair probe', {'price': 93530, 'setup': 0, 'period_days': 30, 'used_days': 29, 'min_days': 14, 'mode': 'immediate'}, [3117, 0]), ('partial-repair probe', {'price': 4900, 'setup': 1208, 'period_days': 365, 'used_days': 78, 'min_days': 14, 'mode': 'immediate'}, [3852, 0]), ('normal control', {'price': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 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': 2500, 'period_days': 365, 'used_days': 232, 'min_days': 0, 'mode': 'immediate'}, [1785, 0]), ('normal control', {'price': 119900, 'setup': 0, 'period_days': 90, 'used_days': 22, 'min_days': 7, 'mode': 'period_end'}, [0, 68])], [('regression', {'price': 67850, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'immediate'}, [2188, 0]), ('regression', {'price': 4900, 'setup': 6691, 'period_days': 31, 'used_days': 19, 'min_days': 30, 'mode': 'immediate'}, [158, 0]), ('partial-repair probe', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 53, 'min_days': 30, 'mode': 'immediate'}, [2014, 0]), ('partial-repair probe', {'price': 119900, 'setup': 1279, 'period_days': 30, 'used_days': 14, 'min_days': 7, 'mode': 'immediate'}, [63946, 0]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 55, 'min_days': 14, 'mode': 'period_end'}, [0, 35]), ('normal control', {'price': 999, 'setup': 0, 'period_days': 90, 'used_days': 3, 'min_days': 14, 'mode': 'period_end'}, [0, 87]), ('normal control', {'price': 4900, 'setup': 7, 'period_days': 90, 'used_days': 46, 'min_days': 0, 'mode': 'immediate'}, [2395, 0]), ('normal control', {'price': 119900, 'setup': 5059, 'period_days': 90, 'used_days': 7, 'min_days': 0, 'mode': 'period_end'}, [0, 83])], [('regression', {'price': 999, 'setup': 2500, 'period_days': 30, 'used_days': 13, 'min_days': 14, 'mode': 'immediate'}, [532, 0]), ('regression', {'price': 67850, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'immediate'}, [2188, 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': 24966, 'setup': 5091, 'period_days': 31, 'used_days': 26, 'min_days': 0, 'mode': 'immediate'}, [4026, 0]), ('normal control', {'price': 119900, 'setup': 2500, 'period_days': 365, 'used_days': 323, 'min_days': 7, 'mode': 'period_end'}, [0, 42]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 30, 'used_days': 27, 'min_days': 0, 'mode': 'period_end'}, [0, 3]), ('normal control', {'price': 92433, 'setup': 0, 'period_days': 90, 'used_days': 5, 'min_days': 30, 'mode': 'period_end'}, [0, 85])]]
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 = x['used_days'] + x['min_days']
    refundable_days = max(0, x['period_days'] - charged_days)
    refund = x['price'] * 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': 4970, 'setup': 0, 'period_days': 90, 'used_days': 0, 'min_days': 7, 'mode': 'immediate'}, [4583, 0]), ('partial-repair probe', {'price': 999, 'setup': 2500, 'period_days': 30, 'used_days': 18, 'min_days': 14, 'mode': 'immediate'}, [399, 0]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 30, 'used_days': 22, 'min_days': 14, 'mode': 'immediate'}, [31973, 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': 4900, 'setup': 0, 'period_days': 90, 'used_days': 66, 'min_days': 0, 'mode': 'period_end'}, [0, 24])], [('regression', {'price': 119900, 'setup': 2500, 'period_days': 30, 'used_days': 6, 'min_days': 14, 'mode': 'immediate'}, [63946, 0]), ('regression', {'price': 4970, 'setup': 0, 'period_days': 90, 'used_days': 0, 'min_days': 7, 'mode': 'immediate'}, [4583, 0]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 30, 'used_days': 22, 'min_days': 14, 'mode': 'immediate'}, [31973, 0]), ('partial-repair probe', {'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]), ('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])], [('regression', {'price': 4900, 'setup': 6691, 'period_days': 31, 'used_days': 19, 'min_days': 30, 'mode': 'immediate'}, [158, 0]), ('regression', {'price': 119900, 'setup': 2500, 'period_days': 30, 'used_days': 6, 'min_days': 14, 'mode': 'immediate'}, [63946, 0]), ('partial-repair probe', {'price': 93530, 'setup': 0, 'period_days': 30, 'used_days': 29, 'min_days': 14, 'mode': 'immediate'}, [3117, 0]), ('partial-repair probe', {'price': 4900, 'setup': 1208, 'period_days': 365, 'used_days': 78, 'min_days': 14, 'mode': 'immediate'}, [3852, 0]), ('normal control', {'price': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 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': 2500, 'period_days': 365, 'used_days': 232, 'min_days': 0, 'mode': 'immediate'}, [1785, 0]), ('normal control', {'price': 119900, 'setup': 0, 'period_days': 90, 'used_days': 22, 'min_days': 7, 'mode': 'period_end'}, [0, 68])], [('regression', {'price': 67850, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'immediate'}, [2188, 0]), ('regression', {'price': 4900, 'setup': 6691, 'period_days': 31, 'used_days': 19, 'min_days': 30, 'mode': 'immediate'}, [158, 0]), ('partial-repair probe', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 53, 'min_days': 30, 'mode': 'immediate'}, [2014, 0]), ('partial-repair probe', {'price': 119900, 'setup': 1279, 'period_days': 30, 'used_days': 14, 'min_days': 7, 'mode': 'immediate'}, [63946, 0]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 55, 'min_days': 14, 'mode': 'period_end'}, [0, 35]), ('normal control', {'price': 999, 'setup': 0, 'period_days': 90, 'used_days': 3, 'min_days': 14, 'mode': 'period_end'}, [0, 87]), ('normal control', {'price': 4900, 'setup': 7, 'period_days': 90, 'used_days': 46, 'min_days': 0, 'mode': 'immediate'}, [2395, 0]), ('normal control', {'price': 119900, 'setup': 5059, 'period_days': 90, 'used_days': 7, 'min_days': 0, 'mode': 'period_end'}, [0, 83])], [('regression', {'price': 999, 'setup': 2500, 'period_days': 30, 'used_days': 13, 'min_days': 14, 'mode': 'immediate'}, [532, 0]), ('regression', {'price': 67850, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'immediate'}, [2188, 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': 24966, 'setup': 5091, 'period_days': 31, 'used_days': 26, 'min_days': 0, 'mode': 'immediate'}, [4026, 0]), ('normal control', {'price': 119900, 'setup': 2500, 'period_days': 365, 'used_days': 323, 'min_days': 7, 'mode': 'period_end'}, [0, 42]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 30, 'used_days': 27, 'min_days': 0, 'mode': 'period_end'}, [0, 3]), ('normal control', {'price': 92433, 'setup': 0, 'period_days': 90, 'used_days': 5, 'min_days': 30, 'mode': 'period_end'}, [0, 85])]]
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 cancelling in the first days get refunds exceeding the minimum-charge terms.

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


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, 0]
    charged_days = max(x['used_days'], x['min_days'])
    refundable_days = max(0, x['period_days'] - charged_days)
    refund = x['price'] * 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': 9483, 'period_days': 31, 'used_days': 13, 'min_days': 0, 'mode': 'period_end'}, [0, 18]), ('regression', {'price': 32440, 'setup': 0, 'period_days': 90, 'used_days': 25, 'min_days': 14, 'mode': 'period_end'}, [0, 65]), ('partial-repair probe', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 66, 'min_days': 0, 'mode': 'period_end'}, [0, 24]), ('partial-repair probe', {'price': 999, 'setup': 0, 'period_days': 365, 'used_days': 45, 'min_days': 30, 'mode': 'period_end'}, [0, 320]), ('normal control', {'price': 4900, 'setup': 2500, 'period_days': 90, 'used_days': 1, 'min_days': 7, 'mode': 'immediate'}, [4518, 0]), ('normal control', {'price': 999, 'setup': 2500, 'period_days': 30, 'used_days': 18, 'min_days': 14, 'mode': 'immediate'}, [399, 0]), ('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': 32440, 'setup': 0, 'period_days': 90, 'used_days': 25, 'min_days': 14, 'mode': 'period_end'}, [0, 65]), ('regression', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 66, 'min_days': 0, 'mode': 'period_end'}, [0, 24]), ('partial-repair probe', {'price': 999, 'setup': 0, 'period_days': 365, 'used_days': 45, 'min_days': 30, 'mode': 'period_end'}, [0, 320]), ('partial-repair probe', {'price': 4900, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'period_end'}, [0, 21]), ('normal control', {'price': 119900, 'setup': 0, 'period_days': 30, 'used_days': 22, 'min_days': 14, 'mode': 'immediate'}, [31973, 0]), ('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': 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])], [('regression', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 66, 'min_days': 0, 'mode': 'period_end'}, [0, 24]), ('regression', {'price': 999, 'setup': 0, 'period_days': 365, 'used_days': 45, 'min_days': 30, 'mode': 'period_end'}, [0, 320]), ('partial-repair probe', {'price': 4900, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'period_end'}, [0, 21]), ('partial-repair probe', {'price': 119900, 'setup': 2500, 'period_days': 31, 'used_days': 17, 'min_days': 14, 'mode': 'period_end'}, [0, 14]), ('normal control', {'price': 93530, 'setup': 0, 'period_days': 30, 'used_days': 29, 'min_days': 14, 'mode': 'immediate'}, [3117, 0]), ('normal control', {'price': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 0]), ('normal control', {'price': 4900, 'setup': 1208, 'period_days': 365, 'used_days': 78, 'min_days': 14, 'mode': 'immediate'}, [3852, 0]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 53, 'min_days': 30, 'mode': 'immediate'}, [2014, 0])], [('regression', {'price': 999, 'setup': 0, 'period_days': 365, 'used_days': 45, 'min_days': 30, 'mode': 'period_end'}, [0, 320]), ('regression', {'price': 4900, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'period_end'}, [0, 21]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 90, 'used_days': 22, 'min_days': 7, 'mode': 'period_end'}, [0, 68]), ('partial-repair probe', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 55, 'min_days': 14, 'mode': 'period_end'}, [0, 35]), ('normal control', {'price': 119900, 'setup': 1279, 'period_days': 30, 'used_days': 14, 'min_days': 7, 'mode': 'immediate'}, [63946, 0]), ('normal control', {'price': 4900, 'setup': 2500, 'period_days': 365, 'used_days': 232, 'min_days': 0, 'mode': 'immediate'}, [1785, 0]), ('normal control', {'price': 119900, 'setup': 3409, 'period_days': 31, 'used_days': 27, 'min_days': 7, 'mode': 'immediate'}, [15470, 0]), ('normal control', {'price': 119900, 'setup': 2500, 'period_days': 30, 'used_days': 6, 'min_days': 14, 'mode': 'immediate'}, [63946, 0])], [('regression', {'price': 4900, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'period_end'}, [0, 21]), ('regression', {'price': 119900, 'setup': 2500, 'period_days': 31, 'used_days': 17, 'min_days': 14, 'mode': 'period_end'}, [0, 14]), ('partial-repair probe', {'price': 999, 'setup': 0, 'period_days': 90, 'used_days': 3, 'min_days': 14, 'mode': 'period_end'}, [0, 87]), ('partial-repair probe', {'price': 119900, 'setup': 5059, 'period_days': 90, 'used_days': 7, 'min_days': 0, 'mode': 'period_end'}, [0, 83]), ('normal control', {'price': 4900, 'setup': 7, 'period_days': 90, 'used_days': 46, 'min_days': 0, 'mode': 'immediate'}, [2395, 0]), ('normal control', {'price': 24966, 'setup': 5091, 'period_days': 31, 'used_days': 26, 'min_days': 0, 'mode': 'immediate'}, [4026, 0]), ('normal control', {'price': 4900, 'setup': 6691, 'period_days': 31, 'used_days': 19, 'min_days': 30, 'mode': 'immediate'}, [158, 0]), ('normal control', {'price': 119900, 'setup': 464, 'period_days': 90, 'used_days': 62, 'min_days': 14, 'mode': 'immediate'}, [37302, 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)

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):
    unused = max(0, x['period_days'] - x['used_days'])
    if x['mode'] == 'period_end':
        return [0, x['period_days']]
    charged_days = max(x['used_days'], x['min_days'])
    refundable_days = max(0, x['period_days'] - charged_days)
    refund = x['price'] * 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': 9483, 'period_days': 31, 'used_days': 13, 'min_days': 0, 'mode': 'period_end'}, [0, 18]), ('regression', {'price': 32440, 'setup': 0, 'period_days': 90, 'used_days': 25, 'min_days': 14, 'mode': 'period_end'}, [0, 65]), ('partial-repair probe', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 66, 'min_days': 0, 'mode': 'period_end'}, [0, 24]), ('partial-repair probe', {'price': 999, 'setup': 0, 'period_days': 365, 'used_days': 45, 'min_days': 30, 'mode': 'period_end'}, [0, 320]), ('normal control', {'price': 4900, 'setup': 2500, 'period_days': 90, 'used_days': 1, 'min_days': 7, 'mode': 'immediate'}, [4518, 0]), ('normal control', {'price': 999, 'setup': 2500, 'period_days': 30, 'used_days': 18, 'min_days': 14, 'mode': 'immediate'}, [399, 0]), ('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': 32440, 'setup': 0, 'period_days': 90, 'used_days': 25, 'min_days': 14, 'mode': 'period_end'}, [0, 65]), ('regression', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 66, 'min_days': 0, 'mode': 'period_end'}, [0, 24]), ('partial-repair probe', {'price': 999, 'setup': 0, 'period_days': 365, 'used_days': 45, 'min_days': 30, 'mode': 'period_end'}, [0, 320]), ('partial-repair probe', {'price': 4900, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'period_end'}, [0, 21]), ('normal control', {'price': 119900, 'setup': 0, 'period_days': 30, 'used_days': 22, 'min_days': 14, 'mode': 'immediate'}, [31973, 0]), ('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': 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])], [('regression', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 66, 'min_days': 0, 'mode': 'period_end'}, [0, 24]), ('regression', {'price': 999, 'setup': 0, 'period_days': 365, 'used_days': 45, 'min_days': 30, 'mode': 'period_end'}, [0, 320]), ('partial-repair probe', {'price': 4900, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'period_end'}, [0, 21]), ('partial-repair probe', {'price': 119900, 'setup': 2500, 'period_days': 31, 'used_days': 17, 'min_days': 14, 'mode': 'period_end'}, [0, 14]), ('normal control', {'price': 93530, 'setup': 0, 'period_days': 30, 'used_days': 29, 'min_days': 14, 'mode': 'immediate'}, [3117, 0]), ('normal control', {'price': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 0]), ('normal control', {'price': 4900, 'setup': 1208, 'period_days': 365, 'used_days': 78, 'min_days': 14, 'mode': 'immediate'}, [3852, 0]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 53, 'min_days': 30, 'mode': 'immediate'}, [2014, 0])], [('regression', {'price': 999, 'setup': 0, 'period_days': 365, 'used_days': 45, 'min_days': 30, 'mode': 'period_end'}, [0, 320]), ('regression', {'price': 4900, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'period_end'}, [0, 21]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 90, 'used_days': 22, 'min_days': 7, 'mode': 'period_end'}, [0, 68]), ('partial-repair probe', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 55, 'min_days': 14, 'mode': 'period_end'}, [0, 35]), ('normal control', {'price': 119900, 'setup': 1279, 'period_days': 30, 'used_days': 14, 'min_days': 7, 'mode': 'immediate'}, [63946, 0]), ('normal control', {'price': 4900, 'setup': 2500, 'period_days': 365, 'used_days': 232, 'min_days': 0, 'mode': 'immediate'}, [1785, 0]), ('normal control', {'price': 119900, 'setup': 3409, 'period_days': 31, 'used_days': 27, 'min_days': 7, 'mode': 'immediate'}, [15470, 0]), ('normal control', {'price': 119900, 'setup': 2500, 'period_days': 30, 'used_days': 6, 'min_days': 14, 'mode': 'immediate'}, [63946, 0])], [('regression', {'price': 4900, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'period_end'}, [0, 21]), ('regression', {'price': 119900, 'setup': 2500, 'period_days': 31, 'used_days': 17, 'min_days': 14, 'mode': 'period_end'}, [0, 14]), ('partial-repair probe', {'price': 999, 'setup': 0, 'period_days': 90, 'used_days': 3, 'min_days': 14, 'mode': 'period_end'}, [0, 87]), ('partial-repair probe', {'price': 119900, 'setup': 5059, 'period_days': 90, 'used_days': 7, 'min_days': 0, 'mode': 'period_end'}, [0, 83]), ('normal control', {'price': 4900, 'setup': 7, 'period_days': 90, 'used_days': 46, 'min_days': 0, 'mode': 'immediate'}, [2395, 0]), ('normal control', {'price': 24966, 'setup': 5091, 'period_days': 31, 'used_days': 26, 'min_days': 0, 'mode': 'immediate'}, [4026, 0]), ('normal control', {'price': 4900, 'setup': 6691, 'period_days': 31, 'used_days': 19, 'min_days': 30, 'mode': 'immediate'}, [158, 0]), ('normal control', {'price': 119900, 'setup': 464, 'period_days': 90, 'used_days': 62, 'min_days': 14, 'mode': 'immediate'}, [37302, 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 cancelling at period end lose access immediately.

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


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'] * refundable_days * 2 + x['period_days']) // (2 * 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': 4970, 'setup': 0, 'period_days': 90, 'used_days': 0, 'min_days': 7, 'mode': 'immediate'}, [4583, 0]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 30, 'used_days': 22, 'min_days': 14, 'mode': 'immediate'}, [31973, 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': 4900, 'setup': 0, 'period_days': 90, 'used_days': 66, 'min_days': 0, 'mode': 'period_end'}, [0, 24])], [('regression', {'price': 999, 'setup': 2500, 'period_days': 30, 'used_days': 18, 'min_days': 14, 'mode': 'immediate'}, [399, 0]), ('regression', {'price': 86963, 'setup': 0, 'period_days': 90, 'used_days': 71, 'min_days': 14, 'mode': 'immediate'}, [18358, 0]), ('partial-repair probe', {'price': 4970, 'setup': 0, 'period_days': 90, 'used_days': 0, 'min_days': 7, 'mode': 'immediate'}, [4583, 0]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 30, 'used_days': 22, 'min_days': 14, 'mode': 'immediate'}, [31973, 0]), ('normal control', {'price': 999, 'setup': 0, 'period_days': 365, 'used_days': 45, 'min_days': 30, 'mode': 'period_end'}, [0, 320]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'period_end'}, [0, 21]), ('normal control', {'price': 119900, 'setup': 2500, 'period_days': 31, 'used_days': 17, 'min_days': 14, 'mode': 'period_end'}, [0, 14]), ('normal control', {'price': 119900, 'setup': 0, 'period_days': 90, 'used_days': 22, 'min_days': 7, 'mode': 'period_end'}, [0, 68])], [('regression', {'price': 86963, 'setup': 0, 'period_days': 90, 'used_days': 71, 'min_days': 14, 'mode': 'immediate'}, [18358, 0]), ('regression', {'price': 93530, 'setup': 0, 'period_days': 30, 'used_days': 29, 'min_days': 14, 'mode': 'immediate'}, [3117, 0]), ('partial-repair probe', {'price': 999, 'setup': 0, 'period_days': 30, 'used_days': 19, 'min_days': 0, 'mode': 'immediate'}, [366, 0]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 365, 'used_days': 32, 'min_days': 0, 'mode': 'immediate'}, [109388, 0]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 55, 'min_days': 14, 'mode': 'period_end'}, [0, 35]), ('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])], [('regression', {'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]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 365, 'used_days': 32, 'min_days': 0, 'mode': 'immediate'}, [109388, 0]), ('partial-repair probe', {'price': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 0]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 30, 'used_days': 27, 'min_days': 0, 'mode': 'period_end'}, [0, 3]), ('normal control', {'price': 92433, 'setup': 0, 'period_days': 90, 'used_days': 5, 'min_days': 30, 'mode': 'period_end'}, [0, 85]), ('normal control', {'price': 83250, 'setup': 7374, 'period_days': 30, 'used_days': 17, 'min_days': 30, 'mode': 'immediate'}, [0, 0]), ('normal control', {'price': 999, 'setup': 6401, 'period_days': 31, 'used_days': 18, 'min_days': 30, 'mode': 'period_end'}, [0, 13])], [('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': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 0]), ('partial-repair probe', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 53, 'min_days': 30, 'mode': 'immediate'}, [2014, 0]), ('normal control', {'price': 4900, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'period_end'}, [0, 15]), ('normal control', {'price': 119900, 'setup': 2500, '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': 0, 'min_days': 0, 'mode': 'immediate'}, [999, 0]), ('normal control', {'price': 219, 'setup': 2500, 'period_days': 365, 'used_days': 164, 'min_days': 7, 'mode': 'period_end'}, [0, 201])]]
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 = -(-x['price'] * 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': 4970, 'setup': 0, 'period_days': 90, 'used_days': 0, 'min_days': 7, 'mode': 'immediate'}, [4583, 0]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 30, 'used_days': 22, 'min_days': 14, 'mode': 'immediate'}, [31973, 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': 4900, 'setup': 0, 'period_days': 90, 'used_days': 66, 'min_days': 0, 'mode': 'period_end'}, [0, 24])], [('regression', {'price': 999, 'setup': 2500, 'period_days': 30, 'used_days': 18, 'min_days': 14, 'mode': 'immediate'}, [399, 0]), ('regression', {'price': 86963, 'setup': 0, 'period_days': 90, 'used_days': 71, 'min_days': 14, 'mode': 'immediate'}, [18358, 0]), ('partial-repair probe', {'price': 4970, 'setup': 0, 'period_days': 90, 'used_days': 0, 'min_days': 7, 'mode': 'immediate'}, [4583, 0]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 30, 'used_days': 22, 'min_days': 14, 'mode': 'immediate'}, [31973, 0]), ('normal control', {'price': 999, 'setup': 0, 'period_days': 365, 'used_days': 45, 'min_days': 30, 'mode': 'period_end'}, [0, 320]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 31, 'used_days': 10, 'min_days': 30, 'mode': 'period_end'}, [0, 21]), ('normal control', {'price': 119900, 'setup': 2500, 'period_days': 31, 'used_days': 17, 'min_days': 14, 'mode': 'period_end'}, [0, 14]), ('normal control', {'price': 119900, 'setup': 0, 'period_days': 90, 'used_days': 22, 'min_days': 7, 'mode': 'period_end'}, [0, 68])], [('regression', {'price': 86963, 'setup': 0, 'period_days': 90, 'used_days': 71, 'min_days': 14, 'mode': 'immediate'}, [18358, 0]), ('regression', {'price': 93530, 'setup': 0, 'period_days': 30, 'used_days': 29, 'min_days': 14, 'mode': 'immediate'}, [3117, 0]), ('partial-repair probe', {'price': 999, 'setup': 0, 'period_days': 30, 'used_days': 19, 'min_days': 0, 'mode': 'immediate'}, [366, 0]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 365, 'used_days': 32, 'min_days': 0, 'mode': 'immediate'}, [109388, 0]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 55, 'min_days': 14, 'mode': 'period_end'}, [0, 35]), ('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])], [('regression', {'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]), ('partial-repair probe', {'price': 119900, 'setup': 0, 'period_days': 365, 'used_days': 32, 'min_days': 0, 'mode': 'immediate'}, [109388, 0]), ('partial-repair probe', {'price': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 0]), ('normal control', {'price': 4900, 'setup': 0, 'period_days': 30, 'used_days': 27, 'min_days': 0, 'mode': 'period_end'}, [0, 3]), ('normal control', {'price': 92433, 'setup': 0, 'period_days': 90, 'used_days': 5, 'min_days': 30, 'mode': 'period_end'}, [0, 85]), ('normal control', {'price': 83250, 'setup': 7374, 'period_days': 30, 'used_days': 17, 'min_days': 30, 'mode': 'immediate'}, [0, 0]), ('normal control', {'price': 999, 'setup': 6401, 'period_days': 31, 'used_days': 18, 'min_days': 30, 'mode': 'period_end'}, [0, 13])], [('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': 999, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'immediate'}, [483, 0]), ('partial-repair probe', {'price': 4900, 'setup': 0, 'period_days': 90, 'used_days': 53, 'min_days': 30, 'mode': 'immediate'}, [2014, 0]), ('normal control', {'price': 4900, 'setup': 2500, 'period_days': 31, 'used_days': 16, 'min_days': 0, 'mode': 'period_end'}, [0, 15]), ('normal control', {'price': 119900, 'setup': 2500, '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': 0, 'min_days': 0, 'mode': 'immediate'}, [999, 0]), ('normal control', {'price': 219, 'setup': 2500, 'period_days': 365, 'used_days': 164, 'min_days': 7, 'mode': 'period_end'}, [0, 201])]]
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:
Refunds sometimes exceed the exact unused value by a cent.

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


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

N = 1
observations = []
def solve(x):
    allow = x['included'] * x['active_days'] // x['period_days']
    units = max(0, x['usage'] - allow)
    if units == 0:
        return [allow, 0]
    if x['mode'] == 'volume':
        for up, price, flat in x['tiers']:
            if up is None or units < up:
                return [allow, units * price + flat]
    cost = 0
    prev = 0
    for up, price, flat in x['tiers']:
        top = units if up is None else min(units, up)
        if top > prev:
            cost += (top - prev) * price + flat
        if up is None or units <= up:
            break
        prev = up
    return [allow, cost]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'usage': 344, 'tiers': [[344, 8, 0], [602, 31, 0], [None, 12, 0]], 'mode': 'volume', 'included': 0, 'active_days': 5, 'period_days': 30}, [0, 2752]), ('regression', {'usage': 255, 'tiers': [[255, 8, 0], [492, 6, 0], [None, 26, 0]], 'mode': 'volume', 'included': 0, 'active_days': 17, 'period_days': 30}, [0, 2040]), ('partial-repair probe', {'usage': 139, 'tiers': [[132, 13, 500], [874, 26, 0], [None, 1, 0]], 'mode': 'volume', 'included': 250, 'active_days': 5, 'period_days': 31}, [40, 1787]), ('partial-repair probe', {'usage': 399, 'tiers': [[281, 41, 0], [1219, 16, 0], [None, 4, 0]], 'mode': 'volume', 'included': 250, 'active_days': 19, 'period_days': 30}, [158, 9881]), ('normal control', {'usage': 215, 'tiers': [[215, 32, 0], [1030, 38, 300], [None, 21, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 27, 'period_days': 30}, [900, 0]), ('normal control', {'usage': 2690, 'tiers': [[446, 10, 0], [1155, 29, 300], [None, 24, 0]], 'mode': 'volume', 'included': 250, 'active_days': 1, 'period_days': 31}, [8, 64368]), ('normal control', {'usage': 195, 'tiers': [[263, 41, 0], [444, 16, 300], [None, 18, 0]], 'mode': 'volume', 'included': 0, 'active_days': 28, 'period_days': 31}, [0, 7995]), ('normal control', {'usage': 149, 'tiers': [[96, 6, 0], [296, 34, 0], [None, 9, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 27, 'period_days': 30}, [0, 2378])], [('regression', {'usage': 255, 'tiers': [[255, 8, 0], [492, 6, 0], [None, 26, 0]], 'mode': 'volume', 'included': 0, 'active_days': 17, 'period_days': 30}, [0, 2040]), ('regression', {'usage': 317, 'tiers': [[317, 39, 500], [991, 40, 0], [None, 22, 0]], 'mode': 'volume', 'included': 0, 'active_days': 10, 'period_days': 31}, [0, 12863]), ('partial-repair probe', {'usage': 1047, 'tiers': [[84, 10, 500], [579, 23, 300], [None, 15, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 25, 'period_days': 31}, [806, 5843]), ('partial-repair probe', {'usage': 627, 'tiers': [[182, 46, 0], [404, 28, 0], [None, 30, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 16, 'period_days': 31}, [516, 5106]), ('normal control', {'usage': 520, 'tiers': [[335, 27, 0], [725, 26, 0], [None, 26, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('normal control', {'usage': 132, 'tiers': [[102, 43, 500], [1090, 39, 300], [None, 1, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 31, 'period_days': 31}, [100, 1876]), ('normal control', {'usage': 1908, 'tiers': [[438, 11, 500], [1328, 8, 0], [None, 10, 0]], 'mode': 'volume', 'included': 0, 'active_days': 3, 'period_days': 31}, [0, 19080]), ('normal control', {'usage': 152, 'tiers': [[116, 12, 0], [540, 33, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 31}, [709, 0])], [('regression', {'usage': 317, 'tiers': [[317, 39, 500], [991, 40, 0], [None, 22, 0]], 'mode': 'volume', 'included': 0, 'active_days': 10, 'period_days': 31}, [0, 12863]), ('regression', {'usage': 91, 'tiers': [[91, 27, 0], [619, 9, 300], [None, 1, 0]], 'mode': 'volume', 'included': 0, 'active_days': 18, 'period_days': 31}, [0, 2457]), ('partial-repair probe', {'usage': 1390, 'tiers': [[427, 31, 500], [1310, 18, 300], [None, 20, 0]], 'mode': 'volume', 'included': 100, 'active_days': 25, 'period_days': 30}, [83, 23826]), ('partial-repair probe', {'usage': 139, 'tiers': [[132, 13, 500], [874, 26, 0], [None, 1, 0]], 'mode': 'volume', 'included': 250, 'active_days': 5, 'period_days': 31}, [40, 1787]), ('normal control', {'usage': 158, 'tiers': [[158, 6, 0], [662, 22, 0], [None, 15, 0]], 'mode': 'volume', 'included': 100, 'active_days': 10, 'period_days': 30}, [33, 750]), ('normal control', {'usage': 47, 'tiers': [[264, 39, 0], [1255, 6, 0], [None, 12, 0]], 'mode': 'volume', 'included': 250, 'active_days': 24, 'period_days': 30}, [200, 0]), ('normal control', {'usage': 31, 'tiers': [[57, 14, 0], [997, 11, 300], [None, 7, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 3, 'period_days': 30}, [100, 0]), ('normal control', {'usage': 335, 'tiers': [[335, 30, 500], [789, 38, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 4, 'period_days': 30}, [13, 10160])], [('regression', {'usage': 91, 'tiers': [[91, 27, 0], [619, 9, 300], [None, 1, 0]], 'mode': 'volume', 'included': 0, 'active_days': 18, 'period_days': 31}, [0, 2457]), ('regression', {'usage': 219, 'tiers': [[219, 31, 0], [1195, 3, 300], [None, 10, 0]], 'mode': 'volume', 'included': 0, 'active_days': 10, 'period_days': 31}, [0, 6789]), ('partial-repair probe', {'usage': 399, 'tiers': [[281, 41, 0], [1219, 16, 0], [None, 4, 0]], 'mode': 'volume', 'included': 250, 'active_days': 19, 'period_days': 30}, [158, 9881]), ('partial-repair probe', {'usage': 1047, 'tiers': [[84, 10, 500], [579, 23, 300], [None, 15, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 25, 'period_days': 31}, [806, 5843]), ('normal control', {'usage': 1862, 'tiers': [[90, 6, 0], [1073, 39, 0], [None, 20, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 29, 'period_days': 31}, [93, 52797]), ('normal control', {'usage': 129, 'tiers': [[129, 9, 500], [845, 8, 300], [None, 18, 0]], 'mode': 'volume', 'included': 250, 'active_days': 29, 'period_days': 30}, [241, 0]), ('normal control', {'usage': 2285, 'tiers': [[395, 26, 500], [1142, 40, 0], [None, 24, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 13, 'period_days': 30}, [0, 68082]), ('normal control', {'usage': 205, 'tiers': [[205, 36, 0], [1123, 17, 300], [None, 4, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 16, 'period_days': 31}, [516, 0])], [('regression', {'usage': 219, 'tiers': [[219, 31, 0], [1195, 3, 300], [None, 10, 0]], 'mode': 'volume', 'included': 0, 'active_days': 10, 'period_days': 31}, [0, 6789]), ('regression', {'usage': 401, 'tiers': [[401, 14, 500], [1132, 13, 0], [None, 29, 0]], 'mode': 'volume', 'included': 0, 'active_days': 11, 'period_days': 30}, [0, 6114]), ('partial-repair probe', {'usage': 627, 'tiers': [[182, 46, 0], [404, 28, 0], [None, 30, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 16, 'period_days': 31}, [516, 5106]), ('partial-repair probe', {'usage': 1390, 'tiers': [[427, 31, 500], [1310, 18, 300], [None, 20, 0]], 'mode': 'volume', 'included': 100, 'active_days': 25, 'period_days': 30}, [83, 23826]), ('normal control', {'usage': 286, 'tiers': [[286, 31, 500], [1169, 13, 0], [None, 22, 0]], 'mode': 'volume', 'included': 250, 'active_days': 30, 'period_days': 31}, [241, 1895]), ('normal control', {'usage': 22, 'tiers': [[271, 40, 0], [731, 39, 0], [None, 19, 0]], 'mode': 'graduated', 'included': 250, 'active_days': 23, 'period_days': 30}, [191, 0]), ('normal control', {'usage': 1383, 'tiers': [[285, 7, 0], [550, 30, 0], [None, 24, 0]], 'mode': 'graduated', 'included': 250, 'active_days': 24, 'period_days': 31}, [193, 25305]), ('normal control', {'usage': 77, 'tiers': [[278, 41, 0], [1269, 3, 0], [None, 6, 0]], 'mode': 'volume', 'included': 100, 'active_days': 13, 'period_days': 30}, [43, 1394])]]
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):
    allow = x['included'] * x['active_days'] // x['period_days']
    units = max(0, x['usage'] - allow)
    if units == 0:
        return [allow, 0]
    if x['mode'] == 'volume':
        for up, price, flat in x['tiers']:
            if up is None or x['usage'] <= up:
                return [allow, units * price + flat]
    cost = 0
    prev = 0
    for up, price, flat in x['tiers']:
        top = units if up is None else min(units, up)
        if top > prev:
            cost += (top - prev) * price + flat
        if up is None or units <= up:
            break
        prev = up
    return [allow, cost]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'usage': 344, 'tiers': [[344, 8, 0], [602, 31, 0], [None, 12, 0]], 'mode': 'volume', 'included': 0, 'active_days': 5, 'period_days': 30}, [0, 2752]), ('regression', {'usage': 255, 'tiers': [[255, 8, 0], [492, 6, 0], [None, 26, 0]], 'mode': 'volume', 'included': 0, 'active_days': 17, 'period_days': 30}, [0, 2040]), ('partial-repair probe', {'usage': 139, 'tiers': [[132, 13, 500], [874, 26, 0], [None, 1, 0]], 'mode': 'volume', 'included': 250, 'active_days': 5, 'period_days': 31}, [40, 1787]), ('partial-repair probe', {'usage': 399, 'tiers': [[281, 41, 0], [1219, 16, 0], [None, 4, 0]], 'mode': 'volume', 'included': 250, 'active_days': 19, 'period_days': 30}, [158, 9881]), ('normal control', {'usage': 215, 'tiers': [[215, 32, 0], [1030, 38, 300], [None, 21, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 27, 'period_days': 30}, [900, 0]), ('normal control', {'usage': 2690, 'tiers': [[446, 10, 0], [1155, 29, 300], [None, 24, 0]], 'mode': 'volume', 'included': 250, 'active_days': 1, 'period_days': 31}, [8, 64368]), ('normal control', {'usage': 195, 'tiers': [[263, 41, 0], [444, 16, 300], [None, 18, 0]], 'mode': 'volume', 'included': 0, 'active_days': 28, 'period_days': 31}, [0, 7995]), ('normal control', {'usage': 149, 'tiers': [[96, 6, 0], [296, 34, 0], [None, 9, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 27, 'period_days': 30}, [0, 2378])], [('regression', {'usage': 255, 'tiers': [[255, 8, 0], [492, 6, 0], [None, 26, 0]], 'mode': 'volume', 'included': 0, 'active_days': 17, 'period_days': 30}, [0, 2040]), ('regression', {'usage': 317, 'tiers': [[317, 39, 500], [991, 40, 0], [None, 22, 0]], 'mode': 'volume', 'included': 0, 'active_days': 10, 'period_days': 31}, [0, 12863]), ('partial-repair probe', {'usage': 1047, 'tiers': [[84, 10, 500], [579, 23, 300], [None, 15, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 25, 'period_days': 31}, [806, 5843]), ('partial-repair probe', {'usage': 627, 'tiers': [[182, 46, 0], [404, 28, 0], [None, 30, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 16, 'period_days': 31}, [516, 5106]), ('normal control', {'usage': 520, 'tiers': [[335, 27, 0], [725, 26, 0], [None, 26, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('normal control', {'usage': 132, 'tiers': [[102, 43, 500], [1090, 39, 300], [None, 1, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 31, 'period_days': 31}, [100, 1876]), ('normal control', {'usage': 1908, 'tiers': [[438, 11, 500], [1328, 8, 0], [None, 10, 0]], 'mode': 'volume', 'included': 0, 'active_days': 3, 'period_days': 31}, [0, 19080]), ('normal control', {'usage': 152, 'tiers': [[116, 12, 0], [540, 33, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 31}, [709, 0])], [('regression', {'usage': 317, 'tiers': [[317, 39, 500], [991, 40, 0], [None, 22, 0]], 'mode': 'volume', 'included': 0, 'active_days': 10, 'period_days': 31}, [0, 12863]), ('regression', {'usage': 91, 'tiers': [[91, 27, 0], [619, 9, 300], [None, 1, 0]], 'mode': 'volume', 'included': 0, 'active_days': 18, 'period_days': 31}, [0, 2457]), ('partial-repair probe', {'usage': 1390, 'tiers': [[427, 31, 500], [1310, 18, 300], [None, 20, 0]], 'mode': 'volume', 'included': 100, 'active_days': 25, 'period_days': 30}, [83, 23826]), ('partial-repair probe', {'usage': 139, 'tiers': [[132, 13, 500], [874, 26, 0], [None, 1, 0]], 'mode': 'volume', 'included': 250, 'active_days': 5, 'period_days': 31}, [40, 1787]), ('normal control', {'usage': 158, 'tiers': [[158, 6, 0], [662, 22, 0], [None, 15, 0]], 'mode': 'volume', 'included': 100, 'active_days': 10, 'period_days': 30}, [33, 750]), ('normal control', {'usage': 47, 'tiers': [[264, 39, 0], [1255, 6, 0], [None, 12, 0]], 'mode': 'volume', 'included': 250, 'active_days': 24, 'period_days': 30}, [200, 0]), ('normal control', {'usage': 31, 'tiers': [[57, 14, 0], [997, 11, 300], [None, 7, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 3, 'period_days': 30}, [100, 0]), ('normal control', {'usage': 335, 'tiers': [[335, 30, 500], [789, 38, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 4, 'period_days': 30}, [13, 10160])], [('regression', {'usage': 91, 'tiers': [[91, 27, 0], [619, 9, 300], [None, 1, 0]], 'mode': 'volume', 'included': 0, 'active_days': 18, 'period_days': 31}, [0, 2457]), ('regression', {'usage': 219, 'tiers': [[219, 31, 0], [1195, 3, 300], [None, 10, 0]], 'mode': 'volume', 'included': 0, 'active_days': 10, 'period_days': 31}, [0, 6789]), ('partial-repair probe', {'usage': 399, 'tiers': [[281, 41, 0], [1219, 16, 0], [None, 4, 0]], 'mode': 'volume', 'included': 250, 'active_days': 19, 'period_days': 30}, [158, 9881]), ('partial-repair probe', {'usage': 1047, 'tiers': [[84, 10, 500], [579, 23, 300], [None, 15, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 25, 'period_days': 31}, [806, 5843]), ('normal control', {'usage': 1862, 'tiers': [[90, 6, 0], [1073, 39, 0], [None, 20, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 29, 'period_days': 31}, [93, 52797]), ('normal control', {'usage': 129, 'tiers': [[129, 9, 500], [845, 8, 300], [None, 18, 0]], 'mode': 'volume', 'included': 250, 'active_days': 29, 'period_days': 30}, [241, 0]), ('normal control', {'usage': 2285, 'tiers': [[395, 26, 500], [1142, 40, 0], [None, 24, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 13, 'period_days': 30}, [0, 68082]), ('normal control', {'usage': 205, 'tiers': [[205, 36, 0], [1123, 17, 300], [None, 4, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 16, 'period_days': 31}, [516, 0])], [('regression', {'usage': 219, 'tiers': [[219, 31, 0], [1195, 3, 300], [None, 10, 0]], 'mode': 'volume', 'included': 0, 'active_days': 10, 'period_days': 31}, [0, 6789]), ('regression', {'usage': 401, 'tiers': [[401, 14, 500], [1132, 13, 0], [None, 29, 0]], 'mode': 'volume', 'included': 0, 'active_days': 11, 'period_days': 30}, [0, 6114]), ('partial-repair probe', {'usage': 627, 'tiers': [[182, 46, 0], [404, 28, 0], [None, 30, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 16, 'period_days': 31}, [516, 5106]), ('partial-repair probe', {'usage': 1390, 'tiers': [[427, 31, 500], [1310, 18, 300], [None, 20, 0]], 'mode': 'volume', 'included': 100, 'active_days': 25, 'period_days': 30}, [83, 23826]), ('normal control', {'usage': 286, 'tiers': [[286, 31, 500], [1169, 13, 0], [None, 22, 0]], 'mode': 'volume', 'included': 250, 'active_days': 30, 'period_days': 31}, [241, 1895]), ('normal control', {'usage': 22, 'tiers': [[271, 40, 0], [731, 39, 0], [None, 19, 0]], 'mode': 'graduated', 'included': 250, 'active_days': 23, 'period_days': 30}, [191, 0]), ('normal control', {'usage': 1383, 'tiers': [[285, 7, 0], [550, 30, 0], [None, 24, 0]], 'mode': 'graduated', 'included': 250, 'active_days': 24, 'period_days': 31}, [193, 25305]), ('normal control', {'usage': 77, 'tiers': [[278, 41, 0], [1269, 3, 0], [None, 6, 0]], 'mode': 'volume', 'included': 100, 'active_days': 13, 'period_days': 30}, [43, 1394])]]
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:
Usage landing exactly on a tier limit is priced at the next tier.

Input {usage, tiers: [[up_to|None, unit_price, flat_fee]], mode volume|graduated, included, active_days, period_days}. Allowance = included*active_days//period_days. Billable = max(0, usage - allowance); zero billable costs 0. Volume: all billable units at the first tier with billable <= up_to, plus that tier's flat fee. Graduated: each tier bills the units falling in its range plus its flat fee if any unit falls in it. Return [allowance, cost].

split: open-access


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

N = 1
observations = []
def solve(x):
    allow = x['included']
    units = max(0, x['usage'] - allow)
    if units == 0:
        return [allow, 0]
    if x['mode'] == 'volume':
        for up, price, flat in x['tiers']:
            if up is None or units <= up:
                return [allow, units * price + flat]
    cost = 0
    prev = 0
    for up, price, flat in x['tiers']:
        top = units if up is None else min(units, up)
        if top > prev:
            cost += (top - prev) * price + flat
        if up is None or units <= up:
            break
        prev = up
    return [allow, cost]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'usage': 2690, 'tiers': [[446, 10, 0], [1155, 29, 300], [None, 24, 0]], 'mode': 'volume', 'included': 250, 'active_days': 1, 'period_days': 31}, [8, 64368]), ('regression', {'usage': 215, 'tiers': [[215, 32, 0], [1030, 38, 300], [None, 21, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 27, 'period_days': 30}, [900, 0]), ('partial-repair probe', {'usage': 520, 'tiers': [[335, 27, 0], [725, 26, 0], [None, 26, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('partial-repair probe', {'usage': 152, 'tiers': [[116, 12, 0], [540, 33, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 31}, [709, 0]), ('normal control', {'usage': 195, 'tiers': [[263, 41, 0], [444, 16, 300], [None, 18, 0]], 'mode': 'volume', 'included': 0, 'active_days': 28, 'period_days': 31}, [0, 7995]), ('normal control', {'usage': 149, 'tiers': [[96, 6, 0], [296, 34, 0], [None, 9, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 27, 'period_days': 30}, [0, 2378]), ('normal control', {'usage': 132, 'tiers': [[102, 43, 500], [1090, 39, 300], [None, 1, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 31, 'period_days': 31}, [100, 1876]), ('normal control', {'usage': 1908, 'tiers': [[438, 11, 500], [1328, 8, 0], [None, 10, 0]], 'mode': 'volume', 'included': 0, 'active_days': 3, 'period_days': 31}, [0, 19080])], [('regression', {'usage': 520, 'tiers': [[335, 27, 0], [725, 26, 0], [None, 26, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('regression', {'usage': 2690, 'tiers': [[446, 10, 0], [1155, 29, 300], [None, 24, 0]], 'mode': 'volume', 'included': 250, 'active_days': 1, 'period_days': 31}, [8, 64368]), ('partial-repair probe', {'usage': 152, 'tiers': [[116, 12, 0], [540, 33, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 31}, [709, 0]), ('partial-repair probe', {'usage': 158, 'tiers': [[158, 6, 0], [662, 22, 0], [None, 15, 0]], 'mode': 'volume', 'included': 100, 'active_days': 10, 'period_days': 30}, [33, 750]), ('normal control', {'usage': 2285, 'tiers': [[395, 26, 500], [1142, 40, 0], [None, 24, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 13, 'period_days': 30}, [0, 68082]), ('normal control', {'usage': 340, 'tiers': [[481, 48, 0], [1132, 35, 300], [None, 6, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 30, 'period_days': 30}, [1000, 0]), ('normal control', {'usage': 392, 'tiers': [[392, 7, 0], [719, 26, 0], [None, 8, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 22, 'period_days': 30}, [0, 2744]), ('normal control', {'usage': 422, 'tiers': [[422, 16, 0], [1350, 20, 0], [None, 20, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 31, 'period_days': 31}, [1000, 0])], [('regression', {'usage': 152, 'tiers': [[116, 12, 0], [540, 33, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 31}, [709, 0]), ('regression', {'usage': 520, 'tiers': [[335, 27, 0], [725, 26, 0], [None, 26, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('partial-repair probe', {'usage': 335, 'tiers': [[335, 30, 500], [789, 38, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 4, 'period_days': 30}, [13, 10160]), ('partial-repair probe', {'usage': 1862, 'tiers': [[90, 6, 0], [1073, 39, 0], [None, 20, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 29, 'period_days': 31}, [93, 52797]), ('normal control', {'usage': 107, 'tiers': [[490, 47, 0], [1088, 17, 0], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 30, 'period_days': 30}, [1000, 0]), ('normal control', {'usage': 106, 'tiers': [[301, 42, 500], [930, 23, 300], [None, 29, 0]], 'mode': 'volume', 'included': 0, 'active_days': 24, 'period_days': 30}, [0, 4952]), ('normal control', {'usage': 143, 'tiers': [[493, 29, 0], [634, 36, 0], [None, 23, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 2, 'period_days': 31}, [0, 4147]), ('normal control', {'usage': 1774, 'tiers': [[155, 27, 0], [290, 6, 0], [None, 14, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 18, 'period_days': 31}, [0, 25771])], [('regression', {'usage': 158, 'tiers': [[158, 6, 0], [662, 22, 0], [None, 15, 0]], 'mode': 'volume', 'included': 100, 'active_days': 10, 'period_days': 30}, [33, 750]), ('regression', {'usage': 152, 'tiers': [[116, 12, 0], [540, 33, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 31}, [709, 0]), ('partial-repair probe', {'usage': 129, 'tiers': [[129, 9, 500], [845, 8, 300], [None, 18, 0]], 'mode': 'volume', 'included': 250, 'active_days': 29, 'period_days': 30}, [241, 0]), ('partial-repair probe', {'usage': 205, 'tiers': [[205, 36, 0], [1123, 17, 300], [None, 4, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 16, 'period_days': 31}, [516, 0]), ('normal control', {'usage': 310, 'tiers': [[237, 10, 0], [892, 12, 0], [None, 15, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 14, 'period_days': 31}, [0, 3246]), ('normal control', {'usage': 66, 'tiers': [[349, 9, 0], [1199, 11, 0], [None, 7, 0]], 'mode': 'volume', 'included': 0, 'active_days': 28, 'period_days': 30}, [0, 594]), ('normal control', {'usage': 344, 'tiers': [[344, 8, 0], [602, 31, 0], [None, 12, 0]], 'mode': 'volume', 'included': 0, 'active_days': 5, 'period_days': 30}, [0, 2752]), ('normal control', {'usage': 270, 'tiers': [[270, 9, 500], [388, 14, 0], [None, 27, 0]], 'mode': 'volume', 'included': 100, 'active_days': 30, 'period_days': 30}, [100, 2030])], [('regression', {'usage': 335, 'tiers': [[335, 30, 500], [789, 38, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 4, 'period_days': 30}, [13, 10160]), ('regression', {'usage': 158, 'tiers': [[158, 6, 0], [662, 22, 0], [None, 15, 0]], 'mode': 'volume', 'included': 100, 'active_days': 10, 'period_days': 30}, [33, 750]), ('partial-repair probe', {'usage': 286, 'tiers': [[286, 31, 500], [1169, 13, 0], [None, 22, 0]], 'mode': 'volume', 'included': 250, 'active_days': 30, 'period_days': 31}, [241, 1895]), ('partial-repair probe', {'usage': 22, 'tiers': [[271, 40, 0], [731, 39, 0], [None, 19, 0]], 'mode': 'graduated', 'included': 250, 'active_days': 23, 'period_days': 30}, [191, 0]), ('normal control', {'usage': 255, 'tiers': [[255, 8, 0], [492, 6, 0], [None, 26, 0]], 'mode': 'volume', 'included': 0, 'active_days': 17, 'period_days': 30}, [0, 2040]), ('normal control', {'usage': 187, 'tiers': [[153, 21, 500], [679, 13, 300], [None, 25, 0]], 'mode': 'volume', 'included': 0, 'active_days': 27, 'period_days': 31}, [0, 2731]), ('normal control', {'usage': 197, 'tiers': [[288, 8, 0], [714, 8, 0], [None, 2, 0]], 'mode': 'volume', 'included': 0, 'active_days': 24, 'period_days': 31}, [0, 1576]), ('normal control', {'usage': 317, 'tiers': [[317, 39, 500], [991, 40, 0], [None, 22, 0]], 'mode': 'volume', 'included': 0, 'active_days': 10, 'period_days': 31}, [0, 12863])]]
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):
    allow = -(-x['included'] * x['active_days'] // x['period_days'])
    units = max(0, x['usage'] - allow)
    if units == 0:
        return [allow, 0]
    if x['mode'] == 'volume':
        for up, price, flat in x['tiers']:
            if up is None or units <= up:
                return [allow, units * price + flat]
    cost = 0
    prev = 0
    for up, price, flat in x['tiers']:
        top = units if up is None else min(units, up)
        if top > prev:
            cost += (top - prev) * price + flat
        if up is None or units <= up:
            break
        prev = up
    return [allow, cost]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'usage': 2690, 'tiers': [[446, 10, 0], [1155, 29, 300], [None, 24, 0]], 'mode': 'volume', 'included': 250, 'active_days': 1, 'period_days': 31}, [8, 64368]), ('regression', {'usage': 215, 'tiers': [[215, 32, 0], [1030, 38, 300], [None, 21, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 27, 'period_days': 30}, [900, 0]), ('partial-repair probe', {'usage': 520, 'tiers': [[335, 27, 0], [725, 26, 0], [None, 26, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('partial-repair probe', {'usage': 152, 'tiers': [[116, 12, 0], [540, 33, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 31}, [709, 0]), ('normal control', {'usage': 195, 'tiers': [[263, 41, 0], [444, 16, 300], [None, 18, 0]], 'mode': 'volume', 'included': 0, 'active_days': 28, 'period_days': 31}, [0, 7995]), ('normal control', {'usage': 149, 'tiers': [[96, 6, 0], [296, 34, 0], [None, 9, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 27, 'period_days': 30}, [0, 2378]), ('normal control', {'usage': 132, 'tiers': [[102, 43, 500], [1090, 39, 300], [None, 1, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 31, 'period_days': 31}, [100, 1876]), ('normal control', {'usage': 1908, 'tiers': [[438, 11, 500], [1328, 8, 0], [None, 10, 0]], 'mode': 'volume', 'included': 0, 'active_days': 3, 'period_days': 31}, [0, 19080])], [('regression', {'usage': 520, 'tiers': [[335, 27, 0], [725, 26, 0], [None, 26, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('regression', {'usage': 2690, 'tiers': [[446, 10, 0], [1155, 29, 300], [None, 24, 0]], 'mode': 'volume', 'included': 250, 'active_days': 1, 'period_days': 31}, [8, 64368]), ('partial-repair probe', {'usage': 152, 'tiers': [[116, 12, 0], [540, 33, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 31}, [709, 0]), ('partial-repair probe', {'usage': 158, 'tiers': [[158, 6, 0], [662, 22, 0], [None, 15, 0]], 'mode': 'volume', 'included': 100, 'active_days': 10, 'period_days': 30}, [33, 750]), ('normal control', {'usage': 2285, 'tiers': [[395, 26, 500], [1142, 40, 0], [None, 24, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 13, 'period_days': 30}, [0, 68082]), ('normal control', {'usage': 340, 'tiers': [[481, 48, 0], [1132, 35, 300], [None, 6, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 30, 'period_days': 30}, [1000, 0]), ('normal control', {'usage': 392, 'tiers': [[392, 7, 0], [719, 26, 0], [None, 8, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 22, 'period_days': 30}, [0, 2744]), ('normal control', {'usage': 422, 'tiers': [[422, 16, 0], [1350, 20, 0], [None, 20, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 31, 'period_days': 31}, [1000, 0])], [('regression', {'usage': 152, 'tiers': [[116, 12, 0], [540, 33, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 31}, [709, 0]), ('regression', {'usage': 520, 'tiers': [[335, 27, 0], [725, 26, 0], [None, 26, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('partial-repair probe', {'usage': 335, 'tiers': [[335, 30, 500], [789, 38, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 4, 'period_days': 30}, [13, 10160]), ('partial-repair probe', {'usage': 1862, 'tiers': [[90, 6, 0], [1073, 39, 0], [None, 20, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 29, 'period_days': 31}, [93, 52797]), ('normal control', {'usage': 107, 'tiers': [[490, 47, 0], [1088, 17, 0], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 30, 'period_days': 30}, [1000, 0]), ('normal control', {'usage': 106, 'tiers': [[301, 42, 500], [930, 23, 300], [None, 29, 0]], 'mode': 'volume', 'included': 0, 'active_days': 24, 'period_days': 30}, [0, 4952]), ('normal control', {'usage': 143, 'tiers': [[493, 29, 0], [634, 36, 0], [None, 23, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 2, 'period_days': 31}, [0, 4147]), ('normal control', {'usage': 1774, 'tiers': [[155, 27, 0], [290, 6, 0], [None, 14, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 18, 'period_days': 31}, [0, 25771])], [('regression', {'usage': 158, 'tiers': [[158, 6, 0], [662, 22, 0], [None, 15, 0]], 'mode': 'volume', 'included': 100, 'active_days': 10, 'period_days': 30}, [33, 750]), ('regression', {'usage': 152, 'tiers': [[116, 12, 0], [540, 33, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 31}, [709, 0]), ('partial-repair probe', {'usage': 129, 'tiers': [[129, 9, 500], [845, 8, 300], [None, 18, 0]], 'mode': 'volume', 'included': 250, 'active_days': 29, 'period_days': 30}, [241, 0]), ('partial-repair probe', {'usage': 205, 'tiers': [[205, 36, 0], [1123, 17, 300], [None, 4, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 16, 'period_days': 31}, [516, 0]), ('normal control', {'usage': 310, 'tiers': [[237, 10, 0], [892, 12, 0], [None, 15, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 14, 'period_days': 31}, [0, 3246]), ('normal control', {'usage': 66, 'tiers': [[349, 9, 0], [1199, 11, 0], [None, 7, 0]], 'mode': 'volume', 'included': 0, 'active_days': 28, 'period_days': 30}, [0, 594]), ('normal control', {'usage': 344, 'tiers': [[344, 8, 0], [602, 31, 0], [None, 12, 0]], 'mode': 'volume', 'included': 0, 'active_days': 5, 'period_days': 30}, [0, 2752]), ('normal control', {'usage': 270, 'tiers': [[270, 9, 500], [388, 14, 0], [None, 27, 0]], 'mode': 'volume', 'included': 100, 'active_days': 30, 'period_days': 30}, [100, 2030])], [('regression', {'usage': 335, 'tiers': [[335, 30, 500], [789, 38, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 4, 'period_days': 30}, [13, 10160]), ('regression', {'usage': 158, 'tiers': [[158, 6, 0], [662, 22, 0], [None, 15, 0]], 'mode': 'volume', 'included': 100, 'active_days': 10, 'period_days': 30}, [33, 750]), ('partial-repair probe', {'usage': 286, 'tiers': [[286, 31, 500], [1169, 13, 0], [None, 22, 0]], 'mode': 'volume', 'included': 250, 'active_days': 30, 'period_days': 31}, [241, 1895]), ('partial-repair probe', {'usage': 22, 'tiers': [[271, 40, 0], [731, 39, 0], [None, 19, 0]], 'mode': 'graduated', 'included': 250, 'active_days': 23, 'period_days': 30}, [191, 0]), ('normal control', {'usage': 255, 'tiers': [[255, 8, 0], [492, 6, 0], [None, 26, 0]], 'mode': 'volume', 'included': 0, 'active_days': 17, 'period_days': 30}, [0, 2040]), ('normal control', {'usage': 187, 'tiers': [[153, 21, 500], [679, 13, 300], [None, 25, 0]], 'mode': 'volume', 'included': 0, 'active_days': 27, 'period_days': 31}, [0, 2731]), ('normal control', {'usage': 197, 'tiers': [[288, 8, 0], [714, 8, 0], [None, 2, 0]], 'mode': 'volume', 'included': 0, 'active_days': 24, 'period_days': 31}, [0, 1576]), ('normal control', {'usage': 317, 'tiers': [[317, 39, 500], [991, 40, 0], [None, 22, 0]], 'mode': 'volume', 'included': 0, 'active_days': 10, 'period_days': 31}, [0, 12863])]]
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 joining late in the period get the full monthly allowance.

Input {usage, tiers: [[up_to|None, unit_price, flat_fee]], mode volume|graduated, included, active_days, period_days}. Allowance = included*active_days//period_days. Billable = max(0, usage - allowance); zero billable costs 0. Volume: all billable units at the first tier with billable <= up_to, plus that tier's flat fee. Graduated: each tier bills the units falling in its range plus its flat fee if any unit falls in it. Return [allowance, cost].

split: open-access


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

N = 1
observations = []
def solve(x):
    allow = x['included'] * x['active_days'] // x['period_days']
    units = max(0, x['usage'] - allow)
    if units < 0:
        return [allow, 0]
    if x['mode'] == 'volume':
        for up, price, flat in x['tiers']:
            if up is None or units <= up:
                return [allow, units * price + flat]
    cost = 0
    prev = 0
    for up, price, flat in x['tiers']:
        top = units if up is None else min(units, up)
        if top > prev:
            cost += (top - prev) * price + flat
        if up is None or units <= up:
            break
        prev = up
    return [allow, cost]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'usage': 129, 'tiers': [[129, 9, 500], [845, 8, 300], [None, 18, 0]], 'mode': 'volume', 'included': 250, 'active_days': 29, 'period_days': 30}, [241, 0]), ('regression', {'usage': 59, 'tiers': [[302, 10, 500], [914, 15, 0], [None, 7, 0]], 'mode': 'volume', 'included': 250, 'active_days': 27, 'period_days': 30}, [225, 0]), ('partial-repair probe', {'usage': 175, 'tiers': [[281, 41, 500], [419, 21, 0], [None, 9, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 24, 'period_days': 30}, [800, 0]), ('partial-repair probe', {'usage': 116, 'tiers': [[56, 12, 500], [335, 14, 300], [None, 2, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('normal control', {'usage': 215, 'tiers': [[215, 32, 0], [1030, 38, 300], [None, 21, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 27, 'period_days': 30}, [900, 0]), ('normal control', {'usage': 2690, 'tiers': [[446, 10, 0], [1155, 29, 300], [None, 24, 0]], 'mode': 'volume', 'included': 250, 'active_days': 1, 'period_days': 31}, [8, 64368]), ('normal control', {'usage': 195, 'tiers': [[263, 41, 0], [444, 16, 300], [None, 18, 0]], 'mode': 'volume', 'included': 0, 'active_days': 28, 'period_days': 31}, [0, 7995]), ('normal control', {'usage': 149, 'tiers': [[96, 6, 0], [296, 34, 0], [None, 9, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 27, 'period_days': 30}, [0, 2378])], [('regression', {'usage': 59, 'tiers': [[302, 10, 500], [914, 15, 0], [None, 7, 0]], 'mode': 'volume', 'included': 250, 'active_days': 27, 'period_days': 30}, [225, 0]), ('regression', {'usage': 0, 'tiers': [[261, 18, 500], [782, 5, 300], [None, 25, 0]], 'mode': 'volume', 'included': 100, 'active_days': 14, 'period_days': 30}, [46, 0]), ('partial-repair probe', {'usage': 175, 'tiers': [[281, 41, 500], [419, 21, 0], [None, 9, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 24, 'period_days': 30}, [800, 0]), ('partial-repair probe', {'usage': 116, 'tiers': [[56, 12, 500], [335, 14, 300], [None, 2, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('normal control', {'usage': 520, 'tiers': [[335, 27, 0], [725, 26, 0], [None, 26, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('normal control', {'usage': 132, 'tiers': [[102, 43, 500], [1090, 39, 300], [None, 1, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 31, 'period_days': 31}, [100, 1876]), ('normal control', {'usage': 1908, 'tiers': [[438, 11, 500], [1328, 8, 0], [None, 10, 0]], 'mode': 'volume', 'included': 0, 'active_days': 3, 'period_days': 31}, [0, 19080]), ('normal control', {'usage': 152, 'tiers': [[116, 12, 0], [540, 33, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 31}, [709, 0])], [('regression', {'usage': 175, 'tiers': [[281, 41, 500], [419, 21, 0], [None, 9, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 24, 'period_days': 30}, [800, 0]), ('regression', {'usage': 0, 'tiers': [[261, 18, 500], [782, 5, 300], [None, 25, 0]], 'mode': 'volume', 'included': 100, 'active_days': 14, 'period_days': 30}, [46, 0]), ('partial-repair probe', {'usage': 71, 'tiers': [[446, 25, 500], [966, 37, 300], [None, 16, 0]], 'mode': 'volume', 'included': 100, 'active_days': 26, 'period_days': 31}, [83, 0]), ('partial-repair probe', {'usage': 175, 'tiers': [[164, 9, 500], [362, 20, 0], [None, 12, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 27, 'period_days': 30}, [900, 0]), ('normal control', {'usage': 158, 'tiers': [[158, 6, 0], [662, 22, 0], [None, 15, 0]], 'mode': 'volume', 'included': 100, 'active_days': 10, 'period_days': 30}, [33, 750]), ('normal control', {'usage': 47, 'tiers': [[264, 39, 0], [1255, 6, 0], [None, 12, 0]], 'mode': 'volume', 'included': 250, 'active_days': 24, 'period_days': 30}, [200, 0]), ('normal control', {'usage': 31, 'tiers': [[57, 14, 0], [997, 11, 300], [None, 7, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 3, 'period_days': 30}, [100, 0]), ('normal control', {'usage': 335, 'tiers': [[335, 30, 500], [789, 38, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 4, 'period_days': 30}, [13, 10160])], [('regression', {'usage': 116, 'tiers': [[56, 12, 500], [335, 14, 300], [None, 2, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('regression', {'usage': 175, 'tiers': [[281, 41, 500], [419, 21, 0], [None, 9, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 24, 'period_days': 30}, [800, 0]), ('partial-repair probe', {'usage': 121, 'tiers': [[92, 15, 500], [339, 6, 300], [None, 14, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 26, 'period_days': 31}, [838, 0]), ('partial-repair probe', {'usage': 60, 'tiers': [[268, 25, 500], [1138, 32, 0], [None, 4, 0]], 'mode': 'volume', 'included': 250, 'active_days': 10, 'period_days': 30}, [83, 0]), ('normal control', {'usage': 1862, 'tiers': [[90, 6, 0], [1073, 39, 0], [None, 20, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 29, 'period_days': 31}, [93, 52797]), ('normal control', {'usage': 2285, 'tiers': [[395, 26, 500], [1142, 40, 0], [None, 24, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 13, 'period_days': 30}, [0, 68082]), ('normal control', {'usage': 205, 'tiers': [[205, 36, 0], [1123, 17, 300], [None, 4, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 16, 'period_days': 31}, [516, 0]), ('normal control', {'usage': 286, 'tiers': [[286, 31, 500], [1169, 13, 0], [None, 22, 0]], 'mode': 'volume', 'included': 250, 'active_days': 30, 'period_days': 31}, [241, 1895])], [('regression', {'usage': 71, 'tiers': [[446, 25, 500], [966, 37, 300], [None, 16, 0]], 'mode': 'volume', 'included': 100, 'active_days': 26, 'period_days': 31}, [83, 0]), ('regression', {'usage': 116, 'tiers': [[56, 12, 500], [335, 14, 300], [None, 2, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('partial-repair probe', {'usage': 480, 'tiers': [[297, 7, 500], [709, 18, 0], [None, 22, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 18, 'period_days': 30}, [600, 0]), ('partial-repair probe', {'usage': 74, 'tiers': [[471, 38, 500], [754, 8, 300], [None, 3, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 16, 'period_days': 30}, [533, 0]), ('normal control', {'usage': 22, 'tiers': [[271, 40, 0], [731, 39, 0], [None, 19, 0]], 'mode': 'graduated', 'included': 250, 'active_days': 23, 'period_days': 30}, [191, 0]), ('normal control', {'usage': 1383, 'tiers': [[285, 7, 0], [550, 30, 0], [None, 24, 0]], 'mode': 'graduated', 'included': 250, 'active_days': 24, 'period_days': 31}, [193, 25305]), ('normal control', {'usage': 77, 'tiers': [[278, 41, 0], [1269, 3, 0], [None, 6, 0]], 'mode': 'volume', 'included': 100, 'active_days': 13, 'period_days': 30}, [43, 1394]), ('normal control', {'usage': 340, 'tiers': [[481, 48, 0], [1132, 35, 300], [None, 6, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 30, 'period_days': 30}, [1000, 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):
    allow = x['included'] * x['active_days'] // x['period_days']
    units = max(0, x['usage'] - allow)
    if x['usage'] == 0:
        return [allow, 0]
    if x['mode'] == 'volume':
        for up, price, flat in x['tiers']:
            if up is None or units <= up:
                return [allow, units * price + flat]
    cost = 0
    prev = 0
    for up, price, flat in x['tiers']:
        top = units if up is None else min(units, up)
        if top > prev:
            cost += (top - prev) * price + flat
        if up is None or units <= up:
            break
        prev = up
    return [allow, cost]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'usage': 129, 'tiers': [[129, 9, 500], [845, 8, 300], [None, 18, 0]], 'mode': 'volume', 'included': 250, 'active_days': 29, 'period_days': 30}, [241, 0]), ('regression', {'usage': 59, 'tiers': [[302, 10, 500], [914, 15, 0], [None, 7, 0]], 'mode': 'volume', 'included': 250, 'active_days': 27, 'period_days': 30}, [225, 0]), ('partial-repair probe', {'usage': 175, 'tiers': [[281, 41, 500], [419, 21, 0], [None, 9, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 24, 'period_days': 30}, [800, 0]), ('partial-repair probe', {'usage': 116, 'tiers': [[56, 12, 500], [335, 14, 300], [None, 2, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('normal control', {'usage': 215, 'tiers': [[215, 32, 0], [1030, 38, 300], [None, 21, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 27, 'period_days': 30}, [900, 0]), ('normal control', {'usage': 2690, 'tiers': [[446, 10, 0], [1155, 29, 300], [None, 24, 0]], 'mode': 'volume', 'included': 250, 'active_days': 1, 'period_days': 31}, [8, 64368]), ('normal control', {'usage': 195, 'tiers': [[263, 41, 0], [444, 16, 300], [None, 18, 0]], 'mode': 'volume', 'included': 0, 'active_days': 28, 'period_days': 31}, [0, 7995]), ('normal control', {'usage': 149, 'tiers': [[96, 6, 0], [296, 34, 0], [None, 9, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 27, 'period_days': 30}, [0, 2378])], [('regression', {'usage': 59, 'tiers': [[302, 10, 500], [914, 15, 0], [None, 7, 0]], 'mode': 'volume', 'included': 250, 'active_days': 27, 'period_days': 30}, [225, 0]), ('regression', {'usage': 0, 'tiers': [[261, 18, 500], [782, 5, 300], [None, 25, 0]], 'mode': 'volume', 'included': 100, 'active_days': 14, 'period_days': 30}, [46, 0]), ('partial-repair probe', {'usage': 175, 'tiers': [[281, 41, 500], [419, 21, 0], [None, 9, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 24, 'period_days': 30}, [800, 0]), ('partial-repair probe', {'usage': 116, 'tiers': [[56, 12, 500], [335, 14, 300], [None, 2, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('normal control', {'usage': 520, 'tiers': [[335, 27, 0], [725, 26, 0], [None, 26, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('normal control', {'usage': 132, 'tiers': [[102, 43, 500], [1090, 39, 300], [None, 1, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 31, 'period_days': 31}, [100, 1876]), ('normal control', {'usage': 1908, 'tiers': [[438, 11, 500], [1328, 8, 0], [None, 10, 0]], 'mode': 'volume', 'included': 0, 'active_days': 3, 'period_days': 31}, [0, 19080]), ('normal control', {'usage': 152, 'tiers': [[116, 12, 0], [540, 33, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 22, 'period_days': 31}, [709, 0])], [('regression', {'usage': 175, 'tiers': [[281, 41, 500], [419, 21, 0], [None, 9, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 24, 'period_days': 30}, [800, 0]), ('regression', {'usage': 0, 'tiers': [[261, 18, 500], [782, 5, 300], [None, 25, 0]], 'mode': 'volume', 'included': 100, 'active_days': 14, 'period_days': 30}, [46, 0]), ('partial-repair probe', {'usage': 71, 'tiers': [[446, 25, 500], [966, 37, 300], [None, 16, 0]], 'mode': 'volume', 'included': 100, 'active_days': 26, 'period_days': 31}, [83, 0]), ('partial-repair probe', {'usage': 175, 'tiers': [[164, 9, 500], [362, 20, 0], [None, 12, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 27, 'period_days': 30}, [900, 0]), ('normal control', {'usage': 158, 'tiers': [[158, 6, 0], [662, 22, 0], [None, 15, 0]], 'mode': 'volume', 'included': 100, 'active_days': 10, 'period_days': 30}, [33, 750]), ('normal control', {'usage': 47, 'tiers': [[264, 39, 0], [1255, 6, 0], [None, 12, 0]], 'mode': 'volume', 'included': 250, 'active_days': 24, 'period_days': 30}, [200, 0]), ('normal control', {'usage': 31, 'tiers': [[57, 14, 0], [997, 11, 300], [None, 7, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 3, 'period_days': 30}, [100, 0]), ('normal control', {'usage': 335, 'tiers': [[335, 30, 500], [789, 38, 300], [None, 29, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 4, 'period_days': 30}, [13, 10160])], [('regression', {'usage': 116, 'tiers': [[56, 12, 500], [335, 14, 300], [None, 2, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('regression', {'usage': 175, 'tiers': [[281, 41, 500], [419, 21, 0], [None, 9, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 24, 'period_days': 30}, [800, 0]), ('partial-repair probe', {'usage': 121, 'tiers': [[92, 15, 500], [339, 6, 300], [None, 14, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 26, 'period_days': 31}, [838, 0]), ('partial-repair probe', {'usage': 60, 'tiers': [[268, 25, 500], [1138, 32, 0], [None, 4, 0]], 'mode': 'volume', 'included': 250, 'active_days': 10, 'period_days': 30}, [83, 0]), ('normal control', {'usage': 1862, 'tiers': [[90, 6, 0], [1073, 39, 0], [None, 20, 0]], 'mode': 'graduated', 'included': 100, 'active_days': 29, 'period_days': 31}, [93, 52797]), ('normal control', {'usage': 2285, 'tiers': [[395, 26, 500], [1142, 40, 0], [None, 24, 0]], 'mode': 'graduated', 'included': 0, 'active_days': 13, 'period_days': 30}, [0, 68082]), ('normal control', {'usage': 205, 'tiers': [[205, 36, 0], [1123, 17, 300], [None, 4, 0]], 'mode': 'graduated', 'included': 1000, 'active_days': 16, 'period_days': 31}, [516, 0]), ('normal control', {'usage': 286, 'tiers': [[286, 31, 500], [1169, 13, 0], [None, 22, 0]], 'mode': 'volume', 'included': 250, 'active_days': 30, 'period_days': 31}, [241, 1895])], [('regression', {'usage': 71, 'tiers': [[446, 25, 500], [966, 37, 300], [None, 16, 0]], 'mode': 'volume', 'included': 100, 'active_days': 26, 'period_days': 31}, [83, 0]), ('regression', {'usage': 116, 'tiers': [[56, 12, 500], [335, 14, 300], [None, 2, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 22, 'period_days': 30}, [733, 0]), ('partial-repair probe', {'usage': 480, 'tiers': [[297, 7, 500], [709, 18, 0], [None, 22, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 18, 'period_days': 30}, [600, 0]), ('partial-repair probe', {'usage': 74, 'tiers': [[471, 38, 500], [754, 8, 300], [None, 3, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 16, 'period_days': 30}, [533, 0]), ('normal control', {'usage': 22, 'tiers': [[271, 40, 0], [731, 39, 0], [None, 19, 0]], 'mode': 'graduated', 'included': 250, 'active_days': 23, 'period_days': 30}, [191, 0]), ('normal control', {'usage': 1383, 'tiers': [[285, 7, 0], [550, 30, 0], [None, 24, 0]], 'mode': 'graduated', 'included': 250, 'active_days': 24, 'period_days': 31}, [193, 25305]), ('normal control', {'usage': 77, 'tiers': [[278, 41, 0], [1269, 3, 0], [None, 6, 0]], 'mode': 'volume', 'included': 100, 'active_days': 13, 'period_days': 30}, [43, 1394]), ('normal control', {'usage': 340, 'tiers': [[481, 48, 0], [1132, 35, 300], [None, 6, 0]], 'mode': 'volume', 'included': 1000, 'active_days': 30, 'period_days': 30}, [1000, 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 whose usage is fully covered by the allowance are billed a tier flat fee.

Input {usage, tiers: [[up_to|None, unit_price, flat_fee]], mode volume|graduated, included, active_days, period_days}. Allowance = included*active_days//period_days. Billable = max(0, usage - allowance); zero billable costs 0. Volume: all billable units at the first tier with billable <= up_to, plus that tier's flat fee. Graduated: each tier bills the units falling in its range plus its flat fee if any unit falls in it. Return [allowance, cost].

split: open-access