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

N = 1
observations = []
def solve(x):
    avail = max(0, x['gross'] - x['taxes'])
    taken = {}
    arrears = {}
    for name, amt, pri, partial in sorted(x['deductions'], key=lambda d: d[2]):
        due = amt
        if due <= avail:
            take = due
        elif partial:
            take = avail
        else:
            take = 0
        avail -= take
        taken[name] = take
        if due - take > 0:
            arrears[name] = due - take
    return {'net': avail, 'taken': taken, 'arrears': arrears}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'gross': 134653, 'taxes': 27454, 'deductions': [['parking', 26770, 3, False], ['loan', 12315, 2, False], ['union', 3364, 1, False], ['support', 11662, 3, True]], 'arrears': {'parking': 10177, 'loan': 15972, 'support': 6216}}, {'net': 20723, 'taken': {'union': 3364, 'loan': 28287, 'parking': 36947, 'support': 17878}, 'arrears': {}}), ('regression', {'gross': 138327, 'taxes': 24358, 'deductions': [['loan', 12004, 3, True], ['union', 4120, 2, True], ['medical', 35331, 3, False], ['parking', 697, 1, True], ['support', 15166, 2, True]], 'arrears': {'loan': 5341, 'union': 19151, 'support': 16971}}, {'net': 5188, 'taken': {'parking': 697, 'union': 23271, 'support': 32137, 'loan': 17345, 'medical': 35331}, 'arrears': {}}), ('partial-repair probe', {'gross': 102757, 'taxes': 16339, 'deductions': [['medical', 20312, 1, False]], 'arrears': {'medical': 12422}}, {'net': 53684, 'taken': {'medical': 32734}, 'arrears': {}}), ('partial-repair probe', {'gross': 6593, 'taxes': 872, 'deductions': [['parking', 5312, 3, False], ['union', 4345, 3, False], ['medical', 14587, 3, False], ['loan', 24540, 2, True], ['support', 7319, 3, True]], 'arrears': {'parking': 388, 'medical': 17218}}, {'net': 0, 'taken': {'loan': 5721, 'parking': 0, 'union': 0, 'medical': 0, 'support': 0}, 'arrears': {'loan': 18819, 'parking': 5700, 'union': 4345, 'medical': 31805, 'support': 7319}}), ('normal control', {'gross': 3515, 'taxes': 674, 'deductions': [['parking', 20962, 1, False], ['union', 18985, 3, True], ['loan', 33701, 1, True]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 0, 'loan': 2841, 'union': 0}, 'arrears': {'parking': 20962, 'loan': 30860, 'union': 18985}}), ('normal control', {'gross': 106436, 'taxes': 29897, 'deductions': [['parking', 39254, 1, True]], 'arrears': {}}, {'net': 37285, 'taken': {'parking': 39254}, 'arrears': {}}), ('normal control', {'gross': 32919, 'taxes': 7584, 'deductions': [['parking', 36420, 3, True], ['charity', 19693, 3, False], ['loan', 12305, 1, True], ['medical', 7400, 2, True], ['support', 39536, 2, True]], 'arrears': {}}, {'net': 0, 'taken': {'loan': 12305, 'medical': 7400, 'support': 5630, 'parking': 0, 'charity': 0}, 'arrears': {'support': 33906, 'parking': 36420, 'charity': 19693}}), ('normal control', {'gross': 42097, 'taxes': 527, 'deductions': [['medical', 23283, 1, False], ['union', 33588, 1, False]], 'arrears': {}}, {'net': 18287, 'taken': {'medical': 23283, 'union': 0}, 'arrears': {'union': 33588}})], [('regression', {'gross': 138327, 'taxes': 24358, 'deductions': [['loan', 12004, 3, True], ['union', 4120, 2, True], ['medical', 35331, 3, False], ['parking', 697, 1, True], ['support', 15166, 2, True]], 'arrears': {'loan': 5341, 'union': 19151, 'support': 16971}}, {'net': 5188, 'taken': {'parking': 697, 'union': 23271, 'support': 32137, 'loan': 17345, 'medical': 35331}, 'arrears': {}}), ('regression', {'gross': 102757, 'taxes': 16339, 'deductions': [['medical', 20312, 1, False]], 'arrears': {'medical': 12422}}, {'net': 53684, 'taken': {'medical': 32734}, 'arrears': {}}), ('partial-repair probe', {'gross': 6593, 'taxes': 872, 'deductions': [['parking', 5312, 3, False], ['union', 4345, 3, False], ['medical', 14587, 3, False], ['loan', 24540, 2, True], ['support', 7319, 3, True]], 'arrears': {'parking': 388, 'medical': 17218}}, {'net': 0, 'taken': {'loan': 5721, 'parking': 0, 'union': 0, 'medical': 0, 'support': 0}, 'arrears': {'loan': 18819, 'parking': 5700, 'union': 4345, 'medical': 31805, 'support': 7319}}), ('partial-repair probe', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('normal control', {'gross': 123929, 'taxes': 27738, 'deductions': [['parking', 24255, 3, False]], 'arrears': {}}, {'net': 71936, 'taken': {'parking': 24255}, 'arrears': {}}), ('normal control', {'gross': 60703, 'taxes': 11543, 'deductions': [['support', 14720, 2, False], ['loan', 10777, 3, False], ['medical', 34788, 2, True]], 'arrears': {}}, {'net': 0, 'taken': {'support': 14720, 'medical': 34440, 'loan': 0}, 'arrears': {'medical': 348, 'loan': 10777}}), ('normal control', {'gross': 121022, 'taxes': 14438, 'deductions': [['charity', 24183, 2, False], ['loan', 4420, 2, True]], 'arrears': {}}, {'net': 77981, 'taken': {'charity': 24183, 'loan': 4420}, 'arrears': {}}), ('normal control', {'gross': 131039, 'taxes': 2132, 'deductions': [['charity', 22460, 2, True], ['loan', 32332, 3, False]], 'arrears': {}}, {'net': 74115, 'taken': {'charity': 22460, 'loan': 32332}, 'arrears': {}})], [('regression', {'gross': 102757, 'taxes': 16339, 'deductions': [['medical', 20312, 1, False]], 'arrears': {'medical': 12422}}, {'net': 53684, 'taken': {'medical': 32734}, 'arrears': {}}), ('regression', {'gross': 6593, 'taxes': 872, 'deductions': [['parking', 5312, 3, False], ['union', 4345, 3, False], ['medical', 14587, 3, False], ['loan', 24540, 2, True], ['support', 7319, 3, True]], 'arrears': {'parking': 388, 'medical': 17218}}, {'net': 0, 'taken': {'loan': 5721, 'parking': 0, 'union': 0, 'medical': 0, 'support': 0}, 'arrears': {'loan': 18819, 'parking': 5700, 'union': 4345, 'medical': 31805, 'support': 7319}}), ('partial-repair probe', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('partial-repair probe', {'gross': 132719, 'taxes': 9617, 'deductions': [['charity', 21694, 1, False], ['loan', 11829, 1, True], ['parking', 18178, 3, True], ['union', 1782, 3, True]], 'arrears': {'loan': 14963, 'parking': 3722, 'union': 3447}}, {'net': 47487, 'taken': {'charity': 21694, 'loan': 26792, 'parking': 21900, 'union': 5229}, 'arrears': {}}), ('normal control', {'gross': 93080, 'taxes': 11717, 'deductions': [['medical', 15825, 3, False]], 'arrears': {}}, {'net': 65538, 'taken': {'medical': 15825}, 'arrears': {}}), ('normal control', {'gross': 58263, 'taxes': 3517, 'deductions': [['loan', 34211, 3, False], ['support', 2445, 1, True], ['parking', 6724, 3, False]], 'arrears': {}}, {'net': 11366, 'taken': {'support': 2445, 'loan': 34211, 'parking': 6724}, 'arrears': {}}), ('normal control', {'gross': 111695, 'taxes': 33699, 'deductions': [['loan', 33780, 3, True], ['parking', 23393, 3, False], ['support', 21898, 2, False], ['charity', 33451, 1, False], ['union', 625, 2, True]], 'arrears': {}}, {'net': 0, 'taken': {'charity': 33451, 'support': 21898, 'union': 625, 'loan': 22022, 'parking': 0}, 'arrears': {'loan': 11758, 'parking': 23393}}), ('normal control', {'gross': 26399, 'taxes': 6362, 'deductions': [['charity', 39113, 3, True], ['parking', 3658, 2, True], ['loan', 30168, 3, True]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 3658, 'charity': 16379, 'loan': 0}, 'arrears': {'charity': 22734, 'loan': 30168}})], [('regression', {'gross': 6593, 'taxes': 872, 'deductions': [['parking', 5312, 3, False], ['union', 4345, 3, False], ['medical', 14587, 3, False], ['loan', 24540, 2, True], ['support', 7319, 3, True]], 'arrears': {'parking': 388, 'medical': 17218}}, {'net': 0, 'taken': {'loan': 5721, 'parking': 0, 'union': 0, 'medical': 0, 'support': 0}, 'arrears': {'loan': 18819, 'parking': 5700, 'union': 4345, 'medical': 31805, 'support': 7319}}), ('regression', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('partial-repair probe', {'gross': 91681, 'taxes': 9563, 'deductions': [['union', 25760, 2, False], ['parking', 28566, 1, False], ['support', 8036, 1, False], ['medical', 15864, 1, False], ['charity', 5966, 2, True]], 'arrears': {'parking': 5836, 'support': 18944, 'charity': 5663}}, {'net': 0, 'taken': {'parking': 34402, 'support': 26980, 'medical': 15864, 'union': 0, 'charity': 4872}, 'arrears': {'union': 25760, 'charity': 6757}}), ('partial-repair probe', {'gross': 13592, 'taxes': 3481, 'deductions': [['union', 941, 2, False], ['medical', 37170, 2, False], ['charity', 11425, 3, True], ['support', 36894, 2, False], ['parking', 10277, 3, True]], 'arrears': {'medical': 2360}}, {'net': 0, 'taken': {'union': 941, 'medical': 0, 'support': 0, 'charity': 9170, 'parking': 0}, 'arrears': {'medical': 39530, 'support': 36894, 'charity': 2255, 'parking': 10277}}), ('normal control', {'gross': 77880, 'taxes': 19549, 'deductions': [['loan', 3379, 3, True]], 'arrears': {}}, {'net': 54952, 'taken': {'loan': 3379}, 'arrears': {}}), ('normal control', {'gross': 21943, 'taxes': 4843, 'deductions': [['parking', 33670, 3, False], ['charity', 24679, 3, True]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 0, 'charity': 17100}, 'arrears': {'parking': 33670, 'charity': 7579}}), ('normal control', {'gross': 46334, 'taxes': 3174, 'deductions': [['loan', 24433, 2, False], ['parking', 37324, 2, True], ['medical', 22420, 1, True], ['union', 9797, 1, True], ['charity', 28327, 2, False]], 'arrears': {}}, {'net': 0, 'taken': {'medical': 22420, 'union': 9797, 'loan': 0, 'parking': 10943, 'charity': 0}, 'arrears': {'loan': 24433, 'parking': 26381, 'charity': 28327}}), ('normal control', {'gross': 134030, 'taxes': 44349, 'deductions': [['support', 11357, 2, False]], 'arrears': {}}, {'net': 78324, 'taken': {'support': 11357}, 'arrears': {}})], [('regression', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('regression', {'gross': 132719, 'taxes': 9617, 'deductions': [['charity', 21694, 1, False], ['loan', 11829, 1, True], ['parking', 18178, 3, True], ['union', 1782, 3, True]], 'arrears': {'loan': 14963, 'parking': 3722, 'union': 3447}}, {'net': 47487, 'taken': {'charity': 21694, 'loan': 26792, 'parking': 21900, 'union': 5229}, 'arrears': {}}), ('partial-repair probe', {'gross': 71403, 'taxes': 15182, 'deductions': [['support', 561, 2, True], ['charity', 7949, 1, True], ['parking', 31674, 1, False], ['medical', 37176, 1, False]], 'arrears': {'support': 13700}}, {'net': 2337, 'taken': {'charity': 7949, 'parking': 31674, 'medical': 0, 'support': 14261}, 'arrears': {'medical': 37176}}), ('partial-repair probe', {'gross': 13589, 'taxes': 714, 'deductions': [['charity', 299, 3, True]], 'arrears': {'charity': 4459}}, {'net': 8117, 'taken': {'charity': 4758}, 'arrears': {}}), ('normal control', {'gross': 109707, 'taxes': 1323, 'deductions': [['union', 39866, 3, True], ['loan', 38020, 1, True], ['support', 11077, 3, False]], 'arrears': {}}, {'net': 19421, 'taken': {'loan': 38020, 'union': 39866, 'support': 11077}, 'arrears': {}}), ('normal control', {'gross': 56861, 'taxes': 5156, 'deductions': [['loan', 39411, 2, True], ['support', 27265, 1, False], ['union', 39662, 3, True]], 'arrears': {}}, {'net': 0, 'taken': {'support': 27265, 'loan': 24440, 'union': 0}, 'arrears': {'loan': 14971, 'union': 39662}}), ('normal control', {'gross': 134888, 'taxes': 11264, 'deductions': [['union', 12074, 3, True], ['charity', 22324, 1, True], ['parking', 15797, 3, True], ['support', 20440, 2, False], ['loan', 14221, 3, True]], 'arrears': {}}, {'net': 38768, 'taken': {'charity': 22324, 'support': 20440, 'union': 12074, 'parking': 15797, 'loan': 14221}, 'arrears': {}}), ('normal control', {'gross': 52145, 'taxes': 5283, 'deductions': [['charity', 22477, 3, False], ['support', 31082, 1, True]], 'arrears': {}}, {'net': 15780, 'taken': {'support': 31082, 'charity': 0}, 'arrears': {'charity': 22477}})]]
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):
    avail = max(0, x['gross'] - x['taxes'])
    taken = {}
    arrears = {}
    for name, amt, pri, partial in sorted(x['deductions'], key=lambda d: d[2]):
        due = max(amt, x['arrears'].get(name, 0))
        if due <= avail:
            take = due
        elif partial:
            take = avail
        else:
            take = 0
        avail -= take
        taken[name] = take
        if due - take > 0:
            arrears[name] = due - take
    return {'net': avail, 'taken': taken, 'arrears': arrears}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'gross': 134653, 'taxes': 27454, 'deductions': [['parking', 26770, 3, False], ['loan', 12315, 2, False], ['union', 3364, 1, False], ['support', 11662, 3, True]], 'arrears': {'parking': 10177, 'loan': 15972, 'support': 6216}}, {'net': 20723, 'taken': {'union': 3364, 'loan': 28287, 'parking': 36947, 'support': 17878}, 'arrears': {}}), ('regression', {'gross': 138327, 'taxes': 24358, 'deductions': [['loan', 12004, 3, True], ['union', 4120, 2, True], ['medical', 35331, 3, False], ['parking', 697, 1, True], ['support', 15166, 2, True]], 'arrears': {'loan': 5341, 'union': 19151, 'support': 16971}}, {'net': 5188, 'taken': {'parking': 697, 'union': 23271, 'support': 32137, 'loan': 17345, 'medical': 35331}, 'arrears': {}}), ('partial-repair probe', {'gross': 102757, 'taxes': 16339, 'deductions': [['medical', 20312, 1, False]], 'arrears': {'medical': 12422}}, {'net': 53684, 'taken': {'medical': 32734}, 'arrears': {}}), ('partial-repair probe', {'gross': 6593, 'taxes': 872, 'deductions': [['parking', 5312, 3, False], ['union', 4345, 3, False], ['medical', 14587, 3, False], ['loan', 24540, 2, True], ['support', 7319, 3, True]], 'arrears': {'parking': 388, 'medical': 17218}}, {'net': 0, 'taken': {'loan': 5721, 'parking': 0, 'union': 0, 'medical': 0, 'support': 0}, 'arrears': {'loan': 18819, 'parking': 5700, 'union': 4345, 'medical': 31805, 'support': 7319}}), ('normal control', {'gross': 3515, 'taxes': 674, 'deductions': [['parking', 20962, 1, False], ['union', 18985, 3, True], ['loan', 33701, 1, True]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 0, 'loan': 2841, 'union': 0}, 'arrears': {'parking': 20962, 'loan': 30860, 'union': 18985}}), ('normal control', {'gross': 106436, 'taxes': 29897, 'deductions': [['parking', 39254, 1, True]], 'arrears': {}}, {'net': 37285, 'taken': {'parking': 39254}, 'arrears': {}}), ('normal control', {'gross': 32919, 'taxes': 7584, 'deductions': [['parking', 36420, 3, True], ['charity', 19693, 3, False], ['loan', 12305, 1, True], ['medical', 7400, 2, True], ['support', 39536, 2, True]], 'arrears': {}}, {'net': 0, 'taken': {'loan': 12305, 'medical': 7400, 'support': 5630, 'parking': 0, 'charity': 0}, 'arrears': {'support': 33906, 'parking': 36420, 'charity': 19693}}), ('normal control', {'gross': 42097, 'taxes': 527, 'deductions': [['medical', 23283, 1, False], ['union', 33588, 1, False]], 'arrears': {}}, {'net': 18287, 'taken': {'medical': 23283, 'union': 0}, 'arrears': {'union': 33588}})], [('regression', {'gross': 138327, 'taxes': 24358, 'deductions': [['loan', 12004, 3, True], ['union', 4120, 2, True], ['medical', 35331, 3, False], ['parking', 697, 1, True], ['support', 15166, 2, True]], 'arrears': {'loan': 5341, 'union': 19151, 'support': 16971}}, {'net': 5188, 'taken': {'parking': 697, 'union': 23271, 'support': 32137, 'loan': 17345, 'medical': 35331}, 'arrears': {}}), ('regression', {'gross': 102757, 'taxes': 16339, 'deductions': [['medical', 20312, 1, False]], 'arrears': {'medical': 12422}}, {'net': 53684, 'taken': {'medical': 32734}, 'arrears': {}}), ('partial-repair probe', {'gross': 6593, 'taxes': 872, 'deductions': [['parking', 5312, 3, False], ['union', 4345, 3, False], ['medical', 14587, 3, False], ['loan', 24540, 2, True], ['support', 7319, 3, True]], 'arrears': {'parking': 388, 'medical': 17218}}, {'net': 0, 'taken': {'loan': 5721, 'parking': 0, 'union': 0, 'medical': 0, 'support': 0}, 'arrears': {'loan': 18819, 'parking': 5700, 'union': 4345, 'medical': 31805, 'support': 7319}}), ('partial-repair probe', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('normal control', {'gross': 123929, 'taxes': 27738, 'deductions': [['parking', 24255, 3, False]], 'arrears': {}}, {'net': 71936, 'taken': {'parking': 24255}, 'arrears': {}}), ('normal control', {'gross': 60703, 'taxes': 11543, 'deductions': [['support', 14720, 2, False], ['loan', 10777, 3, False], ['medical', 34788, 2, True]], 'arrears': {}}, {'net': 0, 'taken': {'support': 14720, 'medical': 34440, 'loan': 0}, 'arrears': {'medical': 348, 'loan': 10777}}), ('normal control', {'gross': 121022, 'taxes': 14438, 'deductions': [['charity', 24183, 2, False], ['loan', 4420, 2, True]], 'arrears': {}}, {'net': 77981, 'taken': {'charity': 24183, 'loan': 4420}, 'arrears': {}}), ('normal control', {'gross': 131039, 'taxes': 2132, 'deductions': [['charity', 22460, 2, True], ['loan', 32332, 3, False]], 'arrears': {}}, {'net': 74115, 'taken': {'charity': 22460, 'loan': 32332}, 'arrears': {}})], [('regression', {'gross': 102757, 'taxes': 16339, 'deductions': [['medical', 20312, 1, False]], 'arrears': {'medical': 12422}}, {'net': 53684, 'taken': {'medical': 32734}, 'arrears': {}}), ('regression', {'gross': 6593, 'taxes': 872, 'deductions': [['parking', 5312, 3, False], ['union', 4345, 3, False], ['medical', 14587, 3, False], ['loan', 24540, 2, True], ['support', 7319, 3, True]], 'arrears': {'parking': 388, 'medical': 17218}}, {'net': 0, 'taken': {'loan': 5721, 'parking': 0, 'union': 0, 'medical': 0, 'support': 0}, 'arrears': {'loan': 18819, 'parking': 5700, 'union': 4345, 'medical': 31805, 'support': 7319}}), ('partial-repair probe', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('partial-repair probe', {'gross': 132719, 'taxes': 9617, 'deductions': [['charity', 21694, 1, False], ['loan', 11829, 1, True], ['parking', 18178, 3, True], ['union', 1782, 3, True]], 'arrears': {'loan': 14963, 'parking': 3722, 'union': 3447}}, {'net': 47487, 'taken': {'charity': 21694, 'loan': 26792, 'parking': 21900, 'union': 5229}, 'arrears': {}}), ('normal control', {'gross': 93080, 'taxes': 11717, 'deductions': [['medical', 15825, 3, False]], 'arrears': {}}, {'net': 65538, 'taken': {'medical': 15825}, 'arrears': {}}), ('normal control', {'gross': 58263, 'taxes': 3517, 'deductions': [['loan', 34211, 3, False], ['support', 2445, 1, True], ['parking', 6724, 3, False]], 'arrears': {}}, {'net': 11366, 'taken': {'support': 2445, 'loan': 34211, 'parking': 6724}, 'arrears': {}}), ('normal control', {'gross': 111695, 'taxes': 33699, 'deductions': [['loan', 33780, 3, True], ['parking', 23393, 3, False], ['support', 21898, 2, False], ['charity', 33451, 1, False], ['union', 625, 2, True]], 'arrears': {}}, {'net': 0, 'taken': {'charity': 33451, 'support': 21898, 'union': 625, 'loan': 22022, 'parking': 0}, 'arrears': {'loan': 11758, 'parking': 23393}}), ('normal control', {'gross': 26399, 'taxes': 6362, 'deductions': [['charity', 39113, 3, True], ['parking', 3658, 2, True], ['loan', 30168, 3, True]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 3658, 'charity': 16379, 'loan': 0}, 'arrears': {'charity': 22734, 'loan': 30168}})], [('regression', {'gross': 6593, 'taxes': 872, 'deductions': [['parking', 5312, 3, False], ['union', 4345, 3, False], ['medical', 14587, 3, False], ['loan', 24540, 2, True], ['support', 7319, 3, True]], 'arrears': {'parking': 388, 'medical': 17218}}, {'net': 0, 'taken': {'loan': 5721, 'parking': 0, 'union': 0, 'medical': 0, 'support': 0}, 'arrears': {'loan': 18819, 'parking': 5700, 'union': 4345, 'medical': 31805, 'support': 7319}}), ('regression', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('partial-repair probe', {'gross': 91681, 'taxes': 9563, 'deductions': [['union', 25760, 2, False], ['parking', 28566, 1, False], ['support', 8036, 1, False], ['medical', 15864, 1, False], ['charity', 5966, 2, True]], 'arrears': {'parking': 5836, 'support': 18944, 'charity': 5663}}, {'net': 0, 'taken': {'parking': 34402, 'support': 26980, 'medical': 15864, 'union': 0, 'charity': 4872}, 'arrears': {'union': 25760, 'charity': 6757}}), ('partial-repair probe', {'gross': 13592, 'taxes': 3481, 'deductions': [['union', 941, 2, False], ['medical', 37170, 2, False], ['charity', 11425, 3, True], ['support', 36894, 2, False], ['parking', 10277, 3, True]], 'arrears': {'medical': 2360}}, {'net': 0, 'taken': {'union': 941, 'medical': 0, 'support': 0, 'charity': 9170, 'parking': 0}, 'arrears': {'medical': 39530, 'support': 36894, 'charity': 2255, 'parking': 10277}}), ('normal control', {'gross': 77880, 'taxes': 19549, 'deductions': [['loan', 3379, 3, True]], 'arrears': {}}, {'net': 54952, 'taken': {'loan': 3379}, 'arrears': {}}), ('normal control', {'gross': 21943, 'taxes': 4843, 'deductions': [['parking', 33670, 3, False], ['charity', 24679, 3, True]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 0, 'charity': 17100}, 'arrears': {'parking': 33670, 'charity': 7579}}), ('normal control', {'gross': 46334, 'taxes': 3174, 'deductions': [['loan', 24433, 2, False], ['parking', 37324, 2, True], ['medical', 22420, 1, True], ['union', 9797, 1, True], ['charity', 28327, 2, False]], 'arrears': {}}, {'net': 0, 'taken': {'medical': 22420, 'union': 9797, 'loan': 0, 'parking': 10943, 'charity': 0}, 'arrears': {'loan': 24433, 'parking': 26381, 'charity': 28327}}), ('normal control', {'gross': 134030, 'taxes': 44349, 'deductions': [['support', 11357, 2, False]], 'arrears': {}}, {'net': 78324, 'taken': {'support': 11357}, 'arrears': {}})], [('regression', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('regression', {'gross': 132719, 'taxes': 9617, 'deductions': [['charity', 21694, 1, False], ['loan', 11829, 1, True], ['parking', 18178, 3, True], ['union', 1782, 3, True]], 'arrears': {'loan': 14963, 'parking': 3722, 'union': 3447}}, {'net': 47487, 'taken': {'charity': 21694, 'loan': 26792, 'parking': 21900, 'union': 5229}, 'arrears': {}}), ('partial-repair probe', {'gross': 71403, 'taxes': 15182, 'deductions': [['support', 561, 2, True], ['charity', 7949, 1, True], ['parking', 31674, 1, False], ['medical', 37176, 1, False]], 'arrears': {'support': 13700}}, {'net': 2337, 'taken': {'charity': 7949, 'parking': 31674, 'medical': 0, 'support': 14261}, 'arrears': {'medical': 37176}}), ('partial-repair probe', {'gross': 13589, 'taxes': 714, 'deductions': [['charity', 299, 3, True]], 'arrears': {'charity': 4459}}, {'net': 8117, 'taken': {'charity': 4758}, 'arrears': {}}), ('normal control', {'gross': 109707, 'taxes': 1323, 'deductions': [['union', 39866, 3, True], ['loan', 38020, 1, True], ['support', 11077, 3, False]], 'arrears': {}}, {'net': 19421, 'taken': {'loan': 38020, 'union': 39866, 'support': 11077}, 'arrears': {}}), ('normal control', {'gross': 56861, 'taxes': 5156, 'deductions': [['loan', 39411, 2, True], ['support', 27265, 1, False], ['union', 39662, 3, True]], 'arrears': {}}, {'net': 0, 'taken': {'support': 27265, 'loan': 24440, 'union': 0}, 'arrears': {'loan': 14971, 'union': 39662}}), ('normal control', {'gross': 134888, 'taxes': 11264, 'deductions': [['union', 12074, 3, True], ['charity', 22324, 1, True], ['parking', 15797, 3, True], ['support', 20440, 2, False], ['loan', 14221, 3, True]], 'arrears': {}}, {'net': 38768, 'taken': {'charity': 22324, 'support': 20440, 'union': 12074, 'parking': 15797, 'loan': 14221}, 'arrears': {}}), ('normal control', {'gross': 52145, 'taxes': 5283, 'deductions': [['charity', 22477, 3, False], ['support', 31082, 1, True]], 'arrears': {}}, {'net': 15780, 'taken': {'support': 31082, 'charity': 0}, 'arrears': {'charity': 22477}})]]
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:
Previously missed deductions are never recovered.

Input {gross, taxes, deductions: [[name, amount, priority, allow_partial]], arrears: {name: cents}}. Available = max(0, gross - taxes). Deductions run by ascending priority, ties in input order. Each is due amount + prior arrears; if due fits it is taken in full, else partial deductions take what is left and all-or-nothing deductions take 0. Any unpaid due becomes new arrears (only positive entries). Return {net, taken, arrears}.

split: open-access


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

N = 1
observations = []
def solve(x):
    avail = max(0, x['gross'] - x['taxes'])
    taken = {}
    arrears = {}
    for name, amt, pri, partial in sorted(x['deductions'], key=lambda d: d[2]):
        due = amt + x['arrears'].get(name, 0)
        if due <= avail:
            take = due
        elif partial:
            take = avail
        else:
            take = min(avail, amt)
        avail -= take
        taken[name] = take
        if due - take > 0:
            arrears[name] = due - take
    return {'net': avail, 'taken': taken, 'arrears': arrears}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('regression', {'gross': 3515, 'taxes': 674, 'deductions': [['parking', 20962, 1, False], ['union', 18985, 3, True], ['loan', 33701, 1, True]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 0, 'loan': 2841, 'union': 0}, 'arrears': {'parking': 20962, 'loan': 30860, 'union': 18985}}), ('partial-repair probe', {'gross': 13592, 'taxes': 3481, 'deductions': [['union', 941, 2, False], ['medical', 37170, 2, False], ['charity', 11425, 3, True], ['support', 36894, 2, False], ['parking', 10277, 3, True]], 'arrears': {'medical': 2360}}, {'net': 0, 'taken': {'union': 941, 'medical': 0, 'support': 0, 'charity': 9170, 'parking': 0}, 'arrears': {'medical': 39530, 'support': 36894, 'charity': 2255, 'parking': 10277}}), ('partial-repair probe', {'gross': 71678, 'taxes': 13217, 'deductions': [['union', 12381, 2, False], ['parking', 18455, 1, True], ['medical', 26232, 1, True], ['charity', 28180, 2, False], ['support', 24570, 2, True]], 'arrears': {'union': 17040, 'parking': 7387}}, {'net': 0, 'taken': {'parking': 25842, 'medical': 26232, 'union': 0, 'charity': 0, 'support': 6387}, 'arrears': {'union': 29421, 'charity': 28180, 'support': 18183}}), ('normal control', {'gross': 134653, 'taxes': 27454, 'deductions': [['parking', 26770, 3, False], ['loan', 12315, 2, False], ['union', 3364, 1, False], ['support', 11662, 3, True]], 'arrears': {'parking': 10177, 'loan': 15972, 'support': 6216}}, {'net': 20723, 'taken': {'union': 3364, 'loan': 28287, 'parking': 36947, 'support': 17878}, 'arrears': {}}), ('normal control', {'gross': 138327, 'taxes': 24358, 'deductions': [['loan', 12004, 3, True], ['union', 4120, 2, True], ['medical', 35331, 3, False], ['parking', 697, 1, True], ['support', 15166, 2, True]], 'arrears': {'loan': 5341, 'union': 19151, 'support': 16971}}, {'net': 5188, 'taken': {'parking': 697, 'union': 23271, 'support': 32137, 'loan': 17345, 'medical': 35331}, 'arrears': {}}), ('normal control', {'gross': 102757, 'taxes': 16339, 'deductions': [['medical', 20312, 1, False]], 'arrears': {'medical': 12422}}, {'net': 53684, 'taken': {'medical': 32734}, 'arrears': {}}), ('normal control', {'gross': 6593, 'taxes': 872, 'deductions': [['parking', 5312, 3, False], ['union', 4345, 3, False], ['medical', 14587, 3, False], ['loan', 24540, 2, True], ['support', 7319, 3, True]], 'arrears': {'parking': 388, 'medical': 17218}}, {'net': 0, 'taken': {'loan': 5721, 'parking': 0, 'union': 0, 'medical': 0, 'support': 0}, 'arrears': {'loan': 18819, 'parking': 5700, 'union': 4345, 'medical': 31805, 'support': 7319}})], [('regression', {'gross': 13592, 'taxes': 3481, 'deductions': [['union', 941, 2, False], ['medical', 37170, 2, False], ['charity', 11425, 3, True], ['support', 36894, 2, False], ['parking', 10277, 3, True]], 'arrears': {'medical': 2360}}, {'net': 0, 'taken': {'union': 941, 'medical': 0, 'support': 0, 'charity': 9170, 'parking': 0}, 'arrears': {'medical': 39530, 'support': 36894, 'charity': 2255, 'parking': 10277}}), ('regression', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('partial-repair probe', {'gross': 71678, 'taxes': 13217, 'deductions': [['union', 12381, 2, False], ['parking', 18455, 1, True], ['medical', 26232, 1, True], ['charity', 28180, 2, False], ['support', 24570, 2, True]], 'arrears': {'union': 17040, 'parking': 7387}}, {'net': 0, 'taken': {'parking': 25842, 'medical': 26232, 'union': 0, 'charity': 0, 'support': 6387}, 'arrears': {'union': 29421, 'charity': 28180, 'support': 18183}}), ('partial-repair probe', {'gross': 18626, 'taxes': 5266, 'deductions': [['union', 19250, 2, False], ['medical', 18064, 1, False], ['parking', 36984, 1, False], ['charity', 38440, 1, True]], 'arrears': {'medical': 7698}}, {'net': 0, 'taken': {'medical': 0, 'parking': 0, 'charity': 13360, 'union': 0}, 'arrears': {'medical': 25762, 'parking': 36984, 'charity': 25080, 'union': 19250}}), ('normal control', {'gross': 106436, 'taxes': 29897, 'deductions': [['parking', 39254, 1, True]], 'arrears': {}}, {'net': 37285, 'taken': {'parking': 39254}, 'arrears': {}}), ('normal control', {'gross': 132719, 'taxes': 9617, 'deductions': [['charity', 21694, 1, False], ['loan', 11829, 1, True], ['parking', 18178, 3, True], ['union', 1782, 3, True]], 'arrears': {'loan': 14963, 'parking': 3722, 'union': 3447}}, {'net': 47487, 'taken': {'charity': 21694, 'loan': 26792, 'parking': 21900, 'union': 5229}, 'arrears': {}}), ('normal control', {'gross': 13589, 'taxes': 714, 'deductions': [['charity', 299, 3, True]], 'arrears': {'charity': 4459}}, {'net': 8117, 'taken': {'charity': 4758}, 'arrears': {}}), ('normal control', {'gross': 33426, 'taxes': 5333, 'deductions': [['union', 35082, 2, False], ['loan', 36975, 1, True], ['support', 21693, 3, False], ['parking', 29816, 1, True]], 'arrears': {'union': 18802, 'support': 12060, 'parking': 10514}}, {'net': 0, 'taken': {'loan': 28093, 'parking': 0, 'union': 0, 'support': 0}, 'arrears': {'loan': 8882, 'parking': 40330, 'union': 53884, 'support': 33753}})], [('regression', {'gross': 71678, 'taxes': 13217, 'deductions': [['union', 12381, 2, False], ['parking', 18455, 1, True], ['medical', 26232, 1, True], ['charity', 28180, 2, False], ['support', 24570, 2, True]], 'arrears': {'union': 17040, 'parking': 7387}}, {'net': 0, 'taken': {'parking': 25842, 'medical': 26232, 'union': 0, 'charity': 0, 'support': 6387}, 'arrears': {'union': 29421, 'charity': 28180, 'support': 18183}}), ('regression', {'gross': 91681, 'taxes': 9563, 'deductions': [['union', 25760, 2, False], ['parking', 28566, 1, False], ['support', 8036, 1, False], ['medical', 15864, 1, False], ['charity', 5966, 2, True]], 'arrears': {'parking': 5836, 'support': 18944, 'charity': 5663}}, {'net': 0, 'taken': {'parking': 34402, 'support': 26980, 'medical': 15864, 'union': 0, 'charity': 4872}, 'arrears': {'union': 25760, 'charity': 6757}}), ('partial-repair probe', {'gross': 75685, 'taxes': 14365, 'deductions': [['union', 4833, 2, True], ['charity', 38756, 2, True], ['parking', 27399, 3, True], ['loan', 13264, 2, False]], 'arrears': {'charity': 10388, 'parking': 8242, 'loan': 10585}}, {'net': 0, 'taken': {'union': 4833, 'charity': 49144, 'loan': 0, 'parking': 7343}, 'arrears': {'loan': 23849, 'parking': 28298}}), ('partial-repair probe', {'gross': 35155, 'taxes': 4541, 'deductions': [['parking', 30989, 1, False], ['support', 32872, 1, False]], 'arrears': {'parking': 10286, 'support': 19476}}, {'net': 30614, 'taken': {'parking': 0, 'support': 0}, 'arrears': {'parking': 41275, 'support': 52348}}), ('normal control', {'gross': 32919, 'taxes': 7584, 'deductions': [['parking', 36420, 3, True], ['charity', 19693, 3, False], ['loan', 12305, 1, True], ['medical', 7400, 2, True], ['support', 39536, 2, True]], 'arrears': {}}, {'net': 0, 'taken': {'loan': 12305, 'medical': 7400, 'support': 5630, 'parking': 0, 'charity': 0}, 'arrears': {'support': 33906, 'parking': 36420, 'charity': 19693}}), ('normal control', {'gross': 142935, 'taxes': 39131, 'deductions': [['parking', 22705, 2, False], ['charity', 7658, 1, True], ['union', 9456, 1, False]], 'arrears': {'charity': 15978}}, {'net': 48007, 'taken': {'charity': 23636, 'union': 9456, 'parking': 22705}, 'arrears': {}}), ('normal control', {'gross': 49085, 'taxes': 13081, 'deductions': [['medical', 5278, 3, False], ['charity', 17544, 1, True], ['support', 5654, 1, True], ['parking', 38004, 1, False]], 'arrears': {'medical': 17073, 'charity': 11573, 'support': 3632}}, {'net': 0, 'taken': {'charity': 29117, 'support': 6887, 'parking': 0, 'medical': 0}, 'arrears': {'support': 2399, 'parking': 38004, 'medical': 22351}}), ('normal control', {'gross': 63849, 'taxes': 6027, 'deductions': [['union', 18684, 2, True], ['charity', 28880, 1, True], ['loan', 38844, 2, True], ['support', 20513, 3, False]], 'arrears': {'union': 15372}}, {'net': 0, 'taken': {'charity': 28880, 'union': 28942, 'loan': 0, 'support': 0}, 'arrears': {'union': 5114, 'loan': 38844, 'support': 20513}})], [('regression', {'gross': 18626, 'taxes': 5266, 'deductions': [['union', 19250, 2, False], ['medical', 18064, 1, False], ['parking', 36984, 1, False], ['charity', 38440, 1, True]], 'arrears': {'medical': 7698}}, {'net': 0, 'taken': {'medical': 0, 'parking': 0, 'charity': 13360, 'union': 0}, 'arrears': {'medical': 25762, 'parking': 36984, 'charity': 25080, 'union': 19250}}), ('regression', {'gross': 13592, 'taxes': 3481, 'deductions': [['union', 941, 2, False], ['medical', 37170, 2, False], ['charity', 11425, 3, True], ['support', 36894, 2, False], ['parking', 10277, 3, True]], 'arrears': {'medical': 2360}}, {'net': 0, 'taken': {'union': 941, 'medical': 0, 'support': 0, 'charity': 9170, 'parking': 0}, 'arrears': {'medical': 39530, 'support': 36894, 'charity': 2255, 'parking': 10277}}), ('partial-repair probe', {'gross': 20103, 'taxes': 4992, 'deductions': [['union', 13346, 2, False], ['medical', 15940, 3, True], ['support', 17952, 1, False]], 'arrears': {'support': 865}}, {'net': 0, 'taken': {'support': 0, 'union': 13346, 'medical': 1765}, 'arrears': {'support': 18817, 'medical': 14175}}), ('partial-repair probe', {'gross': 90353, 'taxes': 27655, 'deductions': [['charity', 13590, 1, False], ['support', 19892, 1, False], ['parking', 27727, 2, False], ['loan', 31849, 1, False]], 'arrears': {'support': 554, 'parking': 7070}}, {'net': 28662, 'taken': {'charity': 13590, 'support': 20446, 'loan': 0, 'parking': 0}, 'arrears': {'loan': 31849, 'parking': 34797}}), ('normal control', {'gross': 118080, 'taxes': 35605, 'deductions': [['medical', 23525, 3, True], ['union', 17009, 2, True], ['support', 18449, 2, False], ['loan', 17353, 1, False]], 'arrears': {'union': 9120}}, {'net': 0, 'taken': {'loan': 17353, 'union': 26129, 'support': 18449, 'medical': 20544}, 'arrears': {'medical': 2981}}), ('normal control', {'gross': 63883, 'taxes': 16403, 'deductions': [['loan', 28842, 3, False], ['medical', 34809, 2, True]], 'arrears': {'loan': 5346, 'medical': 15166}}, {'net': 0, 'taken': {'medical': 47480, 'loan': 0}, 'arrears': {'medical': 2495, 'loan': 34188}}), ('normal control', {'gross': 123929, 'taxes': 27738, 'deductions': [['parking', 24255, 3, False]], 'arrears': {}}, {'net': 71936, 'taken': {'parking': 24255}, 'arrears': {}}), ('normal control', {'gross': 60703, 'taxes': 11543, 'deductions': [['support', 14720, 2, False], ['loan', 10777, 3, False], ['medical', 34788, 2, True]], 'arrears': {}}, {'net': 0, 'taken': {'support': 14720, 'medical': 34440, 'loan': 0}, 'arrears': {'medical': 348, 'loan': 10777}})], [('regression', {'gross': 75685, 'taxes': 14365, 'deductions': [['union', 4833, 2, True], ['charity', 38756, 2, True], ['parking', 27399, 3, True], ['loan', 13264, 2, False]], 'arrears': {'charity': 10388, 'parking': 8242, 'loan': 10585}}, {'net': 0, 'taken': {'union': 4833, 'charity': 49144, 'loan': 0, 'parking': 7343}, 'arrears': {'loan': 23849, 'parking': 28298}}), ('regression', {'gross': 71403, 'taxes': 15182, 'deductions': [['support', 561, 2, True], ['charity', 7949, 1, True], ['parking', 31674, 1, False], ['medical', 37176, 1, False]], 'arrears': {'support': 13700}}, {'net': 2337, 'taken': {'charity': 7949, 'parking': 31674, 'medical': 0, 'support': 14261}, 'arrears': {'medical': 37176}}), ('partial-repair probe', {'gross': 94433, 'taxes': 16951, 'deductions': [['union', 37393, 3, False], ['parking', 27754, 2, True], ['loan', 1454, 1, True], ['charity', 32386, 3, False]], 'arrears': {'union': 14860, 'charity': 15646}}, {'net': 242, 'taken': {'loan': 1454, 'parking': 27754, 'union': 0, 'charity': 48032}, 'arrears': {'union': 52253}}), ('partial-repair probe', {'gross': 73228, 'taxes': 666, 'deductions': [['loan', 31096, 2, False], ['medical', 18616, 3, False], ['support', 10492, 3, True], ['union', 39510, 1, False], ['charity', 17132, 2, False]], 'arrears': {'loan': 2531, 'support': 15520}}, {'net': 0, 'taken': {'union': 39510, 'loan': 0, 'charity': 17132, 'medical': 0, 'support': 15920}, 'arrears': {'loan': 33627, 'medical': 18616, 'support': 10092}}), ('normal control', {'gross': 121022, 'taxes': 14438, 'deductions': [['charity', 24183, 2, False], ['loan', 4420, 2, True]], 'arrears': {}}, {'net': 77981, 'taken': {'charity': 24183, 'loan': 4420}, 'arrears': {}}), ('normal control', {'gross': 123756, 'taxes': 24437, 'deductions': [['loan', 11603, 3, True], ['parking', 10895, 1, False], ['union', 27944, 2, True], ['medical', 22697, 3, True]], 'arrears': {'loan': 11696, 'parking': 14658}}, {'net': 0, 'taken': {'parking': 25553, 'union': 27944, 'loan': 23299, 'medical': 22523}, 'arrears': {'medical': 174}}), ('normal control', {'gross': 37898, 'taxes': 10470, 'deductions': [['union', 18447, 3, False], ['parking', 30036, 3, True], ['support', 12641, 2, True], ['loan', 39343, 2, False], ['charity', 10333, 2, False]], 'arrears': {'parking': 13057, 'support': 19997}}, {'net': 0, 'taken': {'support': 27428, 'loan': 0, 'charity': 0, 'union': 0, 'parking': 0}, 'arrears': {'support': 5210, 'loan': 39343, 'charity': 10333, 'union': 18447, 'parking': 43093}}), ('normal control', {'gross': 70795, 'taxes': 21939, 'deductions': [['charity', 32424, 2, True], ['union', 13741, 1, True], ['support', 37512, 1, True], ['loan', 10429, 3, False], ['parking', 8289, 3, False]], 'arrears': {'charity': 12305, 'union': 10397, 'support': 13769}}, {'net': 0, 'taken': {'union': 24138, 'support': 24718, 'charity': 0, 'loan': 0, 'parking': 0}, 'arrears': {'support': 26563, 'charity': 44729, 'loan': 10429, 'parking': 8289}})]]
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):
    avail = max(0, x['gross'] - x['taxes'])
    taken = {}
    arrears = {}
    for name, amt, pri, partial in sorted(x['deductions'], key=lambda d: d[2]):
        due = amt + x['arrears'].get(name, 0)
        if due <= avail:
            take = due
        elif partial:
            take = avail
        else:
            take = min(avail, x['arrears'].get(name, 0))
        avail -= take
        taken[name] = take
        if due - take > 0:
            arrears[name] = due - take
    return {'net': avail, 'taken': taken, 'arrears': arrears}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('regression', {'gross': 3515, 'taxes': 674, 'deductions': [['parking', 20962, 1, False], ['union', 18985, 3, True], ['loan', 33701, 1, True]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 0, 'loan': 2841, 'union': 0}, 'arrears': {'parking': 20962, 'loan': 30860, 'union': 18985}}), ('partial-repair probe', {'gross': 13592, 'taxes': 3481, 'deductions': [['union', 941, 2, False], ['medical', 37170, 2, False], ['charity', 11425, 3, True], ['support', 36894, 2, False], ['parking', 10277, 3, True]], 'arrears': {'medical': 2360}}, {'net': 0, 'taken': {'union': 941, 'medical': 0, 'support': 0, 'charity': 9170, 'parking': 0}, 'arrears': {'medical': 39530, 'support': 36894, 'charity': 2255, 'parking': 10277}}), ('partial-repair probe', {'gross': 71678, 'taxes': 13217, 'deductions': [['union', 12381, 2, False], ['parking', 18455, 1, True], ['medical', 26232, 1, True], ['charity', 28180, 2, False], ['support', 24570, 2, True]], 'arrears': {'union': 17040, 'parking': 7387}}, {'net': 0, 'taken': {'parking': 25842, 'medical': 26232, 'union': 0, 'charity': 0, 'support': 6387}, 'arrears': {'union': 29421, 'charity': 28180, 'support': 18183}}), ('normal control', {'gross': 134653, 'taxes': 27454, 'deductions': [['parking', 26770, 3, False], ['loan', 12315, 2, False], ['union', 3364, 1, False], ['support', 11662, 3, True]], 'arrears': {'parking': 10177, 'loan': 15972, 'support': 6216}}, {'net': 20723, 'taken': {'union': 3364, 'loan': 28287, 'parking': 36947, 'support': 17878}, 'arrears': {}}), ('normal control', {'gross': 138327, 'taxes': 24358, 'deductions': [['loan', 12004, 3, True], ['union', 4120, 2, True], ['medical', 35331, 3, False], ['parking', 697, 1, True], ['support', 15166, 2, True]], 'arrears': {'loan': 5341, 'union': 19151, 'support': 16971}}, {'net': 5188, 'taken': {'parking': 697, 'union': 23271, 'support': 32137, 'loan': 17345, 'medical': 35331}, 'arrears': {}}), ('normal control', {'gross': 102757, 'taxes': 16339, 'deductions': [['medical', 20312, 1, False]], 'arrears': {'medical': 12422}}, {'net': 53684, 'taken': {'medical': 32734}, 'arrears': {}}), ('normal control', {'gross': 6593, 'taxes': 872, 'deductions': [['parking', 5312, 3, False], ['union', 4345, 3, False], ['medical', 14587, 3, False], ['loan', 24540, 2, True], ['support', 7319, 3, True]], 'arrears': {'parking': 388, 'medical': 17218}}, {'net': 0, 'taken': {'loan': 5721, 'parking': 0, 'union': 0, 'medical': 0, 'support': 0}, 'arrears': {'loan': 18819, 'parking': 5700, 'union': 4345, 'medical': 31805, 'support': 7319}})], [('regression', {'gross': 13592, 'taxes': 3481, 'deductions': [['union', 941, 2, False], ['medical', 37170, 2, False], ['charity', 11425, 3, True], ['support', 36894, 2, False], ['parking', 10277, 3, True]], 'arrears': {'medical': 2360}}, {'net': 0, 'taken': {'union': 941, 'medical': 0, 'support': 0, 'charity': 9170, 'parking': 0}, 'arrears': {'medical': 39530, 'support': 36894, 'charity': 2255, 'parking': 10277}}), ('regression', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('partial-repair probe', {'gross': 71678, 'taxes': 13217, 'deductions': [['union', 12381, 2, False], ['parking', 18455, 1, True], ['medical', 26232, 1, True], ['charity', 28180, 2, False], ['support', 24570, 2, True]], 'arrears': {'union': 17040, 'parking': 7387}}, {'net': 0, 'taken': {'parking': 25842, 'medical': 26232, 'union': 0, 'charity': 0, 'support': 6387}, 'arrears': {'union': 29421, 'charity': 28180, 'support': 18183}}), ('partial-repair probe', {'gross': 18626, 'taxes': 5266, 'deductions': [['union', 19250, 2, False], ['medical', 18064, 1, False], ['parking', 36984, 1, False], ['charity', 38440, 1, True]], 'arrears': {'medical': 7698}}, {'net': 0, 'taken': {'medical': 0, 'parking': 0, 'charity': 13360, 'union': 0}, 'arrears': {'medical': 25762, 'parking': 36984, 'charity': 25080, 'union': 19250}}), ('normal control', {'gross': 106436, 'taxes': 29897, 'deductions': [['parking', 39254, 1, True]], 'arrears': {}}, {'net': 37285, 'taken': {'parking': 39254}, 'arrears': {}}), ('normal control', {'gross': 132719, 'taxes': 9617, 'deductions': [['charity', 21694, 1, False], ['loan', 11829, 1, True], ['parking', 18178, 3, True], ['union', 1782, 3, True]], 'arrears': {'loan': 14963, 'parking': 3722, 'union': 3447}}, {'net': 47487, 'taken': {'charity': 21694, 'loan': 26792, 'parking': 21900, 'union': 5229}, 'arrears': {}}), ('normal control', {'gross': 13589, 'taxes': 714, 'deductions': [['charity', 299, 3, True]], 'arrears': {'charity': 4459}}, {'net': 8117, 'taken': {'charity': 4758}, 'arrears': {}}), ('normal control', {'gross': 33426, 'taxes': 5333, 'deductions': [['union', 35082, 2, False], ['loan', 36975, 1, True], ['support', 21693, 3, False], ['parking', 29816, 1, True]], 'arrears': {'union': 18802, 'support': 12060, 'parking': 10514}}, {'net': 0, 'taken': {'loan': 28093, 'parking': 0, 'union': 0, 'support': 0}, 'arrears': {'loan': 8882, 'parking': 40330, 'union': 53884, 'support': 33753}})], [('regression', {'gross': 71678, 'taxes': 13217, 'deductions': [['union', 12381, 2, False], ['parking', 18455, 1, True], ['medical', 26232, 1, True], ['charity', 28180, 2, False], ['support', 24570, 2, True]], 'arrears': {'union': 17040, 'parking': 7387}}, {'net': 0, 'taken': {'parking': 25842, 'medical': 26232, 'union': 0, 'charity': 0, 'support': 6387}, 'arrears': {'union': 29421, 'charity': 28180, 'support': 18183}}), ('regression', {'gross': 91681, 'taxes': 9563, 'deductions': [['union', 25760, 2, False], ['parking', 28566, 1, False], ['support', 8036, 1, False], ['medical', 15864, 1, False], ['charity', 5966, 2, True]], 'arrears': {'parking': 5836, 'support': 18944, 'charity': 5663}}, {'net': 0, 'taken': {'parking': 34402, 'support': 26980, 'medical': 15864, 'union': 0, 'charity': 4872}, 'arrears': {'union': 25760, 'charity': 6757}}), ('partial-repair probe', {'gross': 75685, 'taxes': 14365, 'deductions': [['union', 4833, 2, True], ['charity', 38756, 2, True], ['parking', 27399, 3, True], ['loan', 13264, 2, False]], 'arrears': {'charity': 10388, 'parking': 8242, 'loan': 10585}}, {'net': 0, 'taken': {'union': 4833, 'charity': 49144, 'loan': 0, 'parking': 7343}, 'arrears': {'loan': 23849, 'parking': 28298}}), ('partial-repair probe', {'gross': 35155, 'taxes': 4541, 'deductions': [['parking', 30989, 1, False], ['support', 32872, 1, False]], 'arrears': {'parking': 10286, 'support': 19476}}, {'net': 30614, 'taken': {'parking': 0, 'support': 0}, 'arrears': {'parking': 41275, 'support': 52348}}), ('normal control', {'gross': 32919, 'taxes': 7584, 'deductions': [['parking', 36420, 3, True], ['charity', 19693, 3, False], ['loan', 12305, 1, True], ['medical', 7400, 2, True], ['support', 39536, 2, True]], 'arrears': {}}, {'net': 0, 'taken': {'loan': 12305, 'medical': 7400, 'support': 5630, 'parking': 0, 'charity': 0}, 'arrears': {'support': 33906, 'parking': 36420, 'charity': 19693}}), ('normal control', {'gross': 142935, 'taxes': 39131, 'deductions': [['parking', 22705, 2, False], ['charity', 7658, 1, True], ['union', 9456, 1, False]], 'arrears': {'charity': 15978}}, {'net': 48007, 'taken': {'charity': 23636, 'union': 9456, 'parking': 22705}, 'arrears': {}}), ('normal control', {'gross': 49085, 'taxes': 13081, 'deductions': [['medical', 5278, 3, False], ['charity', 17544, 1, True], ['support', 5654, 1, True], ['parking', 38004, 1, False]], 'arrears': {'medical': 17073, 'charity': 11573, 'support': 3632}}, {'net': 0, 'taken': {'charity': 29117, 'support': 6887, 'parking': 0, 'medical': 0}, 'arrears': {'support': 2399, 'parking': 38004, 'medical': 22351}}), ('normal control', {'gross': 63849, 'taxes': 6027, 'deductions': [['union', 18684, 2, True], ['charity', 28880, 1, True], ['loan', 38844, 2, True], ['support', 20513, 3, False]], 'arrears': {'union': 15372}}, {'net': 0, 'taken': {'charity': 28880, 'union': 28942, 'loan': 0, 'support': 0}, 'arrears': {'union': 5114, 'loan': 38844, 'support': 20513}})], [('regression', {'gross': 18626, 'taxes': 5266, 'deductions': [['union', 19250, 2, False], ['medical', 18064, 1, False], ['parking', 36984, 1, False], ['charity', 38440, 1, True]], 'arrears': {'medical': 7698}}, {'net': 0, 'taken': {'medical': 0, 'parking': 0, 'charity': 13360, 'union': 0}, 'arrears': {'medical': 25762, 'parking': 36984, 'charity': 25080, 'union': 19250}}), ('regression', {'gross': 13592, 'taxes': 3481, 'deductions': [['union', 941, 2, False], ['medical', 37170, 2, False], ['charity', 11425, 3, True], ['support', 36894, 2, False], ['parking', 10277, 3, True]], 'arrears': {'medical': 2360}}, {'net': 0, 'taken': {'union': 941, 'medical': 0, 'support': 0, 'charity': 9170, 'parking': 0}, 'arrears': {'medical': 39530, 'support': 36894, 'charity': 2255, 'parking': 10277}}), ('partial-repair probe', {'gross': 20103, 'taxes': 4992, 'deductions': [['union', 13346, 2, False], ['medical', 15940, 3, True], ['support', 17952, 1, False]], 'arrears': {'support': 865}}, {'net': 0, 'taken': {'support': 0, 'union': 13346, 'medical': 1765}, 'arrears': {'support': 18817, 'medical': 14175}}), ('partial-repair probe', {'gross': 90353, 'taxes': 27655, 'deductions': [['charity', 13590, 1, False], ['support', 19892, 1, False], ['parking', 27727, 2, False], ['loan', 31849, 1, False]], 'arrears': {'support': 554, 'parking': 7070}}, {'net': 28662, 'taken': {'charity': 13590, 'support': 20446, 'loan': 0, 'parking': 0}, 'arrears': {'loan': 31849, 'parking': 34797}}), ('normal control', {'gross': 118080, 'taxes': 35605, 'deductions': [['medical', 23525, 3, True], ['union', 17009, 2, True], ['support', 18449, 2, False], ['loan', 17353, 1, False]], 'arrears': {'union': 9120}}, {'net': 0, 'taken': {'loan': 17353, 'union': 26129, 'support': 18449, 'medical': 20544}, 'arrears': {'medical': 2981}}), ('normal control', {'gross': 63883, 'taxes': 16403, 'deductions': [['loan', 28842, 3, False], ['medical', 34809, 2, True]], 'arrears': {'loan': 5346, 'medical': 15166}}, {'net': 0, 'taken': {'medical': 47480, 'loan': 0}, 'arrears': {'medical': 2495, 'loan': 34188}}), ('normal control', {'gross': 123929, 'taxes': 27738, 'deductions': [['parking', 24255, 3, False]], 'arrears': {}}, {'net': 71936, 'taken': {'parking': 24255}, 'arrears': {}}), ('normal control', {'gross': 60703, 'taxes': 11543, 'deductions': [['support', 14720, 2, False], ['loan', 10777, 3, False], ['medical', 34788, 2, True]], 'arrears': {}}, {'net': 0, 'taken': {'support': 14720, 'medical': 34440, 'loan': 0}, 'arrears': {'medical': 348, 'loan': 10777}})], [('regression', {'gross': 75685, 'taxes': 14365, 'deductions': [['union', 4833, 2, True], ['charity', 38756, 2, True], ['parking', 27399, 3, True], ['loan', 13264, 2, False]], 'arrears': {'charity': 10388, 'parking': 8242, 'loan': 10585}}, {'net': 0, 'taken': {'union': 4833, 'charity': 49144, 'loan': 0, 'parking': 7343}, 'arrears': {'loan': 23849, 'parking': 28298}}), ('regression', {'gross': 71403, 'taxes': 15182, 'deductions': [['support', 561, 2, True], ['charity', 7949, 1, True], ['parking', 31674, 1, False], ['medical', 37176, 1, False]], 'arrears': {'support': 13700}}, {'net': 2337, 'taken': {'charity': 7949, 'parking': 31674, 'medical': 0, 'support': 14261}, 'arrears': {'medical': 37176}}), ('partial-repair probe', {'gross': 94433, 'taxes': 16951, 'deductions': [['union', 37393, 3, False], ['parking', 27754, 2, True], ['loan', 1454, 1, True], ['charity', 32386, 3, False]], 'arrears': {'union': 14860, 'charity': 15646}}, {'net': 242, 'taken': {'loan': 1454, 'parking': 27754, 'union': 0, 'charity': 48032}, 'arrears': {'union': 52253}}), ('partial-repair probe', {'gross': 73228, 'taxes': 666, 'deductions': [['loan', 31096, 2, False], ['medical', 18616, 3, False], ['support', 10492, 3, True], ['union', 39510, 1, False], ['charity', 17132, 2, False]], 'arrears': {'loan': 2531, 'support': 15520}}, {'net': 0, 'taken': {'union': 39510, 'loan': 0, 'charity': 17132, 'medical': 0, 'support': 15920}, 'arrears': {'loan': 33627, 'medical': 18616, 'support': 10092}}), ('normal control', {'gross': 121022, 'taxes': 14438, 'deductions': [['charity', 24183, 2, False], ['loan', 4420, 2, True]], 'arrears': {}}, {'net': 77981, 'taken': {'charity': 24183, 'loan': 4420}, 'arrears': {}}), ('normal control', {'gross': 123756, 'taxes': 24437, 'deductions': [['loan', 11603, 3, True], ['parking', 10895, 1, False], ['union', 27944, 2, True], ['medical', 22697, 3, True]], 'arrears': {'loan': 11696, 'parking': 14658}}, {'net': 0, 'taken': {'parking': 25553, 'union': 27944, 'loan': 23299, 'medical': 22523}, 'arrears': {'medical': 174}}), ('normal control', {'gross': 37898, 'taxes': 10470, 'deductions': [['union', 18447, 3, False], ['parking', 30036, 3, True], ['support', 12641, 2, True], ['loan', 39343, 2, False], ['charity', 10333, 2, False]], 'arrears': {'parking': 13057, 'support': 19997}}, {'net': 0, 'taken': {'support': 27428, 'loan': 0, 'charity': 0, 'union': 0, 'parking': 0}, 'arrears': {'support': 5210, 'loan': 39343, 'charity': 10333, 'union': 18447, 'parking': 43093}}), ('normal control', {'gross': 70795, 'taxes': 21939, 'deductions': [['charity', 32424, 2, True], ['union', 13741, 1, True], ['support', 37512, 1, True], ['loan', 10429, 3, False], ['parking', 8289, 3, False]], 'arrears': {'charity': 12305, 'union': 10397, 'support': 13769}}, {'net': 0, 'taken': {'union': 24138, 'support': 24718, 'charity': 0, 'loan': 0, 'parking': 0}, 'arrears': {'support': 26563, 'charity': 44729, 'loan': 10429, 'parking': 8289}})]]
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:
Deductions that must be taken in full are partially taken when pay is short.

