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

N = 1
observations = []
def solve(x):
    PAIRS = (('ic_receivable', 'ic_payable'), ('ic_sales', 'ic_purchases'))
    PAIRED = {a for p in PAIRS for a in p}
    cons = {}
    ic = {}
    for ent, acct, cp, amt in x:
        if cp is None or cp == ent or acct not in PAIRED:
            cons[acct] = cons.get(acct, 0) + amt
        else:
            ic[(ent, acct, cp)] = ic.get((ent, acct, cp), 0) + amt
    keys = set()
    for (ent, acct, cp) in ic:
        for left, right in PAIRS:
            if acct == left:
                keys.add((ent, cp, left, right))
            elif acct == right:
                keys.add((cp, ent, left, right))
    mism = []
    for ent, cp, left, right in sorted(keys):
        a = ic.get((ent, left, cp), 0)
        b = ic.get((cp, right, ent), 0)
        m = min(a, b)
        if a != b:
            mism.append([ent, cp, left, a - b])
        if a - m:
            cons[left] = cons.get(left, 0) + a - m
        if b - m:
            cons[right] = cons.get(right, 0) + b - m
    return {'consolidated': {k: v for k, v in sorted(cons.items()) }, 'mismatches': mism}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: zero balance filter', [['S1', 'ic_sales', 'S2', 400], ['P', 'sales', None, 400], ['S1', 'ic_payable', 'S1', 250], ['S1', 'ic_purchases', 'S2', 400], ['S2', 'sales', None, -400], ['S2', 'ic_payable', 'S2', 100], ['S2', 'ic_payable', 'S1', 100]], {'consolidated': {'ic_payable': 450, 'ic_purchases': 400, 'ic_sales': 400}, 'mismatches': [['S1', 'S2', 'ic_receivable', -100], ['S1', 'S2', 'ic_sales', 400], ['S2', 'S1', 'ic_sales', -400]]}], ['regression: zero balance filter, partial-repair probe', [['S1', 'ic_purchases', 'S2', 1000], ['S1', 'ic_loan', 'P', 250], ['S2', 'sales', None, -100]], {'consolidated': {'ic_loan': 250, 'ic_purchases': 1000, 'sales': -100}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 1', [['P', 'ic_loan', 'S1', 400], ['S1', 'ic_loan', 'S1', 400], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_sales', 'P', 400], ['P', 'ic_purchases', 'S1', 400], ['S2', 'ic_sales', 'S1', 250]], {'consolidated': {'ic_loan': 900, 'ic_purchases': 400, 'ic_sales': 650}, 'mismatches': [['S1', 'P', 'ic_sales', -400], ['S2', 'S1', 'ic_sales', 250]]}], ['control 2', [['P', 'ic_purchases', 'S2', 100], ['P', 'ic_receivable', 'P', 400], ['S2', 'cash', None, 400], ['P', 'ic_receivable', 'P', 100], ['P', 'ic_receivable', None, 400], ['S2', 'ic_receivable', None, -400], ['S1', 'ic_payable', 'P', 100]], {'consolidated': {'cash': 400, 'ic_payable': 100, 'ic_purchases': 100, 'ic_receivable': 500}, 'mismatches': [['P', 'S1', 'ic_receivable', -100], ['S2', 'P', 'ic_sales', -100]]}], ['control 3', [['S2', 'ic_receivable', 'P', 1000], ['P', 'ic_payable', 'S1', 400], ['S2', 'ic_purchases', 'S1', 100]], {'consolidated': {'ic_payable': 400, 'ic_purchases': 100, 'ic_receivable': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', -400], ['S1', 'S2', 'ic_sales', -100], ['S2', 'P', 'ic_receivable', 1000]]}], ['control 4', [['S1', 'ic_purchases', 'S2', 250], ['S2', 'ic_payable', 'S2', 100], ['P', 'ic_sales', 'S2', 250], ['S1', 'ic_payable', 'S2', 100], ['S1', 'ic_loan', 'S1', 250], ['S1', 'ic_purchases', 'S2', 400], ['S2', 'cash', None, 400]], {'consolidated': {'cash': 400, 'ic_loan': 250, 'ic_payable': 200, 'ic_purchases': 650, 'ic_sales': 250}, 'mismatches': [['P', 'S2', 'ic_sales', 250], ['S2', 'S1', 'ic_receivable', -100], ['S2', 'S1', 'ic_sales', -650]]}], ['control 5', [['P', 'ic_loan', 'S1', 250], ['S1', 'ic_payable', 'S1', 100], ['S2', 'sales', None, 1000], ['S1', 'ic_receivable', 'P', 250], ['P', 'ic_loan', 'P', 400], ['S1', 'ic_loan', 'P', 400]], {'consolidated': {'ic_loan': 1050, 'ic_payable': 100, 'ic_receivable': 250, 'sales': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', 250]]}]], [['regression: zero balance filter', [['P', 'cash', None, 400], ['S2', 'sales', None, -250], ['S1', 'ic_payable', 'S2', 400], ['S1', 'ic_receivable', 'S2', 100], ['P', 'sales', None, 250], ['P', 'ic_receivable', 'S2', 1000]], {'consolidated': {'cash': 400, 'ic_payable': 400, 'ic_receivable': 1100}, 'mismatches': [['P', 'S2', 'ic_receivable', 1000], ['S1', 'S2', 'ic_receivable', 100], ['S2', 'S1', 'ic_receivable', -400]]}], ['regression: zero balance filter, partial-repair probe', [['S1', 'ic_loan', 'S2', 400], ['P', 'ic_receivable', 'P', 400], ['S2', 'sales', None, -250], ['S2', 'ic_sales', 'P', 100], ['P', 'ic_receivable', 'S1', 1000]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 1400, 'ic_sales': 100, 'sales': -250}, 'mismatches': [['P', 'S1', 'ic_receivable', 1000], ['S2', 'P', 'ic_sales', 100]]}], ['control 1', [['S1', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'S1', 400], ['S2', 'ic_purchases', 'S1', 1000]], {'consolidated': {'ic_purchases': 1000, 'ic_sales': 800}, 'mismatches': [['S1', 'P', 'ic_sales', 400], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'S1', 'ic_sales', 400]]}], ['control 2', [['S1', 'cash', None, 400], ['P', 'ic_receivable', 'S1', 100], ['S1', 'ic_sales', 'P', 100]], {'consolidated': {'cash': 400, 'ic_receivable': 100, 'ic_sales': 100}, 'mismatches': [['P', 'S1', 'ic_receivable', 100], ['S1', 'P', 'ic_sales', 100]]}], ['control 3', [['S1', 'ic_sales', 'S2', 250], ['P', 'ic_receivable', 'S1', 400], ['S1', 'ic_loan', 'S2', 400]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 400, 'ic_sales': 250}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S1', 'S2', 'ic_sales', 250]]}], ['control 4', [['S1', 'ic_receivable', None, 100], ['P', 'ic_loan', 'P', 1000], ['S2', 'ic_receivable', 'S1', 100], ['S1', 'ic_payable', 'S2', 400], ['S1', 'ic_sales', 'P', 250]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 300, 'ic_receivable': 100, 'ic_sales': 250}, 'mismatches': [['S1', 'P', 'ic_sales', 250], ['S2', 'S1', 'ic_receivable', -300]]}], ['control 5', [['S2', 'ic_sales', 'P', 250], ['P', 'ic_payable', 'S2', 250], ['P', 'ic_loan', 'S2', 1000], ['S2', 'ic_sales', 'S2', 400], ['P', 'sales', None, 1000], ['S2', 'cash', None, 250], ['P', 'ic_sales', 'S2', 250], ['P', 'ic_purchases', 'S1', 400]], {'consolidated': {'cash': 250, 'ic_loan': 1000, 'ic_payable': 250, 'ic_purchases': 400, 'ic_sales': 900, 'sales': 1000}, 'mismatches': [['P', 'S2', 'ic_sales', 250], ['S1', 'P', 'ic_sales', -400], ['S2', 'P', 'ic_receivable', -250], ['S2', 'P', 'ic_sales', 250]]}]], [['regression: zero balance filter', [['S2', 'sales', None, -250], ['S2', 'ic_receivable', 'S1', 400], ['S2', 'cash', None, -400], ['P', 'ic_receivable', 'S1', 400], ['S2', 'ic_payable', 'S2', 400], ['S1', 'ic_sales', 'P', 400], ['S1', 'cash', None, 400]], {'consolidated': {'ic_payable': 400, 'ic_receivable': 800, 'ic_sales': 400, 'sales': -250}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S1', 'P', 'ic_sales', 400], ['S2', 'S1', 'ic_receivable', 400]]}], ['control 1', [['S2', 'ic_payable', 'P', 250], ['S2', 'ic_loan', 'S2', 1000], ['P', 'ic_loan', 'P', 250], ['S1', 'ic_receivable', 'S2', 400], ['P', 'ic_receivable', 'S1', 250], ['S2', 'ic_sales', 'P', 1000]], {'consolidated': {'ic_loan': 1250, 'ic_payable': 250, 'ic_receivable': 650, 'ic_sales': 1000}, 'mismatches': [['P', 'S1', 'ic_receivable', 250], ['P', 'S2', 'ic_receivable', -250], ['S1', 'S2', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 1000]]}], ['control 2', [['P', 'ic_receivable', None, -1000], ['P', 'ic_payable', 'P', 100], ['S1', 'ic_sales', 'S1', 400], ['S1', 'ic_payable', 'S2', 250], ['P', 'ic_sales', 'P', 100], ['S2', 'ic_receivable', 'S1', 400]], {'consolidated': {'ic_payable': 100, 'ic_receivable': -850, 'ic_sales': 500}, 'mismatches': [['S2', 'S1', 'ic_receivable', 150]]}], ['control 3', [['S1', 'ic_payable', 'S1', 100], ['S2', 'ic_payable', 'S1', 100], ['S1', 'ic_sales', 'S2', 1000], ['P', 'ic_receivable', 'P', 250], ['S2', 'ic_purchases', 'S2', 100], ['S1', 'ic_payable', 'S2', 250], ['S1', 'ic_purchases', 'P', 100]], {'consolidated': {'ic_payable': 450, 'ic_purchases': 200, 'ic_receivable': 250, 'ic_sales': 1000}, 'mismatches': [['P', 'S1', 'ic_sales', -100], ['S1', 'S2', 'ic_receivable', -100], ['S1', 'S2', 'ic_sales', 1000], ['S2', 'S1', 'ic_receivable', -250]]}], ['control 4', [['P', 'ic_loan', 'P', 1000], ['S2', 'ic_sales', 'S2', 1000], ['S2', 'ic_payable', 'P', 400], ['S1', 'ic_loan', 'P', 1000], ['S1', 'ic_receivable', 'P', 1000], ['P', 'cash', None, 400], ['S2', 'ic_purchases', 'S1', 400], ['S1', 'ic_loan', 'S1', 100]], {'consolidated': {'cash': 400, 'ic_loan': 2100, 'ic_payable': 400, 'ic_purchases': 400, 'ic_receivable': 1000, 'ic_sales': 1000}, 'mismatches': [['P', 'S2', 'ic_receivable', -400], ['S1', 'P', 'ic_receivable', 1000], ['S1', 'S2', 'ic_sales', -400]]}], ['control 5', [['S1', 'ic_payable', 'P', 1000], ['S1', 'ic_loan', 'S1', 1000], ['P', 'ic_sales', 'S1', 400]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 1000, 'ic_sales': 400}, 'mismatches': [['P', 'S1', 'ic_receivable', -1000], ['P', 'S1', 'ic_sales', 400]]}], ['control 6', [['S2', 'ic_purchases', 'S1', 250], ['S2', 'ic_loan', 'S1', 100], ['P', 'ic_loan', 'S2', 250]], {'consolidated': {'ic_loan': 350, 'ic_purchases': 250}, 'mismatches': [['S1', 'S2', 'ic_sales', -250]]}]], [['regression: zero balance filter', [['S1', 'sales', None, -400], ['S1', 'ic_payable', 'P', 400], ['P', 'ic_loan', 'S2', 1000], ['S1', 'ic_receivable', 'S2', 1000], ['S2', 'ic_receivable', None, -1000], ['P', 'ic_sales', 'P', 400]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 400, 'ic_sales': 400, 'sales': -400}, 'mismatches': [['P', 'S1', 'ic_receivable', -400], ['S1', 'S2', 'ic_receivable', 1000]]}], ['control 1', [['S1', 'cash', None, 400], ['P', 'sales', None, -400], ['S1', 'ic_receivable', None, 1000]], {'consolidated': {'cash': 400, 'ic_receivable': 1000, 'sales': -400}, 'mismatches': []}], ['control 2', [['P', 'ic_payable', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['S1', 'sales', None, -250], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_payable', 'S2', 400], ['P', 'ic_purchases', 'P', 100]], {'consolidated': {'ic_loan': 100, 'ic_payable': 800, 'ic_purchases': 500, 'sales': -250}, 'mismatches': [['S2', 'P', 'ic_receivable', -800]]}], ['control 3', [['S2', 'ic_sales', 'P', 100], ['P', 'ic_receivable', 'P', 100], ['S1', 'ic_payable', 'S1', 100], ['S2', 'cash', None, -400], ['S2', 'ic_purchases', 'P', 1000], ['P', 'ic_sales', 'P', 100], ['S1', 'ic_loan', 'S1', 1000], ['S1', 'ic_purchases', 'P', 100]], {'consolidated': {'cash': -400, 'ic_loan': 1000, 'ic_payable': 100, 'ic_purchases': 1100, 'ic_receivable': 100, 'ic_sales': 200}, 'mismatches': [['P', 'S1', 'ic_sales', -100], ['P', 'S2', 'ic_sales', -1000], ['S2', 'P', 'ic_sales', 100]]}], ['control 4', [['S2', 'ic_loan', 'S1', 1000], ['S2', 'ic_payable', 'S1', 100], ['S2', 'ic_loan', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['S1', 'sales', None, 400], ['P', 'sales', None, 250], ['P', 'ic_loan', 'S1', 100]], {'consolidated': {'ic_loan': 1500, 'ic_payable': 100, 'ic_purchases': 400, 'sales': 650}, 'mismatches': [['S1', 'S2', 'ic_receivable', -100]]}], ['control 5', [['S2', 'ic_payable', 'P', 1000], ['P', 'ic_sales', 'P', 400], ['S1', 'ic_purchases', 'S1', 100], ['P', 'ic_loan', 'S2', 400], ['P', 'ic_purchases', 'S2', 100], ['S2', 'ic_purchases', 'S2', 100], ['P', 'cash', None, -100]], {'consolidated': {'cash': -100, 'ic_loan': 400, 'ic_payable': 1000, 'ic_purchases': 300, 'ic_sales': 400}, 'mismatches': [['P', 'S2', 'ic_receivable', -1000], ['S2', 'P', 'ic_sales', -100]]}], ['control 6', [['S2', 'ic_receivable', None, -400], ['S2', 'ic_payable', 'S1', 400], ['S2', 'ic_receivable', None, 100], ['S1', 'ic_payable', 'S2', 1000], ['S2', 'ic_sales', 'S2', 250], ['S2', 'ic_payable', 'S2', 100], ['S2', 'cash', None, 400], ['S2', 'ic_sales', 'S2', 250]], {'consolidated': {'cash': 400, 'ic_payable': 1500, 'ic_receivable': -300, 'ic_sales': 500}, 'mismatches': [['S1', 'S2', 'ic_receivable', -400], ['S2', 'S1', 'ic_receivable', -1000]]}]], [['regression: zero balance filter', [['P', 'ic_payable', 'S1', 100], ['S1', 'cash', None, 400], ['S1', 'ic_receivable', None, 1000], ['P', 'ic_receivable', 'P', 400], ['P', 'ic_loan', 'S2', 100], ['S1', 'cash', None, -400], ['S2', 'ic_loan', 'S2', 1000], ['S2', 'ic_receivable', None, 400]], {'consolidated': {'ic_loan': 1100, 'ic_payable': 100, 'ic_receivable': 1800}, 'mismatches': [['S1', 'P', 'ic_receivable', -100]]}], ['regression: zero balance filter, partial-repair probe', [['S2', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'P', 250], ['S2', 'ic_payable', 'S1', 400], ['S1', 'ic_purchases', 'P', 1000], ['P', 'ic_loan', 'S1', 100], ['S2', 'ic_receivable', 'P', 400], ['S2', 'cash', None, -1000]], {'consolidated': {'cash': -1000, 'ic_loan': 100, 'ic_payable': 400, 'ic_purchases': 1000, 'ic_receivable': 400, 'ic_sales': 650}, 'mismatches': [['P', 'S1', 'ic_sales', -1000], ['S1', 'S2', 'ic_receivable', -400], ['S2', 'P', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 650]]}], ['control 1', [['P', 'ic_purchases', 'S1', 100], ['S1', 'ic_sales', 'P', 400], ['S2', 'ic_loan', 'S2', 1000], ['P', 'cash', None, 100]], {'consolidated': {'cash': 100, 'ic_loan': 1000, 'ic_sales': 300}, 'mismatches': [['S1', 'P', 'ic_sales', 300]]}], ['control 2', [['S1', 'ic_purchases', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['P', 'ic_payable', 'S1', 400], ['S1', 'ic_sales', 'S1', 1000]], {'consolidated': {'ic_payable': 400, 'ic_purchases': 800, 'ic_sales': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', -400], ['S2', 'S1', 'ic_sales', -400]]}], ['control 3', [['S1', 'ic_sales', 'P', 100], ['S2', 'sales', None, 400], ['S1', 'ic_loan', 'S1', 100], ['P', 'ic_sales', 'P', 100], ['S2', 'cash', None, 100], ['S2', 'ic_purchases', 'S1', 100], ['S2', 'ic_purchases', 'S1', 1000]], {'consolidated': {'cash': 100, 'ic_loan': 100, 'ic_purchases': 1100, 'ic_sales': 200, 'sales': 400}, 'mismatches': [['S1', 'P', 'ic_sales', 100], ['S1', 'S2', 'ic_sales', -1100]]}], ['control 4', [['S2', 'ic_loan', 'P', 400], ['P', 'cash', None, 1000], ['P', 'ic_purchases', 'S1', 400], ['S2', 'ic_loan', 'S1', 1000], ['S1', 'cash', None, -100], ['S1', 'ic_purchases', 'S1', 400]], {'consolidated': {'cash': 900, 'ic_loan': 1400, 'ic_purchases': 800}, 'mismatches': [['S1', 'P', 'ic_sales', -400]]}], ['control 5', [['P', 'ic_purchases', 'S2', 250], ['S1', 'ic_sales', 'P', 1000], ['S2', 'ic_payable', 'S1', 250], ['S2', 'ic_loan', 'S1', 1000]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 250, 'ic_purchases': 250, 'ic_sales': 1000}, 'mismatches': [['S1', 'P', 'ic_sales', 1000], ['S1', 'S2', 'ic_receivable', -250], ['S2', 'P', 'ic_sales', -250]]}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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):
    PAIRS = (('ic_receivable', 'ic_payable'), ('ic_sales', 'ic_purchases'))
    PAIRED = {a for p in PAIRS for a in p}
    cons = {}
    ic = {}
    for ent, acct, cp, amt in x:
        if cp is None or cp == ent or acct not in PAIRED:
            cons[acct] = cons.get(acct, 0) + amt
        else:
            ic[(ent, acct, cp)] = ic.get((ent, acct, cp), 0) + amt
    keys = set()
    for (ent, acct, cp) in ic:
        for left, right in PAIRS:
            if acct == left:
                keys.add((ent, cp, left, right))
            elif acct == right:
                keys.add((cp, ent, left, right))
    mism = []
    for ent, cp, left, right in sorted(keys):
        a = ic.get((ent, left, cp), 0)
        b = ic.get((cp, right, ent), 0)
        m = min(a, b)
        if a != b:
            mism.append([ent, cp, left, a - b])
        if a - m:
            cons[left] = cons.get(left, 0) + a - m
        if b - m:
            cons[right] = cons.get(right, 0) + b - m
    return {'consolidated': {k: v for k, v in sorted(cons.items()) if v > 0}, 'mismatches': mism}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: zero balance filter', [['S1', 'ic_sales', 'S2', 400], ['P', 'sales', None, 400], ['S1', 'ic_payable', 'S1', 250], ['S1', 'ic_purchases', 'S2', 400], ['S2', 'sales', None, -400], ['S2', 'ic_payable', 'S2', 100], ['S2', 'ic_payable', 'S1', 100]], {'consolidated': {'ic_payable': 450, 'ic_purchases': 400, 'ic_sales': 400}, 'mismatches': [['S1', 'S2', 'ic_receivable', -100], ['S1', 'S2', 'ic_sales', 400], ['S2', 'S1', 'ic_sales', -400]]}], ['regression: zero balance filter, partial-repair probe', [['S1', 'ic_purchases', 'S2', 1000], ['S1', 'ic_loan', 'P', 250], ['S2', 'sales', None, -100]], {'consolidated': {'ic_loan': 250, 'ic_purchases': 1000, 'sales': -100}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 1', [['P', 'ic_loan', 'S1', 400], ['S1', 'ic_loan', 'S1', 400], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_sales', 'P', 400], ['P', 'ic_purchases', 'S1', 400], ['S2', 'ic_sales', 'S1', 250]], {'consolidated': {'ic_loan': 900, 'ic_purchases': 400, 'ic_sales': 650}, 'mismatches': [['S1', 'P', 'ic_sales', -400], ['S2', 'S1', 'ic_sales', 250]]}], ['control 2', [['P', 'ic_purchases', 'S2', 100], ['P', 'ic_receivable', 'P', 400], ['S2', 'cash', None, 400], ['P', 'ic_receivable', 'P', 100], ['P', 'ic_receivable', None, 400], ['S2', 'ic_receivable', None, -400], ['S1', 'ic_payable', 'P', 100]], {'consolidated': {'cash': 400, 'ic_payable': 100, 'ic_purchases': 100, 'ic_receivable': 500}, 'mismatches': [['P', 'S1', 'ic_receivable', -100], ['S2', 'P', 'ic_sales', -100]]}], ['control 3', [['S2', 'ic_receivable', 'P', 1000], ['P', 'ic_payable', 'S1', 400], ['S2', 'ic_purchases', 'S1', 100]], {'consolidated': {'ic_payable': 400, 'ic_purchases': 100, 'ic_receivable': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', -400], ['S1', 'S2', 'ic_sales', -100], ['S2', 'P', 'ic_receivable', 1000]]}], ['control 4', [['S1', 'ic_purchases', 'S2', 250], ['S2', 'ic_payable', 'S2', 100], ['P', 'ic_sales', 'S2', 250], ['S1', 'ic_payable', 'S2', 100], ['S1', 'ic_loan', 'S1', 250], ['S1', 'ic_purchases', 'S2', 400], ['S2', 'cash', None, 400]], {'consolidated': {'cash': 400, 'ic_loan': 250, 'ic_payable': 200, 'ic_purchases': 650, 'ic_sales': 250}, 'mismatches': [['P', 'S2', 'ic_sales', 250], ['S2', 'S1', 'ic_receivable', -100], ['S2', 'S1', 'ic_sales', -650]]}], ['control 5', [['P', 'ic_loan', 'S1', 250], ['S1', 'ic_payable', 'S1', 100], ['S2', 'sales', None, 1000], ['S1', 'ic_receivable', 'P', 250], ['P', 'ic_loan', 'P', 400], ['S1', 'ic_loan', 'P', 400]], {'consolidated': {'ic_loan': 1050, 'ic_payable': 100, 'ic_receivable': 250, 'sales': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', 250]]}]], [['regression: zero balance filter', [['P', 'cash', None, 400], ['S2', 'sales', None, -250], ['S1', 'ic_payable', 'S2', 400], ['S1', 'ic_receivable', 'S2', 100], ['P', 'sales', None, 250], ['P', 'ic_receivable', 'S2', 1000]], {'consolidated': {'cash': 400, 'ic_payable': 400, 'ic_receivable': 1100}, 'mismatches': [['P', 'S2', 'ic_receivable', 1000], ['S1', 'S2', 'ic_receivable', 100], ['S2', 'S1', 'ic_receivable', -400]]}], ['regression: zero balance filter, partial-repair probe', [['S1', 'ic_loan', 'S2', 400], ['P', 'ic_receivable', 'P', 400], ['S2', 'sales', None, -250], ['S2', 'ic_sales', 'P', 100], ['P', 'ic_receivable', 'S1', 1000]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 1400, 'ic_sales': 100, 'sales': -250}, 'mismatches': [['P', 'S1', 'ic_receivable', 1000], ['S2', 'P', 'ic_sales', 100]]}], ['control 1', [['S1', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'S1', 400], ['S2', 'ic_purchases', 'S1', 1000]], {'consolidated': {'ic_purchases': 1000, 'ic_sales': 800}, 'mismatches': [['S1', 'P', 'ic_sales', 400], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'S1', 'ic_sales', 400]]}], ['control 2', [['S1', 'cash', None, 400], ['P', 'ic_receivable', 'S1', 100], ['S1', 'ic_sales', 'P', 100]], {'consolidated': {'cash': 400, 'ic_receivable': 100, 'ic_sales': 100}, 'mismatches': [['P', 'S1', 'ic_receivable', 100], ['S1', 'P', 'ic_sales', 100]]}], ['control 3', [['S1', 'ic_sales', 'S2', 250], ['P', 'ic_receivable', 'S1', 400], ['S1', 'ic_loan', 'S2', 400]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 400, 'ic_sales': 250}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S1', 'S2', 'ic_sales', 250]]}], ['control 4', [['S1', 'ic_receivable', None, 100], ['P', 'ic_loan', 'P', 1000], ['S2', 'ic_receivable', 'S1', 100], ['S1', 'ic_payable', 'S2', 400], ['S1', 'ic_sales', 'P', 250]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 300, 'ic_receivable': 100, 'ic_sales': 250}, 'mismatches': [['S1', 'P', 'ic_sales', 250], ['S2', 'S1', 'ic_receivable', -300]]}], ['control 5', [['S2', 'ic_sales', 'P', 250], ['P', 'ic_payable', 'S2', 250], ['P', 'ic_loan', 'S2', 1000], ['S2', 'ic_sales', 'S2', 400], ['P', 'sales', None, 1000], ['S2', 'cash', None, 250], ['P', 'ic_sales', 'S2', 250], ['P', 'ic_purchases', 'S1', 400]], {'consolidated': {'cash': 250, 'ic_loan': 1000, 'ic_payable': 250, 'ic_purchases': 400, 'ic_sales': 900, 'sales': 1000}, 'mismatches': [['P', 'S2', 'ic_sales', 250], ['S1', 'P', 'ic_sales', -400], ['S2', 'P', 'ic_receivable', -250], ['S2', 'P', 'ic_sales', 250]]}]], [['regression: zero balance filter', [['S2', 'sales', None, -250], ['S2', 'ic_receivable', 'S1', 400], ['S2', 'cash', None, -400], ['P', 'ic_receivable', 'S1', 400], ['S2', 'ic_payable', 'S2', 400], ['S1', 'ic_sales', 'P', 400], ['S1', 'cash', None, 400]], {'consolidated': {'ic_payable': 400, 'ic_receivable': 800, 'ic_sales': 400, 'sales': -250}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S1', 'P', 'ic_sales', 400], ['S2', 'S1', 'ic_receivable', 400]]}], ['control 1', [['S2', 'ic_payable', 'P', 250], ['S2', 'ic_loan', 'S2', 1000], ['P', 'ic_loan', 'P', 250], ['S1', 'ic_receivable', 'S2', 400], ['P', 'ic_receivable', 'S1', 250], ['S2', 'ic_sales', 'P', 1000]], {'consolidated': {'ic_loan': 1250, 'ic_payable': 250, 'ic_receivable': 650, 'ic_sales': 1000}, 'mismatches': [['P', 'S1', 'ic_receivable', 250], ['P', 'S2', 'ic_receivable', -250], ['S1', 'S2', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 1000]]}], ['control 2', [['P', 'ic_receivable', None, -1000], ['P', 'ic_payable', 'P', 100], ['S1', 'ic_sales', 'S1', 400], ['S1', 'ic_payable', 'S2', 250], ['P', 'ic_sales', 'P', 100], ['S2', 'ic_receivable', 'S1', 400]], {'consolidated': {'ic_payable': 100, 'ic_receivable': -850, 'ic_sales': 500}, 'mismatches': [['S2', 'S1', 'ic_receivable', 150]]}], ['control 3', [['S1', 'ic_payable', 'S1', 100], ['S2', 'ic_payable', 'S1', 100], ['S1', 'ic_sales', 'S2', 1000], ['P', 'ic_receivable', 'P', 250], ['S2', 'ic_purchases', 'S2', 100], ['S1', 'ic_payable', 'S2', 250], ['S1', 'ic_purchases', 'P', 100]], {'consolidated': {'ic_payable': 450, 'ic_purchases': 200, 'ic_receivable': 250, 'ic_sales': 1000}, 'mismatches': [['P', 'S1', 'ic_sales', -100], ['S1', 'S2', 'ic_receivable', -100], ['S1', 'S2', 'ic_sales', 1000], ['S2', 'S1', 'ic_receivable', -250]]}], ['control 4', [['P', 'ic_loan', 'P', 1000], ['S2', 'ic_sales', 'S2', 1000], ['S2', 'ic_payable', 'P', 400], ['S1', 'ic_loan', 'P', 1000], ['S1', 'ic_receivable', 'P', 1000], ['P', 'cash', None, 400], ['S2', 'ic_purchases', 'S1', 400], ['S1', 'ic_loan', 'S1', 100]], {'consolidated': {'cash': 400, 'ic_loan': 2100, 'ic_payable': 400, 'ic_purchases': 400, 'ic_receivable': 1000, 'ic_sales': 1000}, 'mismatches': [['P', 'S2', 'ic_receivable', -400], ['S1', 'P', 'ic_receivable', 1000], ['S1', 'S2', 'ic_sales', -400]]}], ['control 5', [['S1', 'ic_payable', 'P', 1000], ['S1', 'ic_loan', 'S1', 1000], ['P', 'ic_sales', 'S1', 400]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 1000, 'ic_sales': 400}, 'mismatches': [['P', 'S1', 'ic_receivable', -1000], ['P', 'S1', 'ic_sales', 400]]}], ['control 6', [['S2', 'ic_purchases', 'S1', 250], ['S2', 'ic_loan', 'S1', 100], ['P', 'ic_loan', 'S2', 250]], {'consolidated': {'ic_loan': 350, 'ic_purchases': 250}, 'mismatches': [['S1', 'S2', 'ic_sales', -250]]}]], [['regression: zero balance filter', [['S1', 'sales', None, -400], ['S1', 'ic_payable', 'P', 400], ['P', 'ic_loan', 'S2', 1000], ['S1', 'ic_receivable', 'S2', 1000], ['S2', 'ic_receivable', None, -1000], ['P', 'ic_sales', 'P', 400]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 400, 'ic_sales': 400, 'sales': -400}, 'mismatches': [['P', 'S1', 'ic_receivable', -400], ['S1', 'S2', 'ic_receivable', 1000]]}], ['control 1', [['S1', 'cash', None, 400], ['P', 'sales', None, -400], ['S1', 'ic_receivable', None, 1000]], {'consolidated': {'cash': 400, 'ic_receivable': 1000, 'sales': -400}, 'mismatches': []}], ['control 2', [['P', 'ic_payable', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['S1', 'sales', None, -250], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_payable', 'S2', 400], ['P', 'ic_purchases', 'P', 100]], {'consolidated': {'ic_loan': 100, 'ic_payable': 800, 'ic_purchases': 500, 'sales': -250}, 'mismatches': [['S2', 'P', 'ic_receivable', -800]]}], ['control 3', [['S2', 'ic_sales', 'P', 100], ['P', 'ic_receivable', 'P', 100], ['S1', 'ic_payable', 'S1', 100], ['S2', 'cash', None, -400], ['S2', 'ic_purchases', 'P', 1000], ['P', 'ic_sales', 'P', 100], ['S1', 'ic_loan', 'S1', 1000], ['S1', 'ic_purchases', 'P', 100]], {'consolidated': {'cash': -400, 'ic_loan': 1000, 'ic_payable': 100, 'ic_purchases': 1100, 'ic_receivable': 100, 'ic_sales': 200}, 'mismatches': [['P', 'S1', 'ic_sales', -100], ['P', 'S2', 'ic_sales', -1000], ['S2', 'P', 'ic_sales', 100]]}], ['control 4', [['S2', 'ic_loan', 'S1', 1000], ['S2', 'ic_payable', 'S1', 100], ['S2', 'ic_loan', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['S1', 'sales', None, 400], ['P', 'sales', None, 250], ['P', 'ic_loan', 'S1', 100]], {'consolidated': {'ic_loan': 1500, 'ic_payable': 100, 'ic_purchases': 400, 'sales': 650}, 'mismatches': [['S1', 'S2', 'ic_receivable', -100]]}], ['control 5', [['S2', 'ic_payable', 'P', 1000], ['P', 'ic_sales', 'P', 400], ['S1', 'ic_purchases', 'S1', 100], ['P', 'ic_loan', 'S2', 400], ['P', 'ic_purchases', 'S2', 100], ['S2', 'ic_purchases', 'S2', 100], ['P', 'cash', None, -100]], {'consolidated': {'cash': -100, 'ic_loan': 400, 'ic_payable': 1000, 'ic_purchases': 300, 'ic_sales': 400}, 'mismatches': [['P', 'S2', 'ic_receivable', -1000], ['S2', 'P', 'ic_sales', -100]]}], ['control 6', [['S2', 'ic_receivable', None, -400], ['S2', 'ic_payable', 'S1', 400], ['S2', 'ic_receivable', None, 100], ['S1', 'ic_payable', 'S2', 1000], ['S2', 'ic_sales', 'S2', 250], ['S2', 'ic_payable', 'S2', 100], ['S2', 'cash', None, 400], ['S2', 'ic_sales', 'S2', 250]], {'consolidated': {'cash': 400, 'ic_payable': 1500, 'ic_receivable': -300, 'ic_sales': 500}, 'mismatches': [['S1', 'S2', 'ic_receivable', -400], ['S2', 'S1', 'ic_receivable', -1000]]}]], [['regression: zero balance filter', [['P', 'ic_payable', 'S1', 100], ['S1', 'cash', None, 400], ['S1', 'ic_receivable', None, 1000], ['P', 'ic_receivable', 'P', 400], ['P', 'ic_loan', 'S2', 100], ['S1', 'cash', None, -400], ['S2', 'ic_loan', 'S2', 1000], ['S2', 'ic_receivable', None, 400]], {'consolidated': {'ic_loan': 1100, 'ic_payable': 100, 'ic_receivable': 1800}, 'mismatches': [['S1', 'P', 'ic_receivable', -100]]}], ['regression: zero balance filter, partial-repair probe', [['S2', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'P', 250], ['S2', 'ic_payable', 'S1', 400], ['S1', 'ic_purchases', 'P', 1000], ['P', 'ic_loan', 'S1', 100], ['S2', 'ic_receivable', 'P', 400], ['S2', 'cash', None, -1000]], {'consolidated': {'cash': -1000, 'ic_loan': 100, 'ic_payable': 400, 'ic_purchases': 1000, 'ic_receivable': 400, 'ic_sales': 650}, 'mismatches': [['P', 'S1', 'ic_sales', -1000], ['S1', 'S2', 'ic_receivable', -400], ['S2', 'P', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 650]]}], ['control 1', [['P', 'ic_purchases', 'S1', 100], ['S1', 'ic_sales', 'P', 400], ['S2', 'ic_loan', 'S2', 1000], ['P', 'cash', None, 100]], {'consolidated': {'cash': 100, 'ic_loan': 1000, 'ic_sales': 300}, 'mismatches': [['S1', 'P', 'ic_sales', 300]]}], ['control 2', [['S1', 'ic_purchases', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['P', 'ic_payable', 'S1', 400], ['S1', 'ic_sales', 'S1', 1000]], {'consolidated': {'ic_payable': 400, 'ic_purchases': 800, 'ic_sales': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', -400], ['S2', 'S1', 'ic_sales', -400]]}], ['control 3', [['S1', 'ic_sales', 'P', 100], ['S2', 'sales', None, 400], ['S1', 'ic_loan', 'S1', 100], ['P', 'ic_sales', 'P', 100], ['S2', 'cash', None, 100], ['S2', 'ic_purchases', 'S1', 100], ['S2', 'ic_purchases', 'S1', 1000]], {'consolidated': {'cash': 100, 'ic_loan': 100, 'ic_purchases': 1100, 'ic_sales': 200, 'sales': 400}, 'mismatches': [['S1', 'P', 'ic_sales', 100], ['S1', 'S2', 'ic_sales', -1100]]}], ['control 4', [['S2', 'ic_loan', 'P', 400], ['P', 'cash', None, 1000], ['P', 'ic_purchases', 'S1', 400], ['S2', 'ic_loan', 'S1', 1000], ['S1', 'cash', None, -100], ['S1', 'ic_purchases', 'S1', 400]], {'consolidated': {'cash': 900, 'ic_loan': 1400, 'ic_purchases': 800}, 'mismatches': [['S1', 'P', 'ic_sales', -400]]}], ['control 5', [['P', 'ic_purchases', 'S2', 250], ['S1', 'ic_sales', 'P', 1000], ['S2', 'ic_payable', 'S1', 250], ['S2', 'ic_loan', 'S1', 1000]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 250, 'ic_purchases': 250, 'ic_sales': 1000}, 'mismatches': [['S1', 'P', 'ic_sales', 1000], ['S1', 'S2', 'ic_receivable', -250], ['S2', 'P', 'ic_sales', -250]]}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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 eliminated accounts appear in the consolidation with zero totals.

x = [[entity, account, counterparty or None, amount]]. Rows with no counterparty, the entity itself as counterparty, or an account outside the pairs (ic_receivable/ic_payable, ic_sales/ic_purchases) are consolidated as-is. For each (entity, counterparty, pair) the left balance of entity toward counterparty is matched against the right balance of counterparty toward entity; min(left, right) is eliminated from both, residuals are consolidated and a mismatch [entity, counterparty, left account, left - right] is reported when they differ. Return {'consolidated': nonzero account totals, 'mismatches': in sorted key order}.

split: open-access


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

N = 1
observations = []
def solve(x):
    cut = x['as_of']
    control = sum(a for d, a in x['control'] if d < cut)
    per = {}
    for cust, rows in x['sub'].items():
        per[cust] = sum(a for d, a in rows if d <= cut)
    sub_total = sum(per.values())
    credit = sorted(c for c, v in per.items() if v < 0)
    return {'control': control, 'subledger': sub_total, 'difference': control - sub_total, 'credit_balances': credit, 'open_customers': sum(1 for v in per.values() if v != 0)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: control cutoff', {'control': [[21, 400], [5, 250], [19, 250], [20, 400], [19, -80]], 'sub': {'bolt': [[5, 250]]}, 'as_of': 20}, {'control': 820, 'subledger': 250, 'difference': 570, 'credit_balances': [], 'open_customers': 1}], ['control 1', {'control': [[5, 100], [19, -80], [21, -80]], 'sub': {'bolt': [[5, -80], [1, 250], [5, -80]], 'acme': [[21, 100]], 'dyna': [[20, 400], [1, 250], [20, 100]], 'crux': [[20, 400], [5, 100]]}, 'as_of': 20}, {'control': 20, 'subledger': 1340, 'difference': -1320, 'credit_balances': [], 'open_customers': 3}], ['control 2', {'control': [[5, 100], [21, 250], [19, 400], [5, 100]], 'sub': {'dyna': [[21, -80], [21, 100], [20, 400]], 'acme': [[5, 100], [20, -80], [21, -300]], 'bolt': [[5, -80], [21, -300]]}, 'as_of': 20}, {'control': 600, 'subledger': 340, 'difference': 260, 'credit_balances': ['bolt'], 'open_customers': 3}], ['control 3', {'control': [[5, 400], [1, 250], [9, -80], [5, -80], [5, 250]], 'sub': {'acme': [[5, 250], [11, 100]], 'crux': [[5, 400]], 'dyna': [[5, 250]]}, 'as_of': 10}, {'control': 740, 'subledger': 900, 'difference': -160, 'credit_balances': [], 'open_customers': 3}], ['control 4', {'control': [[32, 100], [30, -80], [5, 100], [5, 100]], 'sub': {'crux': [[5, 100], [1, 250], [1, -300]], 'bolt': [[31, 250], [5, -300]]}, 'as_of': 31}, {'control': 120, 'subledger': 0, 'difference': 120, 'credit_balances': ['bolt'], 'open_customers': 2}], ['control 5', {'control': [[11, 250], [1, -80], [11, 400], [1, -80]], 'sub': {'acme': [[1, 250], [1, -80], [11, 250]], 'bolt': [[11, 100]], 'dyna': [[11, -80]]}, 'as_of': 10}, {'control': -160, 'subledger': 170, 'difference': -330, 'credit_balances': [], 'open_customers': 1}], ['control 6', {'control': [[5, 400], [21, 400], [19, -80], [19, 400], [19, 250]], 'sub': {'bolt': [[5, 100]], 'acme': [[1, 250]]}, 'as_of': 20}, {'control': 970, 'subledger': 350, 'difference': 620, 'credit_balances': [], 'open_customers': 2}]], [['regression: control cutoff', {'control': [[20, 250], [19, 100], [5, 400]], 'sub': {'bolt': [[20, 250]], 'acme': [[1, -300], [1, 100], [1, 250]]}, 'as_of': 20}, {'control': 750, 'subledger': 300, 'difference': 450, 'credit_balances': [], 'open_customers': 2}], ['control 1', {'control': [[1, 400], [11, 250]], 'sub': {'dyna': [[1, 250]], 'crux': [[1, 250], [1, 250], [1, 100]], 'acme': [[10, 250], [1, 250], [11, 250]], 'bolt': [[11, 250], [5, 400]]}, 'as_of': 10}, {'control': 400, 'subledger': 1750, 'difference': -1350, 'credit_balances': [], 'open_customers': 4}], ['control 2', {'control': [[5, -80], [19, 400], [1, -80]], 'sub': {'crux': [[21, -300], [21, 400]], 'bolt': [[1, 250], [21, 250]], 'dyna': [[21, -300], [20, 250], [5, 250]]}, 'as_of': 20}, {'control': 240, 'subledger': 750, 'difference': -510, 'credit_balances': [], 'open_customers': 2}], ['control 3', {'control': [[1, 250], [5, 100], [1, 400]], 'sub': {'dyna': [[1, -300]], 'bolt': [[1, -300]], 'crux': [[1, 250]], 'acme': [[11, 100]]}, 'as_of': 10}, {'control': 750, 'subledger': -350, 'difference': 1100, 'credit_balances': ['bolt', 'dyna'], 'open_customers': 3}], ['control 4', {'control': [[1, 400], [30, 100]], 'sub': {'acme': [[31, 100], [32, 250], [31, -80]], 'crux': [[1, 250], [32, -300], [5, -300]]}, 'as_of': 31}, {'control': 500, 'subledger': -30, 'difference': 530, 'credit_balances': ['crux'], 'open_customers': 2}], ['control 5', {'control': [[5, 400], [32, 400]], 'sub': {'acme': [[32, 100]], 'crux': [[1, 100], [5, 400]], 'dyna': [[31, -300], [31, 250]]}, 'as_of': 31}, {'control': 400, 'subledger': 450, 'difference': -50, 'credit_balances': ['dyna'], 'open_customers': 2}], ['control 6', {'control': [[19, 400], [5, 400]], 'sub': {'bolt': [[20, -80], [5, 100]], 'crux': [[21, -80], [5, 100]]}, 'as_of': 20}, {'control': 800, 'subledger': 120, 'difference': 680, 'credit_balances': [], 'open_customers': 2}]], [['regression: control cutoff', {'control': [[1, -80], [1, 100], [32, 400], [31, 250]], 'sub': {'bolt': [[1, 250]]}, 'as_of': 31}, {'control': 270, 'subledger': 250, 'difference': 20, 'credit_balances': [], 'open_customers': 1}], ['control 1', {'control': [[9, 250], [11, 100], [11, -80], [11, 250]], 'sub': {'dyna': [[1, 250], [11, 400], [10, -80]], 'acme': [[11, -300], [1, 400], [1, 400]], 'bolt': [[1, -300]]}, 'as_of': 10}, {'control': 250, 'subledger': 670, 'difference': -420, 'credit_balances': ['bolt'], 'open_customers': 3}], ['control 2', {'control': [[11, 100], [5, -80], [9, 400]], 'sub': {'acme': [[11, 100], [11, 250], [1, 250]], 'dyna': [[5, 100]], 'bolt': [[5, -300], [5, -300]], 'crux': [[5, 250], [11, -300], [1, 100]]}, 'as_of': 10}, {'control': 320, 'subledger': 100, 'difference': 220, 'credit_balances': ['bolt'], 'open_customers': 4}], ['control 3', {'control': [[1, -80], [5, 250], [1, 400], [21, -80], [5, 250], [5, 100]], 'sub': {'crux': [[20, -80]], 'acme': [[1, 100]], 'bolt': [[5, 400], [1, 400], [21, 250]]}, 'as_of': 20}, {'control': 920, 'subledger': 820, 'difference': 100, 'credit_balances': ['crux'], 'open_customers': 3}], ['control 4', {'control': [[5, 400], [1, 250], [5, -80], [5, 250], [11, -80], [1, 400]], 'sub': {'dyna': [[11, 100]], 'acme': [[5, 400], [1, 100]], 'crux': [[5, 400], [5, -300]]}, 'as_of': 10}, {'control': 1220, 'subledger': 600, 'difference': 620, 'credit_balances': [], 'open_customers': 2}], ['control 5', {'control': [[11, 250], [5, 250]], 'sub': {'acme': [[1, -80], [11, 100]], 'crux': [[5, 250]], 'dyna': [[11, -300], [5, 250], [1, 250]]}, 'as_of': 10}, {'control': 250, 'subledger': 670, 'difference': -420, 'credit_balances': ['acme'], 'open_customers': 3}], ['control 6', {'control': [[5, 100], [32, 250], [5, 100]], 'sub': {'dyna': [[32, 400], [5, -80], [1, 100]], 'acme': [[32, -80], [1, -80]], 'bolt': [[1, 400]]}, 'as_of': 31}, {'control': 200, 'subledger': 340, 'difference': -140, 'credit_balances': ['acme'], 'open_customers': 3}]], [['regression: control cutoff', {'control': [[20, -80], [21, 100]], 'sub': {'dyna': [[1, -80], [20, 250]], 'bolt': [[1, -300], [20, 400], [21, -300]]}, 'as_of': 20}, {'control': -80, 'subledger': 270, 'difference': -350, 'credit_balances': [], 'open_customers': 2}], ['control 1', {'control': [[5, 400], [30, 250]], 'sub': {'dyna': [[5, 250], [31, 400]], 'bolt': [[32, 400], [32, -80], [5, 250]], 'crux': [[5, -300], [1, 250], [31, -300]]}, 'as_of': 31}, {'control': 650, 'subledger': 550, 'difference': 100, 'credit_balances': ['crux'], 'open_customers': 3}], ['control 2', {'control': [[1, 100], [11, 100], [1, 100], [9, 400], [9, 100], [9, 100]], 'sub': {'dyna': [[1, 100], [5, 100]]}, 'as_of': 10}, {'control': 800, 'subledger': 200, 'difference': 600, 'credit_balances': [], 'open_customers': 1}], ['control 3', {'control': [[21, -80], [19, 250], [5, 100], [19, -80], [21, 400]], 'sub': {'acme': [[20, 250], [5, 100], [21, 400]], 'bolt': [[1, 400], [20, -80]], 'dyna': [[5, 100]]}, 'as_of': 20}, {'control': 270, 'subledger': 770, 'difference': -500, 'credit_balances': [], 'open_customers': 3}], ['control 4', {'control': [[21, 250], [5, 100], [21, 250]], 'sub': {'bolt': [[21, -300], [5, 100]]}, 'as_of': 20}, {'control': 100, 'subledger': 100, 'difference': 0, 'credit_balances': [], 'open_customers': 1}], ['control 5', {'control': [[5, 250], [11, 250], [1, -80]], 'sub': {'acme': [[10, 400], [1, 400], [5, -80]], 'dyna': [[1, -300]], 'bolt': [[1, -300], [5, 400]], 'crux': [[11, 400], [1, 250], [11, 100]]}, 'as_of': 10}, {'control': 170, 'subledger': 770, 'difference': -600, 'credit_balances': ['dyna'], 'open_customers': 4}], ['control 6', {'control': [[11, 400], [11, -80]], 'sub': {'dyna': [[5, 250]], 'crux': [[10, -300]]}, 'as_of': 10}, {'control': 0, 'subledger': -50, 'difference': 50, 'credit_balances': ['crux'], 'open_customers': 2}]], [['regression: control cutoff', {'control': [[10, -80], [9, 400], [1, 250]], 'sub': {'crux': [[5, 250], [1, 400], [5, 250]], 'acme': [[10, -80]]}, 'as_of': 10}, {'control': 570, 'subledger': 820, 'difference': -250, 'credit_balances': ['acme'], 'open_customers': 2}], ['control 1', {'control': [[5, 100], [5, 250], [19, -80], [21, 250], [21, 250]], 'sub': {'crux': [[20, -80], [20, -80]], 'acme': [[21, 100]]}, 'as_of': 20}, {'control': 270, 'subledger': -160, 'difference': 430, 'credit_balances': ['crux'], 'open_customers': 1}], ['control 2', {'control': [[5, 400], [1, 250], [9, 400], [9, 400]], 'sub': {'acme': [[1, -300], [11, -80]], 'bolt': [[1, 250], [10, 250], [5, -300]], 'dyna': [[1, -300], [10, -300], [1, 100]]}, 'as_of': 10}, {'control': 1450, 'subledger': -600, 'difference': 2050, 'credit_balances': ['acme', 'dyna'], 'open_customers': 3}], ['control 3', {'control': [[9, -80], [11, 400]], 'sub': {'bolt': [[5, 100], [5, 250]], 'acme': [[11, 100], [11, -80], [10, 250]]}, 'as_of': 10}, {'control': -80, 'subledger': 600, 'difference': -680, 'credit_balances': [], 'open_customers': 2}], ['control 4', {'control': [[1, 100], [1, 250], [5, 250], [1, 100], [1, 400], [5, -80]], 'sub': {'bolt': [[10, -80], [10, 400]], 'crux': [[1, 250], [10, 250]]}, 'as_of': 10}, {'control': 1020, 'subledger': 820, 'difference': 200, 'credit_balances': [], 'open_customers': 2}], ['control 5', {'control': [[1, -80], [11, -80], [9, -80]], 'sub': {'crux': [[11, -80]], 'acme': [[1, -80]], 'dyna': [[5, 100], [1, -80]], 'bolt': [[10, 250]]}, 'as_of': 10}, {'control': -160, 'subledger': 190, 'difference': -350, 'credit_balances': ['acme'], 'open_customers': 3}], ['control 6', {'control': [[5, 250], [19, 400], [19, 100], [19, 100]], 'sub': {'crux': [[5, 400], [1, -300], [1, 400]], 'acme': [[21, 250], [5, -300], [20, -80]]}, 'as_of': 20}, {'control': 850, 'subledger': 120, 'difference': 730, 'credit_balances': ['acme'], 'open_customers': 2}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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):
    cut = x['as_of']
    control = sum(a for d, a in x['control'] if d < cut)
    per = {}
    for cust, rows in x['sub'].items():
        per[cust] = sum(a for d, a in rows if d < cut)
    sub_total = sum(per.values())
    credit = sorted(c for c, v in per.items() if v < 0)
    return {'control': control, 'subledger': sub_total, 'difference': control - sub_total, 'credit_balances': credit, 'open_customers': sum(1 for v in per.values() if v != 0)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: control cutoff', {'control': [[21, 400], [5, 250], [19, 250], [20, 400], [19, -80]], 'sub': {'bolt': [[5, 250]]}, 'as_of': 20}, {'control': 820, 'subledger': 250, 'difference': 570, 'credit_balances': [], 'open_customers': 1}], ['control 1', {'control': [[5, 100], [19, -80], [21, -80]], 'sub': {'bolt': [[5, -80], [1, 250], [5, -80]], 'acme': [[21, 100]], 'dyna': [[20, 400], [1, 250], [20, 100]], 'crux': [[20, 400], [5, 100]]}, 'as_of': 20}, {'control': 20, 'subledger': 1340, 'difference': -1320, 'credit_balances': [], 'open_customers': 3}], ['control 2', {'control': [[5, 100], [21, 250], [19, 400], [5, 100]], 'sub': {'dyna': [[21, -80], [21, 100], [20, 400]], 'acme': [[5, 100], [20, -80], [21, -300]], 'bolt': [[5, -80], [21, -300]]}, 'as_of': 20}, {'control': 600, 'subledger': 340, 'difference': 260, 'credit_balances': ['bolt'], 'open_customers': 3}], ['control 3', {'control': [[5, 400], [1, 250], [9, -80], [5, -80], [5, 250]], 'sub': {'acme': [[5, 250], [11, 100]], 'crux': [[5, 400]], 'dyna': [[5, 250]]}, 'as_of': 10}, {'control': 740, 'subledger': 900, 'difference': -160, 'credit_balances': [], 'open_customers': 3}], ['control 4', {'control': [[32, 100], [30, -80], [5, 100], [5, 100]], 'sub': {'crux': [[5, 100], [1, 250], [1, -300]], 'bolt': [[31, 250], [5, -300]]}, 'as_of': 31}, {'control': 120, 'subledger': 0, 'difference': 120, 'credit_balances': ['bolt'], 'open_customers': 2}], ['control 5', {'control': [[11, 250], [1, -80], [11, 400], [1, -80]], 'sub': {'acme': [[1, 250], [1, -80], [11, 250]], 'bolt': [[11, 100]], 'dyna': [[11, -80]]}, 'as_of': 10}, {'control': -160, 'subledger': 170, 'difference': -330, 'credit_balances': [], 'open_customers': 1}], ['control 6', {'control': [[5, 400], [21, 400], [19, -80], [19, 400], [19, 250]], 'sub': {'bolt': [[5, 100]], 'acme': [[1, 250]]}, 'as_of': 20}, {'control': 970, 'subledger': 350, 'difference': 620, 'credit_balances': [], 'open_customers': 2}]], [['regression: control cutoff', {'control': [[20, 250], [19, 100], [5, 400]], 'sub': {'bolt': [[20, 250]], 'acme': [[1, -300], [1, 100], [1, 250]]}, 'as_of': 20}, {'control': 750, 'subledger': 300, 'difference': 450, 'credit_balances': [], 'open_customers': 2}], ['control 1', {'control': [[1, 400], [11, 250]], 'sub': {'dyna': [[1, 250]], 'crux': [[1, 250], [1, 250], [1, 100]], 'acme': [[10, 250], [1, 250], [11, 250]], 'bolt': [[11, 250], [5, 400]]}, 'as_of': 10}, {'control': 400, 'subledger': 1750, 'difference': -1350, 'credit_balances': [], 'open_customers': 4}], ['control 2', {'control': [[5, -80], [19, 400], [1, -80]], 'sub': {'crux': [[21, -300], [21, 400]], 'bolt': [[1, 250], [21, 250]], 'dyna': [[21, -300], [20, 250], [5, 250]]}, 'as_of': 20}, {'control': 240, 'subledger': 750, 'difference': -510, 'credit_balances': [], 'open_customers': 2}], ['control 3', {'control': [[1, 250], [5, 100], [1, 400]], 'sub': {'dyna': [[1, -300]], 'bolt': [[1, -300]], 'crux': [[1, 250]], 'acme': [[11, 100]]}, 'as_of': 10}, {'control': 750, 'subledger': -350, 'difference': 1100, 'credit_balances': ['bolt', 'dyna'], 'open_customers': 3}], ['control 4', {'control': [[1, 400], [30, 100]], 'sub': {'acme': [[31, 100], [32, 250], [31, -80]], 'crux': [[1, 250], [32, -300], [5, -300]]}, 'as_of': 31}, {'control': 500, 'subledger': -30, 'difference': 530, 'credit_balances': ['crux'], 'open_customers': 2}], ['control 5', {'control': [[5, 400], [32, 400]], 'sub': {'acme': [[32, 100]], 'crux': [[1, 100], [5, 400]], 'dyna': [[31, -300], [31, 250]]}, 'as_of': 31}, {'control': 400, 'subledger': 450, 'difference': -50, 'credit_balances': ['dyna'], 'open_customers': 2}], ['control 6', {'control': [[19, 400], [5, 400]], 'sub': {'bolt': [[20, -80], [5, 100]], 'crux': [[21, -80], [5, 100]]}, 'as_of': 20}, {'control': 800, 'subledger': 120, 'difference': 680, 'credit_balances': [], 'open_customers': 2}]], [['regression: control cutoff', {'control': [[1, -80], [1, 100], [32, 400], [31, 250]], 'sub': {'bolt': [[1, 250]]}, 'as_of': 31}, {'control': 270, 'subledger': 250, 'difference': 20, 'credit_balances': [], 'open_customers': 1}], ['control 1', {'control': [[9, 250], [11, 100], [11, -80], [11, 250]], 'sub': {'dyna': [[1, 250], [11, 400], [10, -80]], 'acme': [[11, -300], [1, 400], [1, 400]], 'bolt': [[1, -300]]}, 'as_of': 10}, {'control': 250, 'subledger': 670, 'difference': -420, 'credit_balances': ['bolt'], 'open_customers': 3}], ['control 2', {'control': [[11, 100], [5, -80], [9, 400]], 'sub': {'acme': [[11, 100], [11, 250], [1, 250]], 'dyna': [[5, 100]], 'bolt': [[5, -300], [5, -300]], 'crux': [[5, 250], [11, -300], [1, 100]]}, 'as_of': 10}, {'control': 320, 'subledger': 100, 'difference': 220, 'credit_balances': ['bolt'], 'open_customers': 4}], ['control 3', {'control': [[1, -80], [5, 250], [1, 400], [21, -80], [5, 250], [5, 100]], 'sub': {'crux': [[20, -80]], 'acme': [[1, 100]], 'bolt': [[5, 400], [1, 400], [21, 250]]}, 'as_of': 20}, {'control': 920, 'subledger': 820, 'difference': 100, 'credit_balances': ['crux'], 'open_customers': 3}], ['control 4', {'control': [[5, 400], [1, 250], [5, -80], [5, 250], [11, -80], [1, 400]], 'sub': {'dyna': [[11, 100]], 'acme': [[5, 400], [1, 100]], 'crux': [[5, 400], [5, -300]]}, 'as_of': 10}, {'control': 1220, 'subledger': 600, 'difference': 620, 'credit_balances': [], 'open_customers': 2}], ['control 5', {'control': [[11, 250], [5, 250]], 'sub': {'acme': [[1, -80], [11, 100]], 'crux': [[5, 250]], 'dyna': [[11, -300], [5, 250], [1, 250]]}, 'as_of': 10}, {'control': 250, 'subledger': 670, 'difference': -420, 'credit_balances': ['acme'], 'open_customers': 3}], ['control 6', {'control': [[5, 100], [32, 250], [5, 100]], 'sub': {'dyna': [[32, 400], [5, -80], [1, 100]], 'acme': [[32, -80], [1, -80]], 'bolt': [[1, 400]]}, 'as_of': 31}, {'control': 200, 'subledger': 340, 'difference': -140, 'credit_balances': ['acme'], 'open_customers': 3}]], [['regression: control cutoff', {'control': [[20, -80], [21, 100]], 'sub': {'dyna': [[1, -80], [20, 250]], 'bolt': [[1, -300], [20, 400], [21, -300]]}, 'as_of': 20}, {'control': -80, 'subledger': 270, 'difference': -350, 'credit_balances': [], 'open_customers': 2}], ['control 1', {'control': [[5, 400], [30, 250]], 'sub': {'dyna': [[5, 250], [31, 400]], 'bolt': [[32, 400], [32, -80], [5, 250]], 'crux': [[5, -300], [1, 250], [31, -300]]}, 'as_of': 31}, {'control': 650, 'subledger': 550, 'difference': 100, 'credit_balances': ['crux'], 'open_customers': 3}], ['control 2', {'control': [[1, 100], [11, 100], [1, 100], [9, 400], [9, 100], [9, 100]], 'sub': {'dyna': [[1, 100], [5, 100]]}, 'as_of': 10}, {'control': 800, 'subledger': 200, 'difference': 600, 'credit_balances': [], 'open_customers': 1}], ['control 3', {'control': [[21, -80], [19, 250], [5, 100], [19, -80], [21, 400]], 'sub': {'acme': [[20, 250], [5, 100], [21, 400]], 'bolt': [[1, 400], [20, -80]], 'dyna': [[5, 100]]}, 'as_of': 20}, {'control': 270, 'subledger': 770, 'difference': -500, 'credit_balances': [], 'open_customers': 3}], ['control 4', {'control': [[21, 250], [5, 100], [21, 250]], 'sub': {'bolt': [[21, -300], [5, 100]]}, 'as_of': 20}, {'control': 100, 'subledger': 100, 'difference': 0, 'credit_balances': [], 'open_customers': 1}], ['control 5', {'control': [[5, 250], [11, 250], [1, -80]], 'sub': {'acme': [[10, 400], [1, 400], [5, -80]], 'dyna': [[1, -300]], 'bolt': [[1, -300], [5, 400]], 'crux': [[11, 400], [1, 250], [11, 100]]}, 'as_of': 10}, {'control': 170, 'subledger': 770, 'difference': -600, 'credit_balances': ['dyna'], 'open_customers': 4}], ['control 6', {'control': [[11, 400], [11, -80]], 'sub': {'dyna': [[5, 250]], 'crux': [[10, -300]]}, 'as_of': 10}, {'control': 0, 'subledger': -50, 'difference': 50, 'credit_balances': ['crux'], 'open_customers': 2}]], [['regression: control cutoff', {'control': [[10, -80], [9, 400], [1, 250]], 'sub': {'crux': [[5, 250], [1, 400], [5, 250]], 'acme': [[10, -80]]}, 'as_of': 10}, {'control': 570, 'subledger': 820, 'difference': -250, 'credit_balances': ['acme'], 'open_customers': 2}], ['control 1', {'control': [[5, 100], [5, 250], [19, -80], [21, 250], [21, 250]], 'sub': {'crux': [[20, -80], [20, -80]], 'acme': [[21, 100]]}, 'as_of': 20}, {'control': 270, 'subledger': -160, 'difference': 430, 'credit_balances': ['crux'], 'open_customers': 1}], ['control 2', {'control': [[5, 400], [1, 250], [9, 400], [9, 400]], 'sub': {'acme': [[1, -300], [11, -80]], 'bolt': [[1, 250], [10, 250], [5, -300]], 'dyna': [[1, -300], [10, -300], [1, 100]]}, 'as_of': 10}, {'control': 1450, 'subledger': -600, 'difference': 2050, 'credit_balances': ['acme', 'dyna'], 'open_customers': 3}], ['control 3', {'control': [[9, -80], [11, 400]], 'sub': {'bolt': [[5, 100], [5, 250]], 'acme': [[11, 100], [11, -80], [10, 250]]}, 'as_of': 10}, {'control': -80, 'subledger': 600, 'difference': -680, 'credit_balances': [], 'open_customers': 2}], ['control 4', {'control': [[1, 100], [1, 250], [5, 250], [1, 100], [1, 400], [5, -80]], 'sub': {'bolt': [[10, -80], [10, 400]], 'crux': [[1, 250], [10, 250]]}, 'as_of': 10}, {'control': 1020, 'subledger': 820, 'difference': 200, 'credit_balances': [], 'open_customers': 2}], ['control 5', {'control': [[1, -80], [11, -80], [9, -80]], 'sub': {'crux': [[11, -80]], 'acme': [[1, -80]], 'dyna': [[5, 100], [1, -80]], 'bolt': [[10, 250]]}, 'as_of': 10}, {'control': -160, 'subledger': 190, 'difference': -350, 'credit_balances': ['acme'], 'open_customers': 3}], ['control 6', {'control': [[5, 250], [19, 400], [19, 100], [19, 100]], 'sub': {'crux': [[5, 400], [1, -300], [1, 400]], 'acme': [[21, 250], [5, -300], [20, -80]]}, 'as_of': 20}, {'control': 850, 'subledger': 120, 'difference': 730, 'credit_balances': ['acme'], 'open_customers': 2}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The control account omits postings dated on the reconciliation date.

x = {'control': [[day, amount]], 'sub': {customer: [[day, amount]]}, 'as_of': day}. Only rows dated on or before as_of count. The subledger total nets every customer balance, including credit (negative) balances. Return {'control', 'subledger', 'difference': control - subledger, 'credit_balances': customers with negative balance sorted by name, 'open_customers': count of nonzero balances}.

split: open-access


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

N = 1
observations = []
def solve(x):
    cut = x['as_of']
    control = sum(a for d, a in x['control'] if d <= cut)
    per = {}
    for cust, rows in x['sub'].items():
        per[cust] = sum(a for d, a in rows)
    sub_total = sum(per.values())
    credit = sorted(c for c, v in per.items() if v < 0)
    return {'control': control, 'subledger': sub_total, 'difference': control - sub_total, 'credit_balances': credit, 'open_customers': sum(1 for v in per.values() if v != 0)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: subledger cutoff', {'control': [[5, 100], [19, -80], [21, -80]], 'sub': {'bolt': [[5, -80], [1, 250], [5, -80]], 'acme': [[21, 100]], 'dyna': [[20, 400], [1, 250], [20, 100]], 'crux': [[20, 400], [5, 100]]}, 'as_of': 20}, {'control': 20, 'subledger': 1340, 'difference': -1320, 'credit_balances': [], 'open_customers': 3}], ['regression: subledger cutoff, partial-repair probe', {'control': [[5, 100], [21, 250], [19, 400], [5, 100]], 'sub': {'dyna': [[21, -80], [21, 100], [20, 400]], 'acme': [[5, 100], [20, -80], [21, -300]], 'bolt': [[5, -80], [21, -300]]}, 'as_of': 20}, {'control': 600, 'subledger': 340, 'difference': 260, 'credit_balances': ['bolt'], 'open_customers': 3}], ['control 1', {'control': [[21, 400], [5, 250], [19, 250], [20, 400], [19, -80]], 'sub': {'bolt': [[5, 250]]}, 'as_of': 20}, {'control': 820, 'subledger': 250, 'difference': 570, 'credit_balances': [], 'open_customers': 1}], ['control 2', {'control': [[20, 250], [21, 100], [1, 400], [5, 100]], 'sub': {'acme': [[20, -300]], 'dyna': [[5, 250], [1, -80]]}, 'as_of': 20}, {'control': 750, 'subledger': -130, 'difference': 880, 'credit_balances': ['acme'], 'open_customers': 2}], ['control 3', {'control': [[32, 100], [30, -80], [5, 100], [5, 100]], 'sub': {'crux': [[5, 100], [1, 250], [1, -300]], 'bolt': [[31, 250], [5, -300]]}, 'as_of': 31}, {'control': 120, 'subledger': 0, 'difference': 120, 'credit_balances': ['bolt'], 'open_customers': 2}], ['control 4', {'control': [[32, 400], [32, 250], [31, -80]], 'sub': {'dyna': [[31, 400], [1, -300]], 'bolt': [[5, 250], [5, 250], [1, -300]], 'crux': [[31, -300], [1, -80], [31, -300]], 'acme': [[31, 250]]}, 'as_of': 31}, {'control': -80, 'subledger': -130, 'difference': 50, 'credit_balances': ['crux'], 'open_customers': 4}], ['control 5', {'control': [[5, 400], [21, 400], [19, -80], [19, 400], [19, 250]], 'sub': {'bolt': [[5, 100]], 'acme': [[1, 250]]}, 'as_of': 20}, {'control': 970, 'subledger': 350, 'difference': 620, 'credit_balances': [], 'open_customers': 2}]], [['regression: subledger cutoff', {'control': [[1, 400], [11, 250]], 'sub': {'dyna': [[1, 250]], 'crux': [[1, 250], [1, 250], [1, 100]], 'acme': [[10, 250], [1, 250], [11, 250]], 'bolt': [[11, 250], [5, 400]]}, 'as_of': 10}, {'control': 400, 'subledger': 1750, 'difference': -1350, 'credit_balances': [], 'open_customers': 4}], ['regression: subledger cutoff, partial-repair probe', {'control': [[5, 400], [20, -80], [19, 250]], 'sub': {'acme': [[21, 250]], 'bolt': [[20, 250], [1, -80], [1, -80]], 'dyna': [[21, -80], [20, 100]], 'crux': [[5, 400], [21, 400], [20, -300]]}, 'as_of': 20}, {'control': 570, 'subledger': 290, 'difference': 280, 'credit_balances': [], 'open_customers': 3}], ['control 1', {'control': [[20, 250], [19, 100], [5, 400]], 'sub': {'bolt': [[20, 250]], 'acme': [[1, -300], [1, 100], [1, 250]]}, 'as_of': 20}, {'control': 750, 'subledger': 300, 'difference': 450, 'credit_balances': [], 'open_customers': 2}], ['control 2', {'control': [[32, 250], [32, 400], [5, 100], [31, -80], [1, 100], [1, 400]], 'sub': {'dyna': [[1, -300], [31, -300]], 'bolt': [[1, -80]]}, 'as_of': 31}, {'control': 520, 'subledger': -680, 'difference': 1200, 'credit_balances': ['bolt', 'dyna'], 'open_customers': 2}], ['control 3', {'control': [[10, 100], [5, 250], [5, 400], [11, -80], [1, 400], [9, 400]], 'sub': {'dyna': [[5, 250]]}, 'as_of': 10}, {'control': 1550, 'subledger': 250, 'difference': 1300, 'credit_balances': [], 'open_customers': 1}], ['control 4', {'control': [[31, 250], [1, 400], [31, 100], [31, 400], [30, -80]], 'sub': {'bolt': [[1, -80], [1, -300], [31, -300]], 'acme': [[31, 100], [1, -300]], 'crux': [[1, 100]]}, 'as_of': 31}, {'control': 1070, 'subledger': -780, 'difference': 1850, 'credit_balances': ['acme', 'bolt'], 'open_customers': 3}], ['control 5', {'control': [[5, -80], [1, 250], [20, 100]], 'sub': {'dyna': [[1, 400], [5, -300]], 'crux': [[20, -80], [5, -80]]}, 'as_of': 20}, {'control': 270, 'subledger': -60, 'difference': 330, 'credit_balances': ['crux'], 'open_customers': 2}]], [['regression: subledger cutoff', {'control': [[9, 250], [11, 100], [11, -80], [11, 250]], 'sub': {'dyna': [[1, 250], [11, 400], [10, -80]], 'acme': [[11, -300], [1, 400], [1, 400]], 'bolt': [[1, -300]]}, 'as_of': 10}, {'control': 250, 'subledger': 670, 'difference': -420, 'credit_balances': ['bolt'], 'open_customers': 3}], ['control 1', {'control': [[1, -80], [1, 100], [32, 400], [31, 250]], 'sub': {'bolt': [[1, 250]]}, 'as_of': 31}, {'control': 270, 'subledger': 250, 'difference': 20, 'credit_balances': [], 'open_customers': 1}], ['control 2', {'control': [[9, 250], [1, 400], [11, 100]], 'sub': {'crux': [[5, -80], [1, 250], [1, 250]]}, 'as_of': 10}, {'control': 650, 'subledger': 420, 'difference': 230, 'credit_balances': [], 'open_customers': 1}], ['control 3', {'control': [[21, 250], [1, 400]], 'sub': {'acme': [[5, -300], [5, -80]]}, 'as_of': 20}, {'control': 400, 'subledger': -380, 'difference': 780, 'credit_balances': ['acme'], 'open_customers': 1}], ['control 4', {'control': [[1, -80], [1, 250], [10, 250], [1, 250], [5, 400]], 'sub': {'crux': [[5, -300]]}, 'as_of': 10}, {'control': 1070, 'subledger': -300, 'difference': 1370, 'credit_balances': ['crux'], 'open_customers': 1}], ['control 5', {'control': [[5, 250], [30, 400], [30, -80], [1, -80], [5, 250]], 'sub': {'bolt': [[5, 250]], 'acme': [[1, 250]], 'dyna': [[31, 400]]}, 'as_of': 31}, {'control': 740, 'subledger': 900, 'difference': -160, 'credit_balances': [], 'open_customers': 3}], ['control 6', {'control': [[10, 100], [5, 400], [10, -80], [1, -80]], 'sub': {'bolt': [[1, -80]], 'crux': [[1, 400], [1, -80]]}, 'as_of': 10}, {'control': 340, 'subledger': 240, 'difference': 100, 'credit_balances': ['bolt'], 'open_customers': 2}]], [['regression: subledger cutoff', {'control': [[20, -80], [21, 100]], 'sub': {'dyna': [[1, -80], [20, 250]], 'bolt': [[1, -300], [20, 400], [21, -300]]}, 'as_of': 20}, {'control': -80, 'subledger': 270, 'difference': -350, 'credit_balances': [], 'open_customers': 2}], ['control 1', {'control': [[31, 250], [5, -80], [1, 400], [32, 400], [1, 400]], 'sub': {'crux': [[5, 250], [1, 400]]}, 'as_of': 31}, {'control': 970, 'subledger': 650, 'difference': 320, 'credit_balances': [], 'open_customers': 1}], ['control 2', {'control': [[1, 400], [20, -80], [19, 250], [20, 250], [20, 250]], 'sub': {'acme': [[20, -300]], 'dyna': [[20, 100], [5, 400], [5, 400]], 'bolt': [[1, 250]]}, 'as_of': 20}, {'control': 1070, 'subledger': 850, 'difference': 220, 'credit_balances': ['acme'], 'open_customers': 3}], ['control 3', {'control': [[1, 100], [11, 100], [1, 100], [9, 400], [9, 100], [9, 100]], 'sub': {'dyna': [[1, 100], [5, 100]]}, 'as_of': 10}, {'control': 800, 'subledger': 200, 'difference': 600, 'credit_balances': [], 'open_customers': 1}], ['control 4', {'control': [[31, -80], [5, 100], [30, -80], [5, -80]], 'sub': {'dyna': [[1, 250], [31, 250], [5, 400]]}, 'as_of': 31}, {'control': -140, 'subledger': 900, 'difference': -1040, 'credit_balances': [], 'open_customers': 1}], ['control 5', {'control': [[21, -80], [20, 400], [19, 100], [20, 100], [21, 400], [20, 250]], 'sub': {'bolt': [[20, -300], [20, -80], [5, 250]], 'crux': [[5, 100], [20, 100]]}, 'as_of': 20}, {'control': 850, 'subledger': 70, 'difference': 780, 'credit_balances': ['bolt'], 'open_customers': 2}], ['control 6', {'control': [[11, 400], [11, -80]], 'sub': {'dyna': [[5, 250]], 'crux': [[10, -300]]}, 'as_of': 10}, {'control': 0, 'subledger': -50, 'difference': 50, 'credit_balances': ['crux'], 'open_customers': 2}]], [['regression: subledger cutoff', {'control': [[5, 100], [5, 250], [19, -80], [21, 250], [21, 250]], 'sub': {'crux': [[20, -80], [20, -80]], 'acme': [[21, 100]]}, 'as_of': 20}, {'control': 270, 'subledger': -160, 'difference': 430, 'credit_balances': ['crux'], 'open_customers': 1}], ['regression: subledger cutoff, partial-repair probe', {'control': [[11, -80], [10, 400], [11, -80], [1, 100], [9, 100]], 'sub': {'crux': [[5, -300], [1, -300]], 'dyna': [[5, -80]], 'bolt': [[1, -300], [10, -80], [1, 100]], 'acme': [[10, 250], [11, -80]]}, 'as_of': 10}, {'control': 600, 'subledger': -710, 'difference': 1310, 'credit_balances': ['bolt', 'crux', 'dyna'], 'open_customers': 4}], ['control 1', {'control': [[10, -80], [9, 400], [1, 250]], 'sub': {'crux': [[5, 250], [1, 400], [5, 250]], 'acme': [[10, -80]]}, 'as_of': 10}, {'control': 570, 'subledger': 820, 'difference': -250, 'credit_balances': ['acme'], 'open_customers': 2}], ['control 2', {'control': [[1, -80], [5, 100], [20, 400], [19, 250], [1, 250]], 'sub': {'crux': [[20, 250], [20, -80], [20, 250]]}, 'as_of': 20}, {'control': 920, 'subledger': 420, 'difference': 500, 'credit_balances': [], 'open_customers': 1}], ['control 3', {'control': [[1, 100], [1, 250], [5, 250], [1, 100], [1, 400], [5, -80]], 'sub': {'bolt': [[10, -80], [10, 400]], 'crux': [[1, 250], [10, 250]]}, 'as_of': 10}, {'control': 1020, 'subledger': 820, 'difference': 200, 'credit_balances': [], 'open_customers': 2}], ['control 4', {'control': [[10, 100], [5, 250], [11, 400], [9, 400]], 'sub': {'bolt': [[1, -80], [5, -300]]}, 'as_of': 10}, {'control': 750, 'subledger': -380, 'difference': 1130, 'credit_balances': ['bolt'], 'open_customers': 1}], ['control 5', {'control': [[5, 400], [11, 100]], 'sub': {'bolt': [[5, 400], [1, -80]], 'acme': [[5, 400], [1, 100], [5, -300]], 'dyna': [[1, 100]]}, 'as_of': 10}, {'control': 400, 'subledger': 620, 'difference': -220, 'credit_balances': [], 'open_customers': 3}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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):
    cut = x['as_of']
    control = sum(a for d, a in x['control'] if d <= cut)
    per = {}
    for cust, rows in x['sub'].items():
        per[cust] = sum(a for d, a in rows if d <= cut or a < 0)
    sub_total = sum(per.values())
    credit = sorted(c for c, v in per.items() if v < 0)
    return {'control': control, 'subledger': sub_total, 'difference': control - sub_total, 'credit_balances': credit, 'open_customers': sum(1 for v in per.values() if v != 0)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: subledger cutoff', {'control': [[5, 100], [19, -80], [21, -80]], 'sub': {'bolt': [[5, -80], [1, 250], [5, -80]], 'acme': [[21, 100]], 'dyna': [[20, 400], [1, 250], [20, 100]], 'crux': [[20, 400], [5, 100]]}, 'as_of': 20}, {'control': 20, 'subledger': 1340, 'difference': -1320, 'credit_balances': [], 'open_customers': 3}], ['regression: subledger cutoff, partial-repair probe', {'control': [[5, 100], [21, 250], [19, 400], [5, 100]], 'sub': {'dyna': [[21, -80], [21, 100], [20, 400]], 'acme': [[5, 100], [20, -80], [21, -300]], 'bolt': [[5, -80], [21, -300]]}, 'as_of': 20}, {'control': 600, 'subledger': 340, 'difference': 260, 'credit_balances': ['bolt'], 'open_customers': 3}], ['control 1', {'control': [[21, 400], [5, 250], [19, 250], [20, 400], [19, -80]], 'sub': {'bolt': [[5, 250]]}, 'as_of': 20}, {'control': 820, 'subledger': 250, 'difference': 570, 'credit_balances': [], 'open_customers': 1}], ['control 2', {'control': [[20, 250], [21, 100], [1, 400], [5, 100]], 'sub': {'acme': [[20, -300]], 'dyna': [[5, 250], [1, -80]]}, 'as_of': 20}, {'control': 750, 'subledger': -130, 'difference': 880, 'credit_balances': ['acme'], 'open_customers': 2}], ['control 3', {'control': [[32, 100], [30, -80], [5, 100], [5, 100]], 'sub': {'crux': [[5, 100], [1, 250], [1, -300]], 'bolt': [[31, 250], [5, -300]]}, 'as_of': 31}, {'control': 120, 'subledger': 0, 'difference': 120, 'credit_balances': ['bolt'], 'open_customers': 2}], ['control 4', {'control': [[32, 400], [32, 250], [31, -80]], 'sub': {'dyna': [[31, 400], [1, -300]], 'bolt': [[5, 250], [5, 250], [1, -300]], 'crux': [[31, -300], [1, -80], [31, -300]], 'acme': [[31, 250]]}, 'as_of': 31}, {'control': -80, 'subledger': -130, 'difference': 50, 'credit_balances': ['crux'], 'open_customers': 4}], ['control 5', {'control': [[5, 400], [21, 400], [19, -80], [19, 400], [19, 250]], 'sub': {'bolt': [[5, 100]], 'acme': [[1, 250]]}, 'as_of': 20}, {'control': 970, 'subledger': 350, 'difference': 620, 'credit_balances': [], 'open_customers': 2}]], [['regression: subledger cutoff', {'control': [[1, 400], [11, 250]], 'sub': {'dyna': [[1, 250]], 'crux': [[1, 250], [1, 250], [1, 100]], 'acme': [[10, 250], [1, 250], [11, 250]], 'bolt': [[11, 250], [5, 400]]}, 'as_of': 10}, {'control': 400, 'subledger': 1750, 'difference': -1350, 'credit_balances': [], 'open_customers': 4}], ['regression: subledger cutoff, partial-repair probe', {'control': [[5, 400], [20, -80], [19, 250]], 'sub': {'acme': [[21, 250]], 'bolt': [[20, 250], [1, -80], [1, -80]], 'dyna': [[21, -80], [20, 100]], 'crux': [[5, 400], [21, 400], [20, -300]]}, 'as_of': 20}, {'control': 570, 'subledger': 290, 'difference': 280, 'credit_balances': [], 'open_customers': 3}], ['control 1', {'control': [[20, 250], [19, 100], [5, 400]], 'sub': {'bolt': [[20, 250]], 'acme': [[1, -300], [1, 100], [1, 250]]}, 'as_of': 20}, {'control': 750, 'subledger': 300, 'difference': 450, 'credit_balances': [], 'open_customers': 2}], ['control 2', {'control': [[32, 250], [32, 400], [5, 100], [31, -80], [1, 100], [1, 400]], 'sub': {'dyna': [[1, -300], [31, -300]], 'bolt': [[1, -80]]}, 'as_of': 31}, {'control': 520, 'subledger': -680, 'difference': 1200, 'credit_balances': ['bolt', 'dyna'], 'open_customers': 2}], ['control 3', {'control': [[10, 100], [5, 250], [5, 400], [11, -80], [1, 400], [9, 400]], 'sub': {'dyna': [[5, 250]]}, 'as_of': 10}, {'control': 1550, 'subledger': 250, 'difference': 1300, 'credit_balances': [], 'open_customers': 1}], ['control 4', {'control': [[31, 250], [1, 400], [31, 100], [31, 400], [30, -80]], 'sub': {'bolt': [[1, -80], [1, -300], [31, -300]], 'acme': [[31, 100], [1, -300]], 'crux': [[1, 100]]}, 'as_of': 31}, {'control': 1070, 'subledger': -780, 'difference': 1850, 'credit_balances': ['acme', 'bolt'], 'open_customers': 3}], ['control 5', {'control': [[5, -80], [1, 250], [20, 100]], 'sub': {'dyna': [[1, 400], [5, -300]], 'crux': [[20, -80], [5, -80]]}, 'as_of': 20}, {'control': 270, 'subledger': -60, 'difference': 330, 'credit_balances': ['crux'], 'open_customers': 2}]], [['regression: subledger cutoff', {'control': [[9, 250], [11, 100], [11, -80], [11, 250]], 'sub': {'dyna': [[1, 250], [11, 400], [10, -80]], 'acme': [[11, -300], [1, 400], [1, 400]], 'bolt': [[1, -300]]}, 'as_of': 10}, {'control': 250, 'subledger': 670, 'difference': -420, 'credit_balances': ['bolt'], 'open_customers': 3}], ['control 1', {'control': [[1, -80], [1, 100], [32, 400], [31, 250]], 'sub': {'bolt': [[1, 250]]}, 'as_of': 31}, {'control': 270, 'subledger': 250, 'difference': 20, 'credit_balances': [], 'open_customers': 1}], ['control 2', {'control': [[9, 250], [1, 400], [11, 100]], 'sub': {'crux': [[5, -80], [1, 250], [1, 250]]}, 'as_of': 10}, {'control': 650, 'subledger': 420, 'difference': 230, 'credit_balances': [], 'open_customers': 1}], ['control 3', {'control': [[21, 250], [1, 400]], 'sub': {'acme': [[5, -300], [5, -80]]}, 'as_of': 20}, {'control': 400, 'subledger': -380, 'difference': 780, 'credit_balances': ['acme'], 'open_customers': 1}], ['control 4', {'control': [[1, -80], [1, 250], [10, 250], [1, 250], [5, 400]], 'sub': {'crux': [[5, -300]]}, 'as_of': 10}, {'control': 1070, 'subledger': -300, 'difference': 1370, 'credit_balances': ['crux'], 'open_customers': 1}], ['control 5', {'control': [[5, 250], [30, 400], [30, -80], [1, -80], [5, 250]], 'sub': {'bolt': [[5, 250]], 'acme': [[1, 250]], 'dyna': [[31, 400]]}, 'as_of': 31}, {'control': 740, 'subledger': 900, 'difference': -160, 'credit_balances': [], 'open_customers': 3}], ['control 6', {'control': [[10, 100], [5, 400], [10, -80], [1, -80]], 'sub': {'bolt': [[1, -80]], 'crux': [[1, 400], [1, -80]]}, 'as_of': 10}, {'control': 340, 'subledger': 240, 'difference': 100, 'credit_balances': ['bolt'], 'open_customers': 2}]], [['regression: subledger cutoff', {'control': [[20, -80], [21, 100]], 'sub': {'dyna': [[1, -80], [20, 250]], 'bolt': [[1, -300], [20, 400], [21, -300]]}, 'as_of': 20}, {'control': -80, 'subledger': 270, 'difference': -350, 'credit_balances': [], 'open_customers': 2}], ['control 1', {'control': [[31, 250], [5, -80], [1, 400], [32, 400], [1, 400]], 'sub': {'crux': [[5, 250], [1, 400]]}, 'as_of': 31}, {'control': 970, 'subledger': 650, 'difference': 320, 'credit_balances': [], 'open_customers': 1}], ['control 2', {'control': [[1, 400], [20, -80], [19, 250], [20, 250], [20, 250]], 'sub': {'acme': [[20, -300]], 'dyna': [[20, 100], [5, 400], [5, 400]], 'bolt': [[1, 250]]}, 'as_of': 20}, {'control': 1070, 'subledger': 850, 'difference': 220, 'credit_balances': ['acme'], 'open_customers': 3}], ['control 3', {'control': [[1, 100], [11, 100], [1, 100], [9, 400], [9, 100], [9, 100]], 'sub': {'dyna': [[1, 100], [5, 100]]}, 'as_of': 10}, {'control': 800, 'subledger': 200, 'difference': 600, 'credit_balances': [], 'open_customers': 1}], ['control 4', {'control': [[31, -80], [5, 100], [30, -80], [5, -80]], 'sub': {'dyna': [[1, 250], [31, 250], [5, 400]]}, 'as_of': 31}, {'control': -140, 'subledger': 900, 'difference': -1040, 'credit_balances': [], 'open_customers': 1}], ['control 5', {'control': [[21, -80], [20, 400], [19, 100], [20, 100], [21, 400], [20, 250]], 'sub': {'bolt': [[20, -300], [20, -80], [5, 250]], 'crux': [[5, 100], [20, 100]]}, 'as_of': 20}, {'control': 850, 'subledger': 70, 'difference': 780, 'credit_balances': ['bolt'], 'open_customers': 2}], ['control 6', {'control': [[11, 400], [11, -80]], 'sub': {'dyna': [[5, 250]], 'crux': [[10, -300]]}, 'as_of': 10}, {'control': 0, 'subledger': -50, 'difference': 50, 'credit_balances': ['crux'], 'open_customers': 2}]], [['regression: subledger cutoff', {'control': [[5, 100], [5, 250], [19, -80], [21, 250], [21, 250]], 'sub': {'crux': [[20, -80], [20, -80]], 'acme': [[21, 100]]}, 'as_of': 20}, {'control': 270, 'subledger': -160, 'difference': 430, 'credit_balances': ['crux'], 'open_customers': 1}], ['regression: subledger cutoff, partial-repair probe', {'control': [[11, -80], [10, 400], [11, -80], [1, 100], [9, 100]], 'sub': {'crux': [[5, -300], [1, -300]], 'dyna': [[5, -80]], 'bolt': [[1, -300], [10, -80], [1, 100]], 'acme': [[10, 250], [11, -80]]}, 'as_of': 10}, {'control': 600, 'subledger': -710, 'difference': 1310, 'credit_balances': ['bolt', 'crux', 'dyna'], 'open_customers': 4}], ['control 1', {'control': [[10, -80], [9, 400], [1, 250]], 'sub': {'crux': [[5, 250], [1, 400], [5, 250]], 'acme': [[10, -80]]}, 'as_of': 10}, {'control': 570, 'subledger': 820, 'difference': -250, 'credit_balances': ['acme'], 'open_customers': 2}], ['control 2', {'control': [[1, -80], [5, 100], [20, 400], [19, 250], [1, 250]], 'sub': {'crux': [[20, 250], [20, -80], [20, 250]]}, 'as_of': 20}, {'control': 920, 'subledger': 420, 'difference': 500, 'credit_balances': [], 'open_customers': 1}], ['control 3', {'control': [[1, 100], [1, 250], [5, 250], [1, 100], [1, 400], [5, -80]], 'sub': {'bolt': [[10, -80], [10, 400]], 'crux': [[1, 250], [10, 250]]}, 'as_of': 10}, {'control': 1020, 'subledger': 820, 'difference': 200, 'credit_balances': [], 'open_customers': 2}], ['control 4', {'control': [[10, 100], [5, 250], [11, 400], [9, 400]], 'sub': {'bolt': [[1, -80], [5, -300]]}, 'as_of': 10}, {'control': 750, 'subledger': -380, 'difference': 1130, 'credit_balances': ['bolt'], 'open_customers': 1}], ['control 5', {'control': [[5, 400], [11, 100]], 'sub': {'bolt': [[5, 400], [1, -80]], 'acme': [[5, 400], [1, 100], [5, -300]], 'dyna': [[1, 100]]}, 'as_of': 10}, {'control': 400, 'subledger': 620, 'difference': -220, 'credit_balances': [], 'open_customers': 3}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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:
Future-dated invoices appear in the customer balances.

x = {'control': [[day, amount]], 'sub': {customer: [[day, amount]]}, 'as_of': day}. Only rows dated on or before as_of count. The subledger total nets every customer balance, including credit (negative) balances. Return {'control', 'subledger', 'difference': control - subledger, 'credit_balances': customers with negative balance sorted by name, 'open_customers': count of nonzero balances}.

split: open-access


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

N = 1
observations = []
def solve(x):
    cut = x['as_of']
    control = sum(a for d, a in x['control'] if d <= cut)
    per = {}
    for cust, rows in x['sub'].items():
        per[cust] = sum(a for d, a in rows if d <= cut)
    sub_total = sum(v for v in per.values() if v > 0)
    credit = sorted(c for c, v in per.items() if v < 0)
    return {'control': control, 'subledger': sub_total, 'difference': control - sub_total, 'credit_balances': credit, 'open_customers': sum(1 for v in per.values() if v != 0)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: credit balances in total', {'control': [[9, 250], [10, 100], [11, 400]], 'sub': {'dyna': [[1, 400], [11, 400]], 'acme': [[1, -300], [10, 250], [1, -80]], 'bolt': [[1, -80]]}, 'as_of': 10}, {'control': 350, 'subledger': 190, 'difference': 160, 'credit_balances': ['acme', 'bolt'], 'open_customers': 3}], ['control 1', {'control': [[5, 100], [19, -80], [21, -80]], 'sub': {'bolt': [[5, -80], [1, 250], [5, -80]], 'acme': [[21, 100]], 'dyna': [[20, 400], [1, 250], [20, 100]], 'crux': [[20, 400], [5, 100]]}, 'as_of': 20}, {'control': 20, 'subledger': 1340, 'difference': -1320, 'credit_balances': [], 'open_customers': 3}], ['control 2', {'control': [[21, 400], [5, 250], [19, 250], [20, 400], [19, -80]], 'sub': {'bolt': [[5, 250]]}, 'as_of': 20}, {'control': 820, 'subledger': 250, 'difference': 570, 'credit_balances': [], 'open_customers': 1}], ['control 3', {'control': [[9, 100], [10, 100], [9, 400], [10, 100], [1, 250], [9, 250]], 'sub': {'dyna': [[1, 400], [11, 400], [1, 400]]}, 'as_of': 10}, {'control': 1200, 'subledger': 800, 'difference': 400, 'credit_balances': [], 'open_customers': 1}], ['control 4', {'control': [[10, 400], [10, 100], [9, 250], [11, 250], [1, 400]], 'sub': {'bolt': [[11, 400], [10, 400], [5, 100]]}, 'as_of': 10}, {'control': 1150, 'subledger': 500, 'difference': 650, 'credit_balances': [], 'open_customers': 1}], ['control 5', {'control': [[5, 400], [1, 250], [9, -80], [5, -80], [5, 250]], 'sub': {'acme': [[5, 250], [11, 100]], 'crux': [[5, 400]], 'dyna': [[5, 250]]}, 'as_of': 10}, {'control': 740, 'subledger': 900, 'difference': -160, 'credit_balances': [], 'open_customers': 3}], ['control 6', {'control': [[11, 250], [1, -80], [11, 400], [1, -80]], 'sub': {'acme': [[1, 250], [1, -80], [11, 250]], 'bolt': [[11, 100]], 'dyna': [[11, -80]]}, 'as_of': 10}, {'control': -160, 'subledger': 170, 'difference': -330, 'credit_balances': [], 'open_customers': 1}]], [['regression: credit balances in total', {'control': [[31, 400], [30, 250]], 'sub': {'bolt': [[1, 400], [1, 400], [5, -300]], 'acme': [[32, 250]], 'crux': [[5, -300], [31, 400], [5, -300]]}, 'as_of': 31}, {'control': 650, 'subledger': 300, 'difference': 350, 'credit_balances': ['crux'], 'open_customers': 2}], ['control 1', {'control': [[1, 400], [11, 250]], 'sub': {'dyna': [[1, 250]], 'crux': [[1, 250], [1, 250], [1, 100]], 'acme': [[10, 250], [1, 250], [11, 250]], 'bolt': [[11, 250], [5, 400]]}, 'as_of': 10}, {'control': 400, 'subledger': 1750, 'difference': -1350, 'credit_balances': [], 'open_customers': 4}], ['control 2', {'control': [[20, 250], [19, 100], [5, 400]], 'sub': {'bolt': [[20, 250]], 'acme': [[1, -300], [1, 100], [1, 250]]}, 'as_of': 20}, {'control': 750, 'subledger': 300, 'difference': 450, 'credit_balances': [], 'open_customers': 2}], ['control 3', {'control': [[5, 400], [20, -80], [19, 250]], 'sub': {'acme': [[21, 250]], 'bolt': [[20, 250], [1, -80], [1, -80]], 'dyna': [[21, -80], [20, 100]], 'crux': [[5, 400], [21, 400], [20, -300]]}, 'as_of': 20}, {'control': 570, 'subledger': 290, 'difference': 280, 'credit_balances': [], 'open_customers': 3}], ['control 4', {'control': [[10, 250], [9, -80], [1, 250], [1, 100]], 'sub': {'dyna': [[1, 400], [10, 400]], 'acme': [[10, 250], [5, 400], [11, -300]], 'crux': [[11, 250], [1, 400], [5, -300]], 'bolt': [[5, 250]]}, 'as_of': 10}, {'control': 520, 'subledger': 1800, 'difference': -1280, 'credit_balances': [], 'open_customers': 4}], ['control 5', {'control': [[5, -80], [19, 400], [1, -80]], 'sub': {'crux': [[21, -300], [21, 400]], 'bolt': [[1, 250], [21, 250]], 'dyna': [[21, -300], [20, 250], [5, 250]]}, 'as_of': 20}, {'control': 240, 'subledger': 750, 'difference': -510, 'credit_balances': [], 'open_customers': 2}], ['control 6', {'control': [[10, 100], [5, 250], [5, 400], [11, -80], [1, 400], [9, 400]], 'sub': {'dyna': [[5, 250]]}, 'as_of': 10}, {'control': 1550, 'subledger': 250, 'difference': 1300, 'credit_balances': [], 'open_customers': 1}]], [['regression: credit balances in total', {'control': [[9, 250], [11, 100], [11, -80], [11, 250]], 'sub': {'dyna': [[1, 250], [11, 400], [10, -80]], 'acme': [[11, -300], [1, 400], [1, 400]], 'bolt': [[1, -300]]}, 'as_of': 10}, {'control': 250, 'subledger': 670, 'difference': -420, 'credit_balances': ['bolt'], 'open_customers': 3}], ['control 1', {'control': [[1, -80], [1, 100], [32, 400], [31, 250]], 'sub': {'bolt': [[1, 250]]}, 'as_of': 31}, {'control': 270, 'subledger': 250, 'difference': 20, 'credit_balances': [], 'open_customers': 1}], ['control 2', {'control': [[5, 400], [1, 250], [5, -80], [5, 250], [11, -80], [1, 400]], 'sub': {'dyna': [[11, 100]], 'acme': [[5, 400], [1, 100]], 'crux': [[5, 400], [5, -300]]}, 'as_of': 10}, {'control': 1220, 'subledger': 600, 'difference': 620, 'credit_balances': [], 'open_customers': 2}], ['control 3', {'control': [[9, 250], [1, 400], [11, 100]], 'sub': {'crux': [[5, -80], [1, 250], [1, 250]]}, 'as_of': 10}, {'control': 650, 'subledger': 420, 'difference': 230, 'credit_balances': [], 'open_customers': 1}], ['control 4', {'control': [[5, 250], [30, 400], [30, -80], [1, -80], [5, 250]], 'sub': {'bolt': [[5, 250]], 'acme': [[1, 250]], 'dyna': [[31, 400]]}, 'as_of': 31}, {'control': 740, 'subledger': 900, 'difference': -160, 'credit_balances': [], 'open_customers': 3}], ['control 5', {'control': [[19, 100], [21, 250], [20, -80], [20, -80], [19, 400], [21, 100]], 'sub': {'dyna': [[21, 100]], 'acme': [[21, 100], [21, 400], [20, 250]], 'crux': [[20, -80], [21, -80], [5, 250]]}, 'as_of': 20}, {'control': 340, 'subledger': 420, 'difference': -80, 'credit_balances': [], 'open_customers': 2}], ['control 6', {'control': [[31, -80], [32, 400], [31, -80], [5, -80], [31, 250]], 'sub': {'dyna': [[1, 400], [1, 250]], 'acme': [[32, -80], [31, 100]]}, 'as_of': 31}, {'control': 10, 'subledger': 750, 'difference': -740, 'credit_balances': [], 'open_customers': 2}]], [['regression: credit balances in total', {'control': [[31, -80], [5, 100], [31, 100]], 'sub': {'crux': [[32, 250]], 'dyna': [[32, 250]], 'acme': [[1, -300], [32, 250]]}, 'as_of': 31}, {'control': 120, 'subledger': -300, 'difference': 420, 'credit_balances': ['acme'], 'open_customers': 1}], ['control 1', {'control': [[20, -80], [21, 100]], 'sub': {'dyna': [[1, -80], [20, 250]], 'bolt': [[1, -300], [20, 400], [21, -300]]}, 'as_of': 20}, {'control': -80, 'subledger': 270, 'difference': -350, 'credit_balances': [], 'open_customers': 2}], ['control 2', {'control': [[1, -80], [20, -80], [19, -80], [19, -80], [21, 400]], 'sub': {'acme': [[21, 250]]}, 'as_of': 20}, {'control': -320, 'subledger': 0, 'difference': -320, 'credit_balances': [], 'open_customers': 0}], ['control 3', {'control': [[5, 250], [5, 400], [1, -80], [31, 100], [30, 100]], 'sub': {'dyna': [[32, -300]], 'crux': [[5, 250], [1, 250]], 'acme': [[31, 400]]}, 'as_of': 31}, {'control': 770, 'subledger': 900, 'difference': -130, 'credit_balances': [], 'open_customers': 2}], ['control 4', {'control': [[31, 250], [1, -80], [5, 100], [31, 250], [5, 100]], 'sub': {'crux': [[32, -300], [1, 250]]}, 'as_of': 31}, {'control': 620, 'subledger': 250, 'difference': 370, 'credit_balances': [], 'open_customers': 1}], ['control 5', {'control': [[31, 250], [5, -80], [1, 400], [32, 400], [1, 400]], 'sub': {'crux': [[5, 250], [1, 400]]}, 'as_of': 31}, {'control': 970, 'subledger': 650, 'difference': 320, 'credit_balances': [], 'open_customers': 1}], ['control 6', {'control': [[1, 100], [11, 100], [1, 100], [9, 400], [9, 100], [9, 100]], 'sub': {'dyna': [[1, 100], [5, 100]]}, 'as_of': 10}, {'control': 800, 'subledger': 200, 'difference': 600, 'credit_balances': [], 'open_customers': 1}]], [['regression: credit balances in total', {'control': [[5, 100], [5, 250], [19, -80], [21, 250], [21, 250]], 'sub': {'crux': [[20, -80], [20, -80]], 'acme': [[21, 100]]}, 'as_of': 20}, {'control': 270, 'subledger': -160, 'difference': 430, 'credit_balances': ['crux'], 'open_customers': 1}], ['control 1', {'control': [[1, -80], [5, 100], [20, 400], [19, 250], [1, 250]], 'sub': {'crux': [[20, 250], [20, -80], [20, 250]]}, 'as_of': 20}, {'control': 920, 'subledger': 420, 'difference': 500, 'credit_balances': [], 'open_customers': 1}], ['control 2', {'control': [[9, -80], [11, 400]], 'sub': {'bolt': [[5, 100], [5, 250]], 'acme': [[11, 100], [11, -80], [10, 250]]}, 'as_of': 10}, {'control': -80, 'subledger': 600, 'difference': -680, 'credit_balances': [], 'open_customers': 2}], ['control 3', {'control': [[1, 100], [1, 250], [5, 250], [1, 100], [1, 400], [5, -80]], 'sub': {'bolt': [[10, -80], [10, 400]], 'crux': [[1, 250], [10, 250]]}, 'as_of': 10}, {'control': 1020, 'subledger': 820, 'difference': 200, 'credit_balances': [], 'open_customers': 2}], ['control 4', {'control': [[32, 250], [1, 100], [32, 100], [31, 100], [1, -80]], 'sub': {'dyna': [[5, 100]], 'acme': [[5, 400], [5, -80]], 'crux': [[32, -80]], 'bolt': [[32, -80]]}, 'as_of': 31}, {'control': 120, 'subledger': 420, 'difference': -300, 'credit_balances': [], 'open_customers': 2}], ['control 5', {'control': [[5, 400], [11, 100]], 'sub': {'bolt': [[5, 400], [1, -80]], 'acme': [[5, 400], [1, 100], [5, -300]], 'dyna': [[1, 100]]}, 'as_of': 10}, {'control': 400, 'subledger': 620, 'difference': -220, 'credit_balances': [], 'open_customers': 3}], ['control 6', {'control': [[31, 400], [32, 400]], 'sub': {'acme': [[1, -80], [31, 400]]}, 'as_of': 31}, {'control': 400, 'subledger': 320, 'difference': 80, 'credit_balances': [], 'open_customers': 1}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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):
    cut = x['as_of']
    control = sum(a for d, a in x['control'] if d <= cut)
    per = {}
    for cust, rows in x['sub'].items():
        per[cust] = sum(a for d, a in rows if d <= cut)
    sub_total = sum(abs(v) for v in per.values())
    credit = sorted(c for c, v in per.items() if v < 0)
    return {'control': control, 'subledger': sub_total, 'difference': control - sub_total, 'credit_balances': credit, 'open_customers': sum(1 for v in per.values() if v != 0)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: credit balances in total', {'control': [[9, 250], [10, 100], [11, 400]], 'sub': {'dyna': [[1, 400], [11, 400]], 'acme': [[1, -300], [10, 250], [1, -80]], 'bolt': [[1, -80]]}, 'as_of': 10}, {'control': 350, 'subledger': 190, 'difference': 160, 'credit_balances': ['acme', 'bolt'], 'open_customers': 3}], ['control 1', {'control': [[5, 100], [19, -80], [21, -80]], 'sub': {'bolt': [[5, -80], [1, 250], [5, -80]], 'acme': [[21, 100]], 'dyna': [[20, 400], [1, 250], [20, 100]], 'crux': [[20, 400], [5, 100]]}, 'as_of': 20}, {'control': 20, 'subledger': 1340, 'difference': -1320, 'credit_balances': [], 'open_customers': 3}], ['control 2', {'control': [[21, 400], [5, 250], [19, 250], [20, 400], [19, -80]], 'sub': {'bolt': [[5, 250]]}, 'as_of': 20}, {'control': 820, 'subledger': 250, 'difference': 570, 'credit_balances': [], 'open_customers': 1}], ['control 3', {'control': [[9, 100], [10, 100], [9, 400], [10, 100], [1, 250], [9, 250]], 'sub': {'dyna': [[1, 400], [11, 400], [1, 400]]}, 'as_of': 10}, {'control': 1200, 'subledger': 800, 'difference': 400, 'credit_balances': [], 'open_customers': 1}], ['control 4', {'control': [[10, 400], [10, 100], [9, 250], [11, 250], [1, 400]], 'sub': {'bolt': [[11, 400], [10, 400], [5, 100]]}, 'as_of': 10}, {'control': 1150, 'subledger': 500, 'difference': 650, 'credit_balances': [], 'open_customers': 1}], ['control 5', {'control': [[5, 400], [1, 250], [9, -80], [5, -80], [5, 250]], 'sub': {'acme': [[5, 250], [11, 100]], 'crux': [[5, 400]], 'dyna': [[5, 250]]}, 'as_of': 10}, {'control': 740, 'subledger': 900, 'difference': -160, 'credit_balances': [], 'open_customers': 3}], ['control 6', {'control': [[11, 250], [1, -80], [11, 400], [1, -80]], 'sub': {'acme': [[1, 250], [1, -80], [11, 250]], 'bolt': [[11, 100]], 'dyna': [[11, -80]]}, 'as_of': 10}, {'control': -160, 'subledger': 170, 'difference': -330, 'credit_balances': [], 'open_customers': 1}]], [['regression: credit balances in total', {'control': [[31, 400], [30, 250]], 'sub': {'bolt': [[1, 400], [1, 400], [5, -300]], 'acme': [[32, 250]], 'crux': [[5, -300], [31, 400], [5, -300]]}, 'as_of': 31}, {'control': 650, 'subledger': 300, 'difference': 350, 'credit_balances': ['crux'], 'open_customers': 2}], ['control 1', {'control': [[1, 400], [11, 250]], 'sub': {'dyna': [[1, 250]], 'crux': [[1, 250], [1, 250], [1, 100]], 'acme': [[10, 250], [1, 250], [11, 250]], 'bolt': [[11, 250], [5, 400]]}, 'as_of': 10}, {'control': 400, 'subledger': 1750, 'difference': -1350, 'credit_balances': [], 'open_customers': 4}], ['control 2', {'control': [[20, 250], [19, 100], [5, 400]], 'sub': {'bolt': [[20, 250]], 'acme': [[1, -300], [1, 100], [1, 250]]}, 'as_of': 20}, {'control': 750, 'subledger': 300, 'difference': 450, 'credit_balances': [], 'open_customers': 2}], ['control 3', {'control': [[5, 400], [20, -80], [19, 250]], 'sub': {'acme': [[21, 250]], 'bolt': [[20, 250], [1, -80], [1, -80]], 'dyna': [[21, -80], [20, 100]], 'crux': [[5, 400], [21, 400], [20, -300]]}, 'as_of': 20}, {'control': 570, 'subledger': 290, 'difference': 280, 'credit_balances': [], 'open_customers': 3}], ['control 4', {'control': [[10, 250], [9, -80], [1, 250], [1, 100]], 'sub': {'dyna': [[1, 400], [10, 400]], 'acme': [[10, 250], [5, 400], [11, -300]], 'crux': [[11, 250], [1, 400], [5, -300]], 'bolt': [[5, 250]]}, 'as_of': 10}, {'control': 520, 'subledger': 1800, 'difference': -1280, 'credit_balances': [], 'open_customers': 4}], ['control 5', {'control': [[5, -80], [19, 400], [1, -80]], 'sub': {'crux': [[21, -300], [21, 400]], 'bolt': [[1, 250], [21, 250]], 'dyna': [[21, -300], [20, 250], [5, 250]]}, 'as_of': 20}, {'control': 240, 'subledger': 750, 'difference': -510, 'credit_balances': [], 'open_customers': 2}], ['control 6', {'control': [[10, 100], [5, 250], [5, 400], [11, -80], [1, 400], [9, 400]], 'sub': {'dyna': [[5, 250]]}, 'as_of': 10}, {'control': 1550, 'subledger': 250, 'difference': 1300, 'credit_balances': [], 'open_customers': 1}]], [['regression: credit balances in total', {'control': [[9, 250], [11, 100], [11, -80], [11, 250]], 'sub': {'dyna': [[1, 250], [11, 400], [10, -80]], 'acme': [[11, -300], [1, 400], [1, 400]], 'bolt': [[1, -300]]}, 'as_of': 10}, {'control': 250, 'subledger': 670, 'difference': -420, 'credit_balances': ['bolt'], 'open_customers': 3}], ['control 1', {'control': [[1, -80], [1, 100], [32, 400], [31, 250]], 'sub': {'bolt': [[1, 250]]}, 'as_of': 31}, {'control': 270, 'subledger': 250, 'difference': 20, 'credit_balances': [], 'open_customers': 1}], ['control 2', {'control': [[5, 400], [1, 250], [5, -80], [5, 250], [11, -80], [1, 400]], 'sub': {'dyna': [[11, 100]], 'acme': [[5, 400], [1, 100]], 'crux': [[5, 400], [5, -300]]}, 'as_of': 10}, {'control': 1220, 'subledger': 600, 'difference': 620, 'credit_balances': [], 'open_customers': 2}], ['control 3', {'control': [[9, 250], [1, 400], [11, 100]], 'sub': {'crux': [[5, -80], [1, 250], [1, 250]]}, 'as_of': 10}, {'control': 650, 'subledger': 420, 'difference': 230, 'credit_balances': [], 'open_customers': 1}], ['control 4', {'control': [[5, 250], [30, 400], [30, -80], [1, -80], [5, 250]], 'sub': {'bolt': [[5, 250]], 'acme': [[1, 250]], 'dyna': [[31, 400]]}, 'as_of': 31}, {'control': 740, 'subledger': 900, 'difference': -160, 'credit_balances': [], 'open_customers': 3}], ['control 5', {'control': [[19, 100], [21, 250], [20, -80], [20, -80], [19, 400], [21, 100]], 'sub': {'dyna': [[21, 100]], 'acme': [[21, 100], [21, 400], [20, 250]], 'crux': [[20, -80], [21, -80], [5, 250]]}, 'as_of': 20}, {'control': 340, 'subledger': 420, 'difference': -80, 'credit_balances': [], 'open_customers': 2}], ['control 6', {'control': [[31, -80], [32, 400], [31, -80], [5, -80], [31, 250]], 'sub': {'dyna': [[1, 400], [1, 250]], 'acme': [[32, -80], [31, 100]]}, 'as_of': 31}, {'control': 10, 'subledger': 750, 'difference': -740, 'credit_balances': [], 'open_customers': 2}]], [['regression: credit balances in total', {'control': [[31, -80], [5, 100], [31, 100]], 'sub': {'crux': [[32, 250]], 'dyna': [[32, 250]], 'acme': [[1, -300], [32, 250]]}, 'as_of': 31}, {'control': 120, 'subledger': -300, 'difference': 420, 'credit_balances': ['acme'], 'open_customers': 1}], ['control 1', {'control': [[20, -80], [21, 100]], 'sub': {'dyna': [[1, -80], [20, 250]], 'bolt': [[1, -300], [20, 400], [21, -300]]}, 'as_of': 20}, {'control': -80, 'subledger': 270, 'difference': -350, 'credit_balances': [], 'open_customers': 2}], ['control 2', {'control': [[1, -80], [20, -80], [19, -80], [19, -80], [21, 400]], 'sub': {'acme': [[21, 250]]}, 'as_of': 20}, {'control': -320, 'subledger': 0, 'difference': -320, 'credit_balances': [], 'open_customers': 0}], ['control 3', {'control': [[5, 250], [5, 400], [1, -80], [31, 100], [30, 100]], 'sub': {'dyna': [[32, -300]], 'crux': [[5, 250], [1, 250]], 'acme': [[31, 400]]}, 'as_of': 31}, {'control': 770, 'subledger': 900, 'difference': -130, 'credit_balances': [], 'open_customers': 2}], ['control 4', {'control': [[31, 250], [1, -80], [5, 100], [31, 250], [5, 100]], 'sub': {'crux': [[32, -300], [1, 250]]}, 'as_of': 31}, {'control': 620, 'subledger': 250, 'difference': 370, 'credit_balances': [], 'open_customers': 1}], ['control 5', {'control': [[31, 250], [5, -80], [1, 400], [32, 400], [1, 400]], 'sub': {'crux': [[5, 250], [1, 400]]}, 'as_of': 31}, {'control': 970, 'subledger': 650, 'difference': 320, 'credit_balances': [], 'open_customers': 1}], ['control 6', {'control': [[1, 100], [11, 100], [1, 100], [9, 400], [9, 100], [9, 100]], 'sub': {'dyna': [[1, 100], [5, 100]]}, 'as_of': 10}, {'control': 800, 'subledger': 200, 'difference': 600, 'credit_balances': [], 'open_customers': 1}]], [['regression: credit balances in total', {'control': [[5, 100], [5, 250], [19, -80], [21, 250], [21, 250]], 'sub': {'crux': [[20, -80], [20, -80]], 'acme': [[21, 100]]}, 'as_of': 20}, {'control': 270, 'subledger': -160, 'difference': 430, 'credit_balances': ['crux'], 'open_customers': 1}], ['control 1', {'control': [[1, -80], [5, 100], [20, 400], [19, 250], [1, 250]], 'sub': {'crux': [[20, 250], [20, -80], [20, 250]]}, 'as_of': 20}, {'control': 920, 'subledger': 420, 'difference': 500, 'credit_balances': [], 'open_customers': 1}], ['control 2', {'control': [[9, -80], [11, 400]], 'sub': {'bolt': [[5, 100], [5, 250]], 'acme': [[11, 100], [11, -80], [10, 250]]}, 'as_of': 10}, {'control': -80, 'subledger': 600, 'difference': -680, 'credit_balances': [], 'open_customers': 2}], ['control 3', {'control': [[1, 100], [1, 250], [5, 250], [1, 100], [1, 400], [5, -80]], 'sub': {'bolt': [[10, -80], [10, 400]], 'crux': [[1, 250], [10, 250]]}, 'as_of': 10}, {'control': 1020, 'subledger': 820, 'difference': 200, 'credit_balances': [], 'open_customers': 2}], ['control 4', {'control': [[32, 250], [1, 100], [32, 100], [31, 100], [1, -80]], 'sub': {'dyna': [[5, 100]], 'acme': [[5, 400], [5, -80]], 'crux': [[32, -80]], 'bolt': [[32, -80]]}, 'as_of': 31}, {'control': 120, 'subledger': 420, 'difference': -300, 'credit_balances': [], 'open_customers': 2}], ['control 5', {'control': [[5, 400], [11, 100]], 'sub': {'bolt': [[5, 400], [1, -80]], 'acme': [[5, 400], [1, 100], [5, -300]], 'dyna': [[1, 100]]}, 'as_of': 10}, {'control': 400, 'subledger': 620, 'difference': -220, 'credit_balances': [], 'open_customers': 3}], ['control 6', {'control': [[31, 400], [32, 400]], 'sub': {'acme': [[1, -80], [31, 400]]}, 'as_of': 31}, {'control': 400, 'subledger': 320, 'difference': 80, 'credit_balances': [], 'open_customers': 1}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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:
Customer overpayments create a phantom reconciling difference.

x = {'control': [[day, amount]], 'sub': {customer: [[day, amount]]}, 'as_of': day}. Only rows dated on or before as_of count. The subledger total nets every customer balance, including credit (negative) balances. Return {'control', 'subledger', 'difference': control - subledger, 'credit_balances': customers with negative balance sorted by name, 'open_customers': count of nonzero balances}.

split: open-access