Input {gross, taxes, deductions: [[name, amount, priority, allow_partial]], arrears: {name: cents}}. Available = max(0, gross - taxes). Deductions run by ascending priority, ties in input order. Each is due amount + prior arrears; if due fits it is taken in full, else partial deductions take what is left and all-or-nothing deductions take 0. Any unpaid due becomes new arrears (only positive entries). Return {net, taken, arrears}.

split: open-access


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

N = 1
observations = []
def solve(x):
    avail = max(0, x['gross'] - x['taxes'])
    taken = {}
    arrears = {}
    for name, amt, pri, partial in sorted(x['deductions'], key=lambda d: d[2]):
        due = amt + x['arrears'].get(name, 0)
        if due <= avail:
            take = due
        elif partial:
            take = avail
        else:
            take = 0
        avail -= take
        taken[name] = take
        if due - take >= 0:
            arrears[name] = due - take
    return {'net': avail, 'taken': taken, 'arrears': arrears}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'gross': 49085, 'taxes': 13081, 'deductions': [['medical', 5278, 3, False], ['charity', 17544, 1, True], ['support', 5654, 1, True], ['parking', 38004, 1, False]], 'arrears': {'medical': 17073, 'charity': 11573, 'support': 3632}}, {'net': 0, 'taken': {'charity': 29117, 'support': 6887, 'parking': 0, 'medical': 0}, 'arrears': {'support': 2399, 'parking': 38004, 'medical': 22351}}), ('regression', {'gross': 134653, 'taxes': 27454, 'deductions': [['parking', 26770, 3, False], ['loan', 12315, 2, False], ['union', 3364, 1, False], ['support', 11662, 3, True]], 'arrears': {'parking': 10177, 'loan': 15972, 'support': 6216}}, {'net': 20723, 'taken': {'union': 3364, 'loan': 28287, 'parking': 36947, 'support': 17878}, 'arrears': {}}), ('partial-repair probe', {'gross': 63849, 'taxes': 6027, 'deductions': [['union', 18684, 2, True], ['charity', 28880, 1, True], ['loan', 38844, 2, True], ['support', 20513, 3, False]], 'arrears': {'union': 15372}}, {'net': 0, 'taken': {'charity': 28880, 'union': 28942, 'loan': 0, 'support': 0}, 'arrears': {'union': 5114, 'loan': 38844, 'support': 20513}}), ('partial-repair probe', {'gross': 63883, 'taxes': 16403, 'deductions': [['loan', 28842, 3, False], ['medical', 34809, 2, True]], 'arrears': {'loan': 5346, 'medical': 15166}}, {'net': 0, 'taken': {'medical': 47480, 'loan': 0}, 'arrears': {'medical': 2495, 'loan': 34188}}), ('normal control', {'gross': 6593, 'taxes': 872, 'deductions': [['parking', 5312, 3, False], ['union', 4345, 3, False], ['medical', 14587, 3, False], ['loan', 24540, 2, True], ['support', 7319, 3, True]], 'arrears': {'parking': 388, 'medical': 17218}}, {'net': 0, 'taken': {'loan': 5721, 'parking': 0, 'union': 0, 'medical': 0, 'support': 0}, 'arrears': {'loan': 18819, 'parking': 5700, 'union': 4345, 'medical': 31805, 'support': 7319}}), ('normal control', {'gross': 3515, 'taxes': 674, 'deductions': [['parking', 20962, 1, False], ['union', 18985, 3, True], ['loan', 33701, 1, True]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 0, 'loan': 2841, 'union': 0}, 'arrears': {'parking': 20962, 'loan': 30860, 'union': 18985}}), ('normal control', {'gross': 33426, 'taxes': 5333, 'deductions': [['union', 35082, 2, False], ['loan', 36975, 1, True], ['support', 21693, 3, False], ['parking', 29816, 1, True]], 'arrears': {'union': 18802, 'support': 12060, 'parking': 10514}}, {'net': 0, 'taken': {'loan': 28093, 'parking': 0, 'union': 0, 'support': 0}, 'arrears': {'loan': 8882, 'parking': 40330, 'union': 53884, 'support': 33753}}), ('normal control', {'gross': 18626, 'taxes': 5266, 'deductions': [['union', 19250, 2, False], ['medical', 18064, 1, False], ['parking', 36984, 1, False], ['charity', 38440, 1, True]], 'arrears': {'medical': 7698}}, {'net': 0, 'taken': {'medical': 0, 'parking': 0, 'charity': 13360, 'union': 0}, 'arrears': {'medical': 25762, 'parking': 36984, 'charity': 25080, 'union': 19250}})], [('regression', {'gross': 63849, 'taxes': 6027, 'deductions': [['union', 18684, 2, True], ['charity', 28880, 1, True], ['loan', 38844, 2, True], ['support', 20513, 3, False]], 'arrears': {'union': 15372}}, {'net': 0, 'taken': {'charity': 28880, 'union': 28942, 'loan': 0, 'support': 0}, 'arrears': {'union': 5114, 'loan': 38844, 'support': 20513}}), ('regression', {'gross': 138327, 'taxes': 24358, 'deductions': [['loan', 12004, 3, True], ['union', 4120, 2, True], ['medical', 35331, 3, False], ['parking', 697, 1, True], ['support', 15166, 2, True]], 'arrears': {'loan': 5341, 'union': 19151, 'support': 16971}}, {'net': 5188, 'taken': {'parking': 697, 'union': 23271, 'support': 32137, 'loan': 17345, 'medical': 35331}, 'arrears': {}}), ('partial-repair probe', {'gross': 63883, 'taxes': 16403, 'deductions': [['loan', 28842, 3, False], ['medical', 34809, 2, True]], 'arrears': {'loan': 5346, 'medical': 15166}}, {'net': 0, 'taken': {'medical': 47480, 'loan': 0}, 'arrears': {'medical': 2495, 'loan': 34188}}), ('partial-repair probe', {'gross': 37898, 'taxes': 10470, 'deductions': [['union', 18447, 3, False], ['parking', 30036, 3, True], ['support', 12641, 2, True], ['loan', 39343, 2, False], ['charity', 10333, 2, False]], 'arrears': {'parking': 13057, 'support': 19997}}, {'net': 0, 'taken': {'support': 27428, 'loan': 0, 'charity': 0, 'union': 0, 'parking': 0}, 'arrears': {'support': 5210, 'loan': 39343, 'charity': 10333, 'union': 18447, 'parking': 43093}}), ('normal control', {'gross': 35155, 'taxes': 4541, 'deductions': [['parking', 30989, 1, False], ['support', 32872, 1, False]], 'arrears': {'parking': 10286, 'support': 19476}}, {'net': 30614, 'taken': {'parking': 0, 'support': 0}, 'arrears': {'parking': 41275, 'support': 52348}}), ('normal control', {'gross': 21943, 'taxes': 4843, 'deductions': [['parking', 33670, 3, False], ['charity', 24679, 3, True]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 0, 'charity': 17100}, 'arrears': {'parking': 33670, 'charity': 7579}}), ('normal control', {'gross': 20726, 'taxes': 5501, 'deductions': [['charity', 22176, 1, True], ['medical', 30958, 1, True], ['loan', 15175, 2, True], ['support', 34214, 2, True]], 'arrears': {'support': 1284}}, {'net': 0, 'taken': {'charity': 15225, 'medical': 0, 'loan': 0, 'support': 0}, 'arrears': {'charity': 6951, 'medical': 30958, 'loan': 15175, 'support': 35498}}), ('normal control', {'gross': 15352, 'taxes': 3409, 'deductions': [['charity', 24919, 2, True], ['support', 16527, 2, False], ['loan', 28071, 2, False]], 'arrears': {}}, {'net': 0, 'taken': {'charity': 11943, 'support': 0, 'loan': 0}, 'arrears': {'charity': 12976, 'support': 16527, 'loan': 28071}})], [('regression', {'gross': 75893, 'taxes': 4166, 'deductions': [['parking', 28979, 1, True], ['medical', 32307, 2, True], ['support', 39584, 3, True], ['loan', 36560, 1, True]], 'arrears': {'medical': 4976, 'loan': 12853}}, {'net': 0, 'taken': {'parking': 28979, 'loan': 42748, 'medical': 0, 'support': 0}, 'arrears': {'loan': 6665, 'medical': 37283, 'support': 39584}}), ('regression', {'gross': 102757, 'taxes': 16339, 'deductions': [['medical', 20312, 1, False]], 'arrears': {'medical': 12422}}, {'net': 53684, 'taken': {'medical': 32734}, 'arrears': {}}), ('partial-repair probe', {'gross': 73228, 'taxes': 666, 'deductions': [['loan', 31096, 2, False], ['medical', 18616, 3, False], ['support', 10492, 3, True], ['union', 39510, 1, False], ['charity', 17132, 2, False]], 'arrears': {'loan': 2531, 'support': 15520}}, {'net': 0, 'taken': {'union': 39510, 'loan': 0, 'charity': 17132, 'medical': 0, 'support': 15920}, 'arrears': {'loan': 33627, 'medical': 18616, 'support': 10092}}), ('partial-repair probe', {'gross': 59493, 'taxes': 12677, 'deductions': [['parking', 36655, 2, True], ['union', 22566, 3, True], ['loan', 20538, 1, True], ['medical', 9246, 1, False], ['support', 14303, 1, True]], 'arrears': {'medical': 18889, 'support': 15753}}, {'net': 0, 'taken': {'loan': 20538, 'medical': 0, 'support': 26278, 'parking': 0, 'union': 0}, 'arrears': {'medical': 28135, 'support': 3778, 'parking': 36655, 'union': 22566}}), ('normal control', {'gross': 19093, 'taxes': 472, 'deductions': [['loan', 33282, 1, False], ['union', 29885, 3, False], ['charity', 36812, 3, True], ['support', 18134, 3, True]], 'arrears': {'charity': 8415}}, {'net': 0, 'taken': {'loan': 0, 'union': 0, 'charity': 18621, 'support': 0}, 'arrears': {'loan': 33282, 'union': 29885, 'charity': 26606, 'support': 18134}}), ('normal control', {'gross': 9496, 'taxes': 2152, 'deductions': [['loan', 37495, 2, False], ['union', 25319, 1, True]], 'arrears': {'loan': 5106}}, {'net': 0, 'taken': {'union': 7344, 'loan': 0}, 'arrears': {'union': 17975, 'loan': 42601}}), ('normal control', {'gross': 4210, 'taxes': 944, 'deductions': [['charity', 12281, 3, True], ['loan', 21699, 2, False]], 'arrears': {'loan': 5243}}, {'net': 0, 'taken': {'loan': 0, 'charity': 3266}, 'arrears': {'loan': 26942, 'charity': 9015}}), ('normal control', {'gross': 46504, 'taxes': 13089, 'deductions': [['union', 16705, 3, False], ['charity', 31049, 3, True], ['medical', 10810, 3, True], ['support', 34027, 2, True]], 'arrears': {'union': 13605}}, {'net': 0, 'taken': {'support': 33415, 'union': 0, 'charity': 0, 'medical': 0}, 'arrears': {'support': 612, 'union': 30310, 'charity': 31049, 'medical': 10810}})], [('regression', {'gross': 73228, 'taxes': 666, 'deductions': [['loan', 31096, 2, False], ['medical', 18616, 3, False], ['support', 10492, 3, True], ['union', 39510, 1, False], ['charity', 17132, 2, False]], 'arrears': {'loan': 2531, 'support': 15520}}, {'net': 0, 'taken': {'union': 39510, 'loan': 0, 'charity': 17132, 'medical': 0, 'support': 15920}, 'arrears': {'loan': 33627, 'medical': 18616, 'support': 10092}}), ('regression', {'gross': 106436, 'taxes': 29897, 'deductions': [['parking', 39254, 1, True]], 'arrears': {}}, {'net': 37285, 'taken': {'parking': 39254}, 'arrears': {}}), ('partial-repair probe', {'gross': 59493, 'taxes': 12677, 'deductions': [['parking', 36655, 2, True], ['union', 22566, 3, True], ['loan', 20538, 1, True], ['medical', 9246, 1, False], ['support', 14303, 1, True]], 'arrears': {'medical': 18889, 'support': 15753}}, {'net': 0, 'taken': {'loan': 20538, 'medical': 0, 'support': 26278, 'parking': 0, 'union': 0}, 'arrears': {'medical': 28135, 'support': 3778, 'parking': 36655, 'union': 22566}}), ('partial-repair probe', {'gross': 44674, 'taxes': 14202, 'deductions': [['parking', 23778, 3, True], ['union', 2374, 1, False], ['loan', 38498, 1, False]], 'arrears': {'parking': 6381}}, {'net': 0, 'taken': {'union': 2374, 'loan': 0, 'parking': 28098}, 'arrears': {'loan': 38498, 'parking': 2061}}), ('normal control', {'gross': 770, 'taxes': 57, 'deductions': [['support', 18698, 2, False]], 'arrears': {'support': 10191}}, {'net': 713, 'taken': {'support': 0}, 'arrears': {'support': 28889}}), ('normal control', {'gross': 23798, 'taxes': 1352, 'deductions': [['support', 18645, 2, False]], 'arrears': {'support': 11865}}, {'net': 22446, 'taken': {'support': 0}, 'arrears': {'support': 30510}}), ('normal control', {'gross': 8461, 'taxes': 2779, 'deductions': [['medical', 9329, 2, False], ['support', 30631, 3, True], ['union', 9173, 1, False], ['loan', 6881, 2, True]], 'arrears': {'support': 18557}}, {'net': 0, 'taken': {'union': 0, 'medical': 0, 'loan': 5682, 'support': 0}, 'arrears': {'union': 9173, 'medical': 9329, 'loan': 1199, 'support': 49188}}), ('normal control', {'gross': 12268, 'taxes': 105, 'deductions': [['union', 3130, 3, True], ['parking', 22747, 2, True]], 'arrears': {'union': 1170, 'parking': 19111}}, {'net': 0, 'taken': {'parking': 12163, 'union': 0}, 'arrears': {'parking': 29695, 'union': 4300}})], [('regression', {'gross': 59493, 'taxes': 12677, 'deductions': [['parking', 36655, 2, True], ['union', 22566, 3, True], ['loan', 20538, 1, True], ['medical', 9246, 1, False], ['support', 14303, 1, True]], 'arrears': {'medical': 18889, 'support': 15753}}, {'net': 0, 'taken': {'loan': 20538, 'medical': 0, 'support': 26278, 'parking': 0, 'union': 0}, 'arrears': {'medical': 28135, 'support': 3778, 'parking': 36655, 'union': 22566}}), ('regression', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('partial-repair probe', {'gross': 33954, 'taxes': 3974, 'deductions': [['loan', 33783, 3, True], ['medical', 18835, 2, True], ['support', 24562, 3, True]], 'arrears': {'loan': 14539, 'medical': 11293, 'support': 1936}}, {'net': 0, 'taken': {'medical': 29980, 'loan': 0, 'support': 0}, 'arrears': {'medical': 148, 'loan': 48322, 'support': 26498}}), ('partial-repair probe', {'gross': 46241, 'taxes': 10134, 'deductions': [['charity', 112, 3, True], ['medical', 10972, 3, True], ['support', 1334, 3, True], ['loan', 1505, 3, False]], 'arrears': {'charity': 17330, 'medical': 9649, 'support': 5750, 'loan': 15010}}, {'net': 0, 'taken': {'charity': 17442, 'medical': 18665, 'support': 0, 'loan': 0}, 'arrears': {'medical': 1956, 'support': 7084, 'loan': 16515}}), ('normal control', {'gross': 5973, 'taxes': 1901, 'deductions': [['union', 6319, 2, False], ['charity', 37229, 2, False], ['support', 33286, 1, True], ['loan', 14454, 2, True]], 'arrears': {'support': 19587}}, {'net': 0, 'taken': {'support': 4072, 'union': 0, 'charity': 0, 'loan': 0}, 'arrears': {'support': 48801, 'union': 6319, 'charity': 37229, 'loan': 14454}}), ('normal control', {'gross': 27869, 'taxes': 5642, 'deductions': [['charity', 28332, 2, True]], 'arrears': {'charity': 19203}}, {'net': 0, 'taken': {'charity': 22227}, 'arrears': {'charity': 25308}}), ('normal control', {'gross': 1568, 'taxes': 348, 'deductions': [['parking', 30713, 2, True], ['loan', 23064, 3, False]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 1220, 'loan': 0}, 'arrears': {'parking': 29493, 'loan': 23064}}), ('normal control', {'gross': 2966, 'taxes': 806, 'deductions': [['loan', 19712, 3, False], ['charity', 5272, 1, False], ['parking', 29129, 2, False], ['support', 19215, 1, False], ['union', 19767, 3, False]], 'arrears': {'charity': 11901, 'parking': 13244}}, {'net': 2160, 'taken': {'charity': 0, 'support': 0, 'parking': 0, 'loan': 0, 'union': 0}, 'arrears': {'charity': 17173, 'support': 19215, 'parking': 42373, 'loan': 19712, 'union': 19767}})]]
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):
    avail = max(0, x['gross'] - x['taxes'])
    taken = {}
    arrears = {}
    for name, amt, pri, partial in sorted(x['deductions'], key=lambda d: d[2]):
        due = amt + x['arrears'].get(name, 0)
        if due <= avail:
            take = due
        elif partial:
            take = avail
        else:
            take = 0
        avail -= take
        taken[name] = take
        if take < amt:
            arrears[name] = due - take
    return {'net': avail, 'taken': taken, 'arrears': arrears}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'gross': 49085, 'taxes': 13081, 'deductions': [['medical', 5278, 3, False], ['charity', 17544, 1, True], ['support', 5654, 1, True], ['parking', 38004, 1, False]], 'arrears': {'medical': 17073, 'charity': 11573, 'support': 3632}}, {'net': 0, 'taken': {'charity': 29117, 'support': 6887, 'parking': 0, 'medical': 0}, 'arrears': {'support': 2399, 'parking': 38004, 'medical': 22351}}), ('regression', {'gross': 134653, 'taxes': 27454, 'deductions': [['parking', 26770, 3, False], ['loan', 12315, 2, False], ['union', 3364, 1, False], ['support', 11662, 3, True]], 'arrears': {'parking': 10177, 'loan': 15972, 'support': 6216}}, {'net': 20723, 'taken': {'union': 3364, 'loan': 28287, 'parking': 36947, 'support': 17878}, 'arrears': {}}), ('partial-repair probe', {'gross': 63849, 'taxes': 6027, 'deductions': [['union', 18684, 2, True], ['charity', 28880, 1, True], ['loan', 38844, 2, True], ['support', 20513, 3, False]], 'arrears': {'union': 15372}}, {'net': 0, 'taken': {'charity': 28880, 'union': 28942, 'loan': 0, 'support': 0}, 'arrears': {'union': 5114, 'loan': 38844, 'support': 20513}}), ('partial-repair probe', {'gross': 63883, 'taxes': 16403, 'deductions': [['loan', 28842, 3, False], ['medical', 34809, 2, True]], 'arrears': {'loan': 5346, 'medical': 15166}}, {'net': 0, 'taken': {'medical': 47480, 'loan': 0}, 'arrears': {'medical': 2495, 'loan': 34188}}), ('normal control', {'gross': 6593, 'taxes': 872, 'deductions': [['parking', 5312, 3, False], ['union', 4345, 3, False], ['medical', 14587, 3, False], ['loan', 24540, 2, True], ['support', 7319, 3, True]], 'arrears': {'parking': 388, 'medical': 17218}}, {'net': 0, 'taken': {'loan': 5721, 'parking': 0, 'union': 0, 'medical': 0, 'support': 0}, 'arrears': {'loan': 18819, 'parking': 5700, 'union': 4345, 'medical': 31805, 'support': 7319}}), ('normal control', {'gross': 3515, 'taxes': 674, 'deductions': [['parking', 20962, 1, False], ['union', 18985, 3, True], ['loan', 33701, 1, True]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 0, 'loan': 2841, 'union': 0}, 'arrears': {'parking': 20962, 'loan': 30860, 'union': 18985}}), ('normal control', {'gross': 33426, 'taxes': 5333, 'deductions': [['union', 35082, 2, False], ['loan', 36975, 1, True], ['support', 21693, 3, False], ['parking', 29816, 1, True]], 'arrears': {'union': 18802, 'support': 12060, 'parking': 10514}}, {'net': 0, 'taken': {'loan': 28093, 'parking': 0, 'union': 0, 'support': 0}, 'arrears': {'loan': 8882, 'parking': 40330, 'union': 53884, 'support': 33753}}), ('normal control', {'gross': 18626, 'taxes': 5266, 'deductions': [['union', 19250, 2, False], ['medical', 18064, 1, False], ['parking', 36984, 1, False], ['charity', 38440, 1, True]], 'arrears': {'medical': 7698}}, {'net': 0, 'taken': {'medical': 0, 'parking': 0, 'charity': 13360, 'union': 0}, 'arrears': {'medical': 25762, 'parking': 36984, 'charity': 25080, 'union': 19250}})], [('regression', {'gross': 63849, 'taxes': 6027, 'deductions': [['union', 18684, 2, True], ['charity', 28880, 1, True], ['loan', 38844, 2, True], ['support', 20513, 3, False]], 'arrears': {'union': 15372}}, {'net': 0, 'taken': {'charity': 28880, 'union': 28942, 'loan': 0, 'support': 0}, 'arrears': {'union': 5114, 'loan': 38844, 'support': 20513}}), ('regression', {'gross': 138327, 'taxes': 24358, 'deductions': [['loan', 12004, 3, True], ['union', 4120, 2, True], ['medical', 35331, 3, False], ['parking', 697, 1, True], ['support', 15166, 2, True]], 'arrears': {'loan': 5341, 'union': 19151, 'support': 16971}}, {'net': 5188, 'taken': {'parking': 697, 'union': 23271, 'support': 32137, 'loan': 17345, 'medical': 35331}, 'arrears': {}}), ('partial-repair probe', {'gross': 63883, 'taxes': 16403, 'deductions': [['loan', 28842, 3, False], ['medical', 34809, 2, True]], 'arrears': {'loan': 5346, 'medical': 15166}}, {'net': 0, 'taken': {'medical': 47480, 'loan': 0}, 'arrears': {'medical': 2495, 'loan': 34188}}), ('partial-repair probe', {'gross': 37898, 'taxes': 10470, 'deductions': [['union', 18447, 3, False], ['parking', 30036, 3, True], ['support', 12641, 2, True], ['loan', 39343, 2, False], ['charity', 10333, 2, False]], 'arrears': {'parking': 13057, 'support': 19997}}, {'net': 0, 'taken': {'support': 27428, 'loan': 0, 'charity': 0, 'union': 0, 'parking': 0}, 'arrears': {'support': 5210, 'loan': 39343, 'charity': 10333, 'union': 18447, 'parking': 43093}}), ('normal control', {'gross': 35155, 'taxes': 4541, 'deductions': [['parking', 30989, 1, False], ['support', 32872, 1, False]], 'arrears': {'parking': 10286, 'support': 19476}}, {'net': 30614, 'taken': {'parking': 0, 'support': 0}, 'arrears': {'parking': 41275, 'support': 52348}}), ('normal control', {'gross': 21943, 'taxes': 4843, 'deductions': [['parking', 33670, 3, False], ['charity', 24679, 3, True]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 0, 'charity': 17100}, 'arrears': {'parking': 33670, 'charity': 7579}}), ('normal control', {'gross': 20726, 'taxes': 5501, 'deductions': [['charity', 22176, 1, True], ['medical', 30958, 1, True], ['loan', 15175, 2, True], ['support', 34214, 2, True]], 'arrears': {'support': 1284}}, {'net': 0, 'taken': {'charity': 15225, 'medical': 0, 'loan': 0, 'support': 0}, 'arrears': {'charity': 6951, 'medical': 30958, 'loan': 15175, 'support': 35498}}), ('normal control', {'gross': 15352, 'taxes': 3409, 'deductions': [['charity', 24919, 2, True], ['support', 16527, 2, False], ['loan', 28071, 2, False]], 'arrears': {}}, {'net': 0, 'taken': {'charity': 11943, 'support': 0, 'loan': 0}, 'arrears': {'charity': 12976, 'support': 16527, 'loan': 28071}})], [('regression', {'gross': 75893, 'taxes': 4166, 'deductions': [['parking', 28979, 1, True], ['medical', 32307, 2, True], ['support', 39584, 3, True], ['loan', 36560, 1, True]], 'arrears': {'medical': 4976, 'loan': 12853}}, {'net': 0, 'taken': {'parking': 28979, 'loan': 42748, 'medical': 0, 'support': 0}, 'arrears': {'loan': 6665, 'medical': 37283, 'support': 39584}}), ('regression', {'gross': 102757, 'taxes': 16339, 'deductions': [['medical', 20312, 1, False]], 'arrears': {'medical': 12422}}, {'net': 53684, 'taken': {'medical': 32734}, 'arrears': {}}), ('partial-repair probe', {'gross': 73228, 'taxes': 666, 'deductions': [['loan', 31096, 2, False], ['medical', 18616, 3, False], ['support', 10492, 3, True], ['union', 39510, 1, False], ['charity', 17132, 2, False]], 'arrears': {'loan': 2531, 'support': 15520}}, {'net': 0, 'taken': {'union': 39510, 'loan': 0, 'charity': 17132, 'medical': 0, 'support': 15920}, 'arrears': {'loan': 33627, 'medical': 18616, 'support': 10092}}), ('partial-repair probe', {'gross': 59493, 'taxes': 12677, 'deductions': [['parking', 36655, 2, True], ['union', 22566, 3, True], ['loan', 20538, 1, True], ['medical', 9246, 1, False], ['support', 14303, 1, True]], 'arrears': {'medical': 18889, 'support': 15753}}, {'net': 0, 'taken': {'loan': 20538, 'medical': 0, 'support': 26278, 'parking': 0, 'union': 0}, 'arrears': {'medical': 28135, 'support': 3778, 'parking': 36655, 'union': 22566}}), ('normal control', {'gross': 19093, 'taxes': 472, 'deductions': [['loan', 33282, 1, False], ['union', 29885, 3, False], ['charity', 36812, 3, True], ['support', 18134, 3, True]], 'arrears': {'charity': 8415}}, {'net': 0, 'taken': {'loan': 0, 'union': 0, 'charity': 18621, 'support': 0}, 'arrears': {'loan': 33282, 'union': 29885, 'charity': 26606, 'support': 18134}}), ('normal control', {'gross': 9496, 'taxes': 2152, 'deductions': [['loan', 37495, 2, False], ['union', 25319, 1, True]], 'arrears': {'loan': 5106}}, {'net': 0, 'taken': {'union': 7344, 'loan': 0}, 'arrears': {'union': 17975, 'loan': 42601}}), ('normal control', {'gross': 4210, 'taxes': 944, 'deductions': [['charity', 12281, 3, True], ['loan', 21699, 2, False]], 'arrears': {'loan': 5243}}, {'net': 0, 'taken': {'loan': 0, 'charity': 3266}, 'arrears': {'loan': 26942, 'charity': 9015}}), ('normal control', {'gross': 46504, 'taxes': 13089, 'deductions': [['union', 16705, 3, False], ['charity', 31049, 3, True], ['medical', 10810, 3, True], ['support', 34027, 2, True]], 'arrears': {'union': 13605}}, {'net': 0, 'taken': {'support': 33415, 'union': 0, 'charity': 0, 'medical': 0}, 'arrears': {'support': 612, 'union': 30310, 'charity': 31049, 'medical': 10810}})], [('regression', {'gross': 73228, 'taxes': 666, 'deductions': [['loan', 31096, 2, False], ['medical', 18616, 3, False], ['support', 10492, 3, True], ['union', 39510, 1, False], ['charity', 17132, 2, False]], 'arrears': {'loan': 2531, 'support': 15520}}, {'net': 0, 'taken': {'union': 39510, 'loan': 0, 'charity': 17132, 'medical': 0, 'support': 15920}, 'arrears': {'loan': 33627, 'medical': 18616, 'support': 10092}}), ('regression', {'gross': 106436, 'taxes': 29897, 'deductions': [['parking', 39254, 1, True]], 'arrears': {}}, {'net': 37285, 'taken': {'parking': 39254}, 'arrears': {}}), ('partial-repair probe', {'gross': 59493, 'taxes': 12677, 'deductions': [['parking', 36655, 2, True], ['union', 22566, 3, True], ['loan', 20538, 1, True], ['medical', 9246, 1, False], ['support', 14303, 1, True]], 'arrears': {'medical': 18889, 'support': 15753}}, {'net': 0, 'taken': {'loan': 20538, 'medical': 0, 'support': 26278, 'parking': 0, 'union': 0}, 'arrears': {'medical': 28135, 'support': 3778, 'parking': 36655, 'union': 22566}}), ('partial-repair probe', {'gross': 44674, 'taxes': 14202, 'deductions': [['parking', 23778, 3, True], ['union', 2374, 1, False], ['loan', 38498, 1, False]], 'arrears': {'parking': 6381}}, {'net': 0, 'taken': {'union': 2374, 'loan': 0, 'parking': 28098}, 'arrears': {'loan': 38498, 'parking': 2061}}), ('normal control', {'gross': 770, 'taxes': 57, 'deductions': [['support', 18698, 2, False]], 'arrears': {'support': 10191}}, {'net': 713, 'taken': {'support': 0}, 'arrears': {'support': 28889}}), ('normal control', {'gross': 23798, 'taxes': 1352, 'deductions': [['support', 18645, 2, False]], 'arrears': {'support': 11865}}, {'net': 22446, 'taken': {'support': 0}, 'arrears': {'support': 30510}}), ('normal control', {'gross': 8461, 'taxes': 2779, 'deductions': [['medical', 9329, 2, False], ['support', 30631, 3, True], ['union', 9173, 1, False], ['loan', 6881, 2, True]], 'arrears': {'support': 18557}}, {'net': 0, 'taken': {'union': 0, 'medical': 0, 'loan': 5682, 'support': 0}, 'arrears': {'union': 9173, 'medical': 9329, 'loan': 1199, 'support': 49188}}), ('normal control', {'gross': 12268, 'taxes': 105, 'deductions': [['union', 3130, 3, True], ['parking', 22747, 2, True]], 'arrears': {'union': 1170, 'parking': 19111}}, {'net': 0, 'taken': {'parking': 12163, 'union': 0}, 'arrears': {'parking': 29695, 'union': 4300}})], [('regression', {'gross': 59493, 'taxes': 12677, 'deductions': [['parking', 36655, 2, True], ['union', 22566, 3, True], ['loan', 20538, 1, True], ['medical', 9246, 1, False], ['support', 14303, 1, True]], 'arrears': {'medical': 18889, 'support': 15753}}, {'net': 0, 'taken': {'loan': 20538, 'medical': 0, 'support': 26278, 'parking': 0, 'union': 0}, 'arrears': {'medical': 28135, 'support': 3778, 'parking': 36655, 'union': 22566}}), ('regression', {'gross': 45406, 'taxes': 3941, 'deductions': [['parking', 32509, 3, True], ['loan', 15062, 3, True], ['charity', 31648, 1, False]], 'arrears': {'parking': 1472, 'charity': 15167}}, {'net': 0, 'taken': {'charity': 0, 'parking': 33981, 'loan': 7484}, 'arrears': {'charity': 46815, 'loan': 7578}}), ('partial-repair probe', {'gross': 33954, 'taxes': 3974, 'deductions': [['loan', 33783, 3, True], ['medical', 18835, 2, True], ['support', 24562, 3, True]], 'arrears': {'loan': 14539, 'medical': 11293, 'support': 1936}}, {'net': 0, 'taken': {'medical': 29980, 'loan': 0, 'support': 0}, 'arrears': {'medical': 148, 'loan': 48322, 'support': 26498}}), ('partial-repair probe', {'gross': 46241, 'taxes': 10134, 'deductions': [['charity', 112, 3, True], ['medical', 10972, 3, True], ['support', 1334, 3, True], ['loan', 1505, 3, False]], 'arrears': {'charity': 17330, 'medical': 9649, 'support': 5750, 'loan': 15010}}, {'net': 0, 'taken': {'charity': 17442, 'medical': 18665, 'support': 0, 'loan': 0}, 'arrears': {'medical': 1956, 'support': 7084, 'loan': 16515}}), ('normal control', {'gross': 5973, 'taxes': 1901, 'deductions': [['union', 6319, 2, False], ['charity', 37229, 2, False], ['support', 33286, 1, True], ['loan', 14454, 2, True]], 'arrears': {'support': 19587}}, {'net': 0, 'taken': {'support': 4072, 'union': 0, 'charity': 0, 'loan': 0}, 'arrears': {'support': 48801, 'union': 6319, 'charity': 37229, 'loan': 14454}}), ('normal control', {'gross': 27869, 'taxes': 5642, 'deductions': [['charity', 28332, 2, True]], 'arrears': {'charity': 19203}}, {'net': 0, 'taken': {'charity': 22227}, 'arrears': {'charity': 25308}}), ('normal control', {'gross': 1568, 'taxes': 348, 'deductions': [['parking', 30713, 2, True], ['loan', 23064, 3, False]], 'arrears': {}}, {'net': 0, 'taken': {'parking': 1220, 'loan': 0}, 'arrears': {'parking': 29493, 'loan': 23064}}), ('normal control', {'gross': 2966, 'taxes': 806, 'deductions': [['loan', 19712, 3, False], ['charity', 5272, 1, False], ['parking', 29129, 2, False], ['support', 19215, 1, False], ['union', 19767, 3, False]], 'arrears': {'charity': 11901, 'parking': 13244}}, {'net': 2160, 'taken': {'charity': 0, 'support': 0, 'parking': 0, 'loan': 0, 'union': 0}, 'arrears': {'charity': 17173, 'support': 19215, 'parking': 42373, 'loan': 19712, 'union': 19767}})]]
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:
Fully paid deductions appear in the arrears report with zero balances.

Input {gross, taxes, deductions: [[name, amount, priority, allow_partial]], arrears: {name: cents}}. Available = max(0, gross - taxes). Deductions run by ascending priority, ties in input order. Each is due amount + prior arrears; if due fits it is taken in full, else partial deductions take what is left and all-or-nothing deductions take 0. Any unpaid due becomes new arrears (only positive entries). Return {net, taken, arrears}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
from fractions import Fraction
import math
N = 1
observations = []
def solve(x):
    bands = [(25, 5), (30, 6), (35, 8), (40, 9), (45, 10), (50, 15), (55, 23), (60, 43), (65, 66), (70, 127)]
    rate = 206
    for upper, r in bands:
        if x['age'] <= upper:
            rate = r
            break
    excess = max(0, x['coverage'] - 5000000)
    cost = Fraction(excess * rate * x['months'], 100000)
    imputed = max(0, cost - x['employee_paid'])
    return math.floor(imputed + Fraction(1, 2))
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'coverage': 23127161, 'age': 70, 'months': 4, 'employee_paid': 0}, 149368), ('regression', {'coverage': 16856564, 'age': 70, 'months': 8, 'employee_paid': 0}, 195396), ('partial-repair probe', {'coverage': 31951582, 'age': 24, 'months': 2, 'employee_paid': 0}, 2695), ('partial-repair probe', {'coverage': 26680167, 'age': 24, 'months': 8, 'employee_paid': 0}, 8672), ('normal control', {'coverage': 5000000, 'age': 30, 'months': 6, 'employee_paid': 0}, 0), ('normal control', {'coverage': 7307633, 'age': 24, 'months': 3, 'employee_paid': 19462}, 0), ('normal control', {'coverage': 5000000, 'age': 64, 'months': 3, 'employee_paid': 0}, 0), ('normal control', {'coverage': 5000000, 'age': 24, 'months': 10, 'employee_paid': 0}, 0)], [('regression', {'coverage': 16856564, 'age': 70, 'months': 8, 'employee_paid': 0}, 195396), ('regression', {'coverage': 9181727, 'age': 25, 'months': 6, 'employee_paid': 0}, 1505), ('partial-repair probe', {'coverage': 20685038, 'age': 29, 'months': 4, 'employee_paid': 4881}, 0), ('partial-repair probe', {'coverage': 20847001, 'age': 64, 'months': 8, 'employee_paid': 0}, 83672), ('normal control', {'coverage': 4843457, 'age': 30, 'months': 9, 'employee_paid': 37040}, 0), ('normal control', {'coverage': 19400395, 'age': 25, 'months': 7, 'employee_paid': 36789}, 0), ('normal control', {'coverage': 5000000, 'age': 25, 'months': 6, 'employee_paid': 0}, 0), ('normal control', {'coverage': 5000000, 'age': 65, 'months': 8, 'employee_paid': 12759}, 0)], [('regression', {'coverage': 9181727, 'age': 25, 'months': 6, 'employee_paid': 0}, 1505), ('regression', {'coverage': 15762067, 'age': 70, 'months': 10, 'employee_paid': 0}, 221699), ('partial-repair probe', {'coverage': 6128065, 'age': 64, 'months': 12, 'employee_paid': 8257}, 677), ('partial-repair probe', {'coverage': 25191576, 'age': 24, 'months': 5, 'employee_paid': 4142}, 906), ('normal control', {'coverage': 5000000, 'age': 61, 'months': 12, 'employee_paid': 2761}, 0), ('normal control', {'coverage': 22529249, 'age': 29, 'months': 1, 'employee_paid': 38785}, 0), ('normal control', {'coverage': 15471033, 'age': 66, 'months': 5, 'employee_paid': 0}, 66491), ('normal control', {'coverage': 5000000, 'age': 50, 'months': 9, 'employee_paid': 0}, 0)], [('regression', {'coverage': 15762067, 'age': 70, 'months': 10, 'employee_paid': 0}, 221699), ('regression', {'coverage': 45748038, 'age': 25, 'months': 3, 'employee_paid': 2480}, 4855), ('partial-repair probe', {'coverage': 18671520, 'age': 24, 'months': 12, 'employee_paid': 0}, 8203), ('partial-repair probe', {'coverage': 27955665, 'age': 24, 'months': 10, 'employee_paid': 5575}, 5903), ('normal control', {'coverage': 9261111, 'age': 29, 'months': 1, 'employee_paid': 24425}, 0), ('normal control', {'coverage': 20304981, 'age': 29, 'months': 10, 'employee_paid': 12373}, 0), ('normal control', {'coverage': 5000000, 'age': 64, 'months': 12, 'employee_paid': 0}, 0), ('normal control', {'coverage': 5000000, 'age': 27, 'months': 10, 'employee_paid': 0}, 0)], [('regression', {'coverage': 45748038, 'age': 25, 'months': 3, 'employee_paid': 2480}, 4855), ('regression', {'coverage': 46403884, 'age': 30, 'months': 4, 'employee_paid': 0}, 13249), ('partial-repair probe', {'coverage': 9682341, 'age': 49, 'months': 2, 'employee_paid': 0}, 1405), ('partial-repair probe', {'coverage': 11740179, 'age': 64, 'months': 10, 'employee_paid': 0}, 44485), ('normal control', {'coverage': 635952, 'age': 25, 'months': 4, 'employee_paid': 16597}, 0), ('normal control', {'coverage': 22006788, 'age': 66, 'months': 9, 'employee_paid': 6358}, 188030), ('normal control', {'coverage': 5000000, 'age': 29, 'months': 2, 'employee_paid': 22644}, 0), ('normal control', {'coverage': 15550480, 'age': 24, 'months': 6, 'employee_paid': 26462}, 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
from fractions import Fraction
import math
N = 1
observations = []
def solve(x):
    bands = [(25, 5), (30, 6), (35, 8), (40, 9), (45, 10), (50, 15), (55, 23), (60, 43), (65, 66), (70, 127)]
    rate = 206
    for upper, r in bands:
        if x['age'] + 1 < upper:
            rate = r
            break
    excess = max(0, x['coverage'] - 5000000)
    cost = Fraction(excess * rate * x['months'], 100000)
    imputed = max(0, cost - x['employee_paid'])
    return math.floor(imputed + Fraction(1, 2))
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'coverage': 23127161, 'age': 70, 'months': 4, 'employee_paid': 0}, 149368), ('regression', {'coverage': 16856564, 'age': 70, 'months': 8, 'employee_paid': 0}, 195396), ('partial-repair probe', {'coverage': 31951582, 'age': 24, 'months': 2, 'employee_paid': 0}, 2695), ('partial-repair probe', {'coverage': 26680167, 'age': 24, 'months': 8, 'employee_paid': 0}, 8672), ('normal control', {'coverage': 5000000, 'age': 30, 'months': 6, 'employee_paid': 0}, 0), ('normal control', {'coverage': 7307633, 'age': 24, 'months': 3, 'employee_paid': 19462}, 0), ('normal control', {'coverage': 5000000, 'age': 64, 'months': 3, 'employee_paid': 0}, 0), ('normal control', {'coverage': 5000000, 'age': 24, 'months': 10, 'employee_paid': 0}, 0)], [('regression', {'coverage': 16856564, 'age': 70, 'months': 8, 'employee_paid': 0}, 195396), ('regression', {'coverage': 9181727, 'age': 25, 'months': 6, 'employee_paid': 0}, 1505), ('partial-repair probe', {'coverage': 20685038, 'age': 29, 'months': 4, 'employee_paid': 4881}, 0), ('partial-repair probe', {'coverage': 20847001, 'age': 64, 'months': 8, 'employee_paid': 0}, 83672), ('normal control', {'coverage': 4843457, 'age': 30, 'months': 9, 'employee_paid': 37040}, 0), ('normal control', {'coverage': 19400395, 'age': 25, 'months': 7, 'employee_paid': 36789}, 0), ('normal control', {'coverage': 5000000, 'age': 25, 'months': 6, 'employee_paid': 0}, 0), ('normal control', {'coverage': 5000000, 'age': 65, 'months': 8, 'employee_paid': 12759}, 0)], [('regression', {'coverage': 9181727, 'age': 25, 'months': 6, 'employee_paid': 0}, 1505), ('regression', {'coverage': 15762067, 'age': 70, 'months': 10, 'employee_paid': 0}, 221699), ('partial-repair probe', {'coverage': 6128065, 'age': 64, 'months': 12, 'employee_paid': 8257}, 677), ('partial-repair probe', {'coverage': 25191576, 'age': 24, 'months': 5, 'employee_paid': 4142}, 906), ('normal control', {'coverage': 5000000, 'age': 61, 'months': 12, 'employee_paid': 2761}, 0), ('normal control', {'coverage': 22529249, 'age': 29, 'months': 1, 'employee_paid': 38785}, 0), ('normal control', {'coverage': 15471033, 'age': 66, 'months': 5, 'employee_paid': 0}, 66491), ('normal control', {'coverage': 5000000, 'age': 50, 'months': 9, 'employee_paid': 0}, 0)], [('regression', {'coverage': 15762067, 'age': 70, 'months': 10, 'employee_paid': 0}, 221699), ('regression', {'coverage': 45748038, 'age': 25, 'months': 3, 'employee_paid': 2480}, 4855), ('partial-repair probe', {'coverage': 18671520, 'age': 24, 'months': 12, 'employee_paid': 0}, 8203), ('partial-repair probe', {'coverage': 27955665, 'age': 24, 'months': 10, 'employee_paid': 5575}, 5903), ('normal control', {'coverage': 9261111, 'age': 29, 'months': 1, 'employee_paid': 24425}, 0), ('normal control', {'coverage': 20304981, 'age': 29, 'months': 10, 'employee_paid': 12373}, 0), ('normal control', {'coverage': 5000000, 'age': 64, 'months': 12, 'employee_paid': 0}, 0), ('normal control', {'coverage': 5000000, 'age': 27, 'months': 10, 'employee_paid': 0}, 0)], [('regression', {'coverage': 45748038, 'age': 25, 'months': 3, 'employee_paid': 2480}, 4855), ('regression', {'coverage': 46403884, 'age': 30, 'months': 4, 'employee_paid': 0}, 13249), ('partial-repair probe', {'coverage': 9682341, 'age': 49, 'months': 2, 'employee_paid': 0}, 1405), ('partial-repair probe', {'coverage': 11740179, 'age': 64, 'months': 10, 'employee_paid': 0}, 44485), ('normal control', {'coverage': 635952, 'age': 25, 'months': 4, 'employee_paid': 16597}, 0), ('normal control', {'coverage': 22006788, 'age': 66, 'months': 9, 'employee_paid': 6358}, 188030), ('normal control', {'coverage': 5000000, 'age': 29, 'months': 2, 'employee_paid': 22644}, 0), ('normal control', {'coverage': 15550480, 'age': 24, 'months': 6, 'employee_paid': 26462}, 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:
Employees exactly at a band's starting age are charged the younger band's rate.

Input {coverage cents, age, months, employee_paid}. Monthly cost per 1,000.00 of coverage by age band (<25:5, <30:6, <35:8, <40:9, <45:10, <50:15, <55:23, <60:43, <65:66, <70:127, else 206 cents). Excess = max(0, coverage - 50,000.00). Imputed = max(0, excess/100000*rate*months - employee_paid), half-up. Return imputed cents.

split: open-access