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

N = 1
observations = []
def solve(x):
    now = x['now']
    cand = [t for t in x['trains'] if t['eta'] - now <= x['lookahead_s']]
    cand.sort(key=lambda t: (t['eta'], t['id']))
    pairs = set()
    for a, b in x['conflicts']:
        pairs.add((a, b))
        pairs.add((b, a))
    granted = []
    routes = []
    for t in cand:
        if any(t['route'] == r or (t['route'], r) in pairs for r in routes):
            continue
        granted.append(t['id'])
        routes.append(t['route'])
    return granted
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('sampled regression 4', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R3'}, {'id': '8A01', 'cls': 1, 'sched': 100, 'eta': 80, 'route': 'R5'}, {'id': '7A02', 'cls': 3, 'sched': 100, 'eta': 80, 'route': 'R2'}], 'conflicts': [['R1', 'R5'], ['R1', 'R2'], ['R3', 'R2'], ['R4', 'R5']]}, ['8A01', '2A00']), ('sampled regression 3', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '3A00', 'cls': 1, 'sched': 150, 'eta': 160, 'route': 'R1'}, {'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 160, 'route': 'R4'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 100, 'route': 'R4'}, {'id': '1A03', 'cls': 3, 'sched': 150, 'eta': 80, 'route': 'R1'}, {'id': '9A04', 'cls': 3, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '7A05', 'cls': 3, 'sched': 120, 'eta': 220, 'route': 'R3'}], 'conflicts': [['R2', 'R1'], ['R1', 'R5'], ['R3', 'R4']]}, ['6A02', '3A00']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('boundary: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('control 1', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '7A01', 'cls': 1, 'sched': 120, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R4', 'R3'], ['R2', 'R3'], ['R4', 'R5'], ['R2', 'R1']]}, []), ('control 7', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R5'}, {'id': '9A01', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R1'}], 'conflicts': [['R3', 'R2']]}, ['9A01']), ('sampled regression 10', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '1A00', 'cls': 1, 'sched': 200, 'eta': 220, 'route': 'R1'}, {'id': '6A01', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R5'}, {'id': '7A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R2'}, {'id': '4A03', 'cls': 3, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '1A04', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '8A05', 'cls': 3, 'sched': 150, 'eta': 400, 'route': 'R4'}], 'conflicts': [['R2', 'R1'], ['R3', 'R4'], ['R5', 'R4']]}, ['1A04', '1A00'])], [('regression: late running train keeps schedule order', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 150, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 105, 'route': 'R2'}]}, ['1A01']), ('regression: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('sampled regression 15', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 200, 'eta': 180, 'route': 'R5'}, {'id': '6A01', 'cls': 3, 'sched': 150, 'eta': 80, 'route': 'R2'}, {'id': '7A02', 'cls': 3, 'sched': 100, 'eta': 130, 'route': 'R5'}, {'id': '5A03', 'cls': 3, 'sched': 120, 'eta': 160, 'route': 'R1'}], 'conflicts': [['R2', 'R1'], ['R4', 'R5'], ['R1', 'R5']]}, ['7A02', '6A01']), ('sampled regression 18', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '8A01', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R3'}], 'conflicts': [['R5', 'R4'], ['R1', 'R2'], ['R4', 'R3']]}, ['9A00']), ('boundary: conflict listed in reverse order', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R2'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R1'}]}, ['1A01']), ('control 12', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '8A00', 'cls': 1, 'sched': 100, 'eta': 180, 'route': 'R3'}, {'id': '8A01', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R3', 'R4']]}, ['8A00', '8A01']), ('control 21', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '8A00', 'cls': 3, 'sched': 150, 'eta': 180, 'route': 'R2'}, {'id': '7A01', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R5'}, {'id': '3A02', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R2'}], 'conflicts': [['R5', 'R1'], ['R4', 'R3'], ['R5', 'R4'], ['R1', 'R2']]}, ['7A01', '3A02']), ('sampled regression 24', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '5A00', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R4'}, {'id': '4A01', 'cls': 3, 'sched': 200, 'eta': 400, 'route': 'R5'}, {'id': '8A02', 'cls': 2, 'sched': 100, 'eta': 180, 'route': 'R2'}, {'id': '3A03', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R5'}], 'conflicts': [['R2', 'R3'], ['R1', 'R5'], ['R1', 'R2']]}, ['8A02', '5A00'])], [('regression: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('sampled regression 25', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 1, 'sched': 150, 'eta': 160, 'route': 'R1'}, {'id': '7A01', 'cls': 1, 'sched': 150, 'eta': 80, 'route': 'R2'}, {'id': '2A02', 'cls': 1, 'sched': 150, 'eta': 160, 'route': 'R2'}, {'id': '7A03', 'cls': 1, 'sched': 200, 'eta': 400, 'route': 'R2'}, {'id': '1A04', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R2', 'R3'], ['R4', 'R3']]}, ['2A00', '2A02']), ('sampled regression 33', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '7A00', 'cls': 1, 'sched': 200, 'eta': 130, 'route': 'R2'}, {'id': '1A01', 'cls': 2, 'sched': 120, 'eta': 400, 'route': 'R3'}, {'id': '9A02', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R2', 'R3'], ['R4', 'R3'], ['R4', 'R5']]}, ['7A00']), ('boundary: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('boundary: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('sampled regression 23', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 200, 'eta': 180, 'route': 'R4'}, {'id': '1A01', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R2'}, {'id': '5A02', 'cls': 2, 'sched': 120, 'eta': 160, 'route': 'R5'}, {'id': '2A03', 'cls': 2, 'sched': 150, 'eta': 80, 'route': 'R2'}], 'conflicts': [['R1', 'R5'], ['R2', 'R1']]}, ['9A00', '5A02', '2A03']), ('sampled regression 26', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R2'}, {'id': '7A01', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '2A02', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R2'}, {'id': '1A03', 'cls': 3, 'sched': 150, 'eta': 130, 'route': 'R1'}, {'id': '7A04', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R1'}, {'id': '1A05', 'cls': 2, 'sched': 150, 'eta': 400, 'route': 'R3'}], 'conflicts': [['R1', 'R5']]}, ['9A00', '7A04']), ('control 29', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '6A00', 'cls': 2, 'sched': 200, 'eta': 80, 'route': 'R5'}, {'id': '8A01', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R1'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '2A03', 'cls': 1, 'sched': 200, 'eta': 400, 'route': 'R3'}], 'conflicts': [['R5', 'R4'], ['R4', 'R3'], ['R2', 'R1'], ['R3', 'R2']]}, ['6A00'])], [('regression: late running train keeps schedule order', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 150, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 105, 'route': 'R2'}]}, ['1A01']), ('regression: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('sampled regression 35', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R3'}, {'id': '1A01', 'cls': 2, 'sched': 100, 'eta': 100, 'route': 'R4'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 160, 'route': 'R3'}, {'id': '8A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R3'}, {'id': '8A04', 'cls': 2, 'sched': 120, 'eta': 400, 'route': 'R4'}], 'conflicts': [['R1', 'R2'], ['R2', 'R3'], ['R1', 'R5']]}, ['1A02', '1A01']), ('sampled regression 40', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R4'}, {'id': '2A01', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R4'}, {'id': '8A02', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R3'}, {'id': '1A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R3'}, {'id': '8A04', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A05', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R1', 'R5'], ['R2', 'R3'], ['R1', 'R2'], ['R3', 'R4']]}, ['8A02', '3A05']), ('boundary: conflict with an earlier grant', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R5']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 110, 'eta': 120, 'route': 'R4'}, {'id': '1A03', 'cls': 1, 'sched': 130, 'eta': 130, 'route': 'R5'}]}, ['1A01', '1A02']), ('sampled regression 34', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 150, 'eta': 160, 'route': 'R5'}, {'id': '6A01', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R4', 'R3']]}, ['2A00']), ('control 37', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '4A00', 'cls': 3, 'sched': 120, 'eta': 400, 'route': 'R4'}, {'id': '1A01', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R4'}, {'id': '6A02', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A03', 'cls': 2, 'sched': 150, 'eta': 220, 'route': 'R5'}, {'id': '1A04', 'cls': 3, 'sched': 120, 'eta': 220, 'route': 'R2'}, {'id': '6A05', 'cls': 3, 'sched': 120, 'eta': 180, 'route': 'R1'}], 'conflicts': [['R1', 'R5'], ['R2', 'R1'], ['R3', 'R4']]}, ['1A01']), ('control 43', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '7A00', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R5'}, {'id': '3A01', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R2'}], 'conflicts': [['R3', 'R2'], ['R5', 'R4']]}, ['7A00'])], [('regression: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('sampled regression 45', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 3, 'sched': 150, 'eta': 180, 'route': 'R1'}, {'id': '2A01', 'cls': 3, 'sched': 120, 'eta': 80, 'route': 'R1'}, {'id': '2A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '5A03', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R2'}, {'id': '9A04', 'cls': 3, 'sched': 200, 'eta': 130, 'route': 'R1'}, {'id': '5A05', 'cls': 1, 'sched': 120, 'eta': 130, 'route': 'R1'}], 'conflicts': [['R5', 'R4'], ['R3', 'R2'], ['R1', 'R5'], ['R3', 'R4']]}, ['5A03', '5A05']), ('sampled regression 58', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '8A00', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R5'}, {'id': '9A01', 'cls': 3, 'sched': 120, 'eta': 80, 'route': 'R4'}, {'id': '7A02', 'cls': 3, 'sched': 200, 'eta': 180, 'route': 'R1'}, {'id': '8A03', 'cls': 1, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '7A04', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R5'}, {'id': '6A05', 'cls': 3, 'sched': 200, 'eta': 180, 'route': 'R1'}], 'conflicts': [['R4', 'R3'], ['R1', 'R2']]}, ['8A03', '9A01', '6A05']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('boundary: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('sampled regression 48', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '5A00', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R1'}, {'id': '2A01', 'cls': 3, 'sched': 100, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R4', 'R5'], ['R3', 'R4'], ['R1', 'R2']]}, ['5A00', '2A01']), ('control 51', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '5A00', 'cls': 3, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '6A01', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R2'}, {'id': '3A02', 'cls': 2, 'sched': 120, 'eta': 160, 'route': 'R4'}, {'id': '6A03', 'cls': 2, 'sched': 100, 'eta': 400, 'route': 'R2'}], 'conflicts': [['R3', 'R2'], ['R4', 'R5']]}, ['3A02']), ('sampled regression 54', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '7A00', 'cls': 3, 'sched': 150, 'eta': 100, 'route': 'R2'}, {'id': '5A01', 'cls': 3, 'sched': 150, 'eta': 400, 'route': 'R3'}, {'id': '9A02', 'cls': 1, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '7A03', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R3'}, {'id': '4A04', 'cls': 3, 'sched': 120, 'eta': 180, 'route': 'R3'}], 'conflicts': [['R3', 'R2'], ['R1', 'R5'], ['R4', 'R3']]}, ['9A02', '7A00'])]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    now = x['now']
    cand = [t for t in x['trains'] if t['eta'] - now <= x['lookahead_s']]
    cand.sort(key=lambda t: (t['sched'], t['cls'], t['id']))
    pairs = set()
    for a, b in x['conflicts']:
        pairs.add((a, b))
        pairs.add((b, a))
    granted = []
    routes = []
    for t in cand:
        if any(t['route'] == r or (t['route'], r) in pairs for r in routes):
            continue
        granted.append(t['id'])
        routes.append(t['route'])
    return granted
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('sampled regression 4', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R3'}, {'id': '8A01', 'cls': 1, 'sched': 100, 'eta': 80, 'route': 'R5'}, {'id': '7A02', 'cls': 3, 'sched': 100, 'eta': 80, 'route': 'R2'}], 'conflicts': [['R1', 'R5'], ['R1', 'R2'], ['R3', 'R2'], ['R4', 'R5']]}, ['8A01', '2A00']), ('sampled regression 3', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '3A00', 'cls': 1, 'sched': 150, 'eta': 160, 'route': 'R1'}, {'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 160, 'route': 'R4'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 100, 'route': 'R4'}, {'id': '1A03', 'cls': 3, 'sched': 150, 'eta': 80, 'route': 'R1'}, {'id': '9A04', 'cls': 3, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '7A05', 'cls': 3, 'sched': 120, 'eta': 220, 'route': 'R3'}], 'conflicts': [['R2', 'R1'], ['R1', 'R5'], ['R3', 'R4']]}, ['6A02', '3A00']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('boundary: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('control 1', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '7A01', 'cls': 1, 'sched': 120, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R4', 'R3'], ['R2', 'R3'], ['R4', 'R5'], ['R2', 'R1']]}, []), ('control 7', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R5'}, {'id': '9A01', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R1'}], 'conflicts': [['R3', 'R2']]}, ['9A01']), ('sampled regression 10', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '1A00', 'cls': 1, 'sched': 200, 'eta': 220, 'route': 'R1'}, {'id': '6A01', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R5'}, {'id': '7A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R2'}, {'id': '4A03', 'cls': 3, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '1A04', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '8A05', 'cls': 3, 'sched': 150, 'eta': 400, 'route': 'R4'}], 'conflicts': [['R2', 'R1'], ['R3', 'R4'], ['R5', 'R4']]}, ['1A04', '1A00'])], [('regression: late running train keeps schedule order', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 150, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 105, 'route': 'R2'}]}, ['1A01']), ('regression: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('sampled regression 15', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 200, 'eta': 180, 'route': 'R5'}, {'id': '6A01', 'cls': 3, 'sched': 150, 'eta': 80, 'route': 'R2'}, {'id': '7A02', 'cls': 3, 'sched': 100, 'eta': 130, 'route': 'R5'}, {'id': '5A03', 'cls': 3, 'sched': 120, 'eta': 160, 'route': 'R1'}], 'conflicts': [['R2', 'R1'], ['R4', 'R5'], ['R1', 'R5']]}, ['7A02', '6A01']), ('sampled regression 18', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '8A01', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R3'}], 'conflicts': [['R5', 'R4'], ['R1', 'R2'], ['R4', 'R3']]}, ['9A00']), ('boundary: conflict listed in reverse order', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R2'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R1'}]}, ['1A01']), ('control 12', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '8A00', 'cls': 1, 'sched': 100, 'eta': 180, 'route': 'R3'}, {'id': '8A01', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R3', 'R4']]}, ['8A00', '8A01']), ('control 21', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '8A00', 'cls': 3, 'sched': 150, 'eta': 180, 'route': 'R2'}, {'id': '7A01', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R5'}, {'id': '3A02', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R2'}], 'conflicts': [['R5', 'R1'], ['R4', 'R3'], ['R5', 'R4'], ['R1', 'R2']]}, ['7A01', '3A02']), ('sampled regression 24', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '5A00', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R4'}, {'id': '4A01', 'cls': 3, 'sched': 200, 'eta': 400, 'route': 'R5'}, {'id': '8A02', 'cls': 2, 'sched': 100, 'eta': 180, 'route': 'R2'}, {'id': '3A03', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R5'}], 'conflicts': [['R2', 'R3'], ['R1', 'R5'], ['R1', 'R2']]}, ['8A02', '5A00'])], [('regression: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('sampled regression 25', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 1, 'sched': 150, 'eta': 160, 'route': 'R1'}, {'id': '7A01', 'cls': 1, 'sched': 150, 'eta': 80, 'route': 'R2'}, {'id': '2A02', 'cls': 1, 'sched': 150, 'eta': 160, 'route': 'R2'}, {'id': '7A03', 'cls': 1, 'sched': 200, 'eta': 400, 'route': 'R2'}, {'id': '1A04', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R2', 'R3'], ['R4', 'R3']]}, ['2A00', '2A02']), ('sampled regression 33', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '7A00', 'cls': 1, 'sched': 200, 'eta': 130, 'route': 'R2'}, {'id': '1A01', 'cls': 2, 'sched': 120, 'eta': 400, 'route': 'R3'}, {'id': '9A02', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R2', 'R3'], ['R4', 'R3'], ['R4', 'R5']]}, ['7A00']), ('boundary: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('boundary: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('sampled regression 23', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 200, 'eta': 180, 'route': 'R4'}, {'id': '1A01', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R2'}, {'id': '5A02', 'cls': 2, 'sched': 120, 'eta': 160, 'route': 'R5'}, {'id': '2A03', 'cls': 2, 'sched': 150, 'eta': 80, 'route': 'R2'}], 'conflicts': [['R1', 'R5'], ['R2', 'R1']]}, ['9A00', '5A02', '2A03']), ('sampled regression 26', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R2'}, {'id': '7A01', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '2A02', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R2'}, {'id': '1A03', 'cls': 3, 'sched': 150, 'eta': 130, 'route': 'R1'}, {'id': '7A04', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R1'}, {'id': '1A05', 'cls': 2, 'sched': 150, 'eta': 400, 'route': 'R3'}], 'conflicts': [['R1', 'R5']]}, ['9A00', '7A04']), ('control 29', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '6A00', 'cls': 2, 'sched': 200, 'eta': 80, 'route': 'R5'}, {'id': '8A01', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R1'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '2A03', 'cls': 1, 'sched': 200, 'eta': 400, 'route': 'R3'}], 'conflicts': [['R5', 'R4'], ['R4', 'R3'], ['R2', 'R1'], ['R3', 'R2']]}, ['6A00'])], [('regression: late running train keeps schedule order', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 150, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 105, 'route': 'R2'}]}, ['1A01']), ('regression: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('sampled regression 35', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R3'}, {'id': '1A01', 'cls': 2, 'sched': 100, 'eta': 100, 'route': 'R4'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 160, 'route': 'R3'}, {'id': '8A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R3'}, {'id': '8A04', 'cls': 2, 'sched': 120, 'eta': 400, 'route': 'R4'}], 'conflicts': [['R1', 'R2'], ['R2', 'R3'], ['R1', 'R5']]}, ['1A02', '1A01']), ('sampled regression 40', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R4'}, {'id': '2A01', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R4'}, {'id': '8A02', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R3'}, {'id': '1A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R3'}, {'id': '8A04', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A05', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R1', 'R5'], ['R2', 'R3'], ['R1', 'R2'], ['R3', 'R4']]}, ['8A02', '3A05']), ('boundary: conflict with an earlier grant', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R5']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 110, 'eta': 120, 'route': 'R4'}, {'id': '1A03', 'cls': 1, 'sched': 130, 'eta': 130, 'route': 'R5'}]}, ['1A01', '1A02']), ('sampled regression 34', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 150, 'eta': 160, 'route': 'R5'}, {'id': '6A01', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R4', 'R3']]}, ['2A00']), ('control 37', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '4A00', 'cls': 3, 'sched': 120, 'eta': 400, 'route': 'R4'}, {'id': '1A01', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R4'}, {'id': '6A02', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A03', 'cls': 2, 'sched': 150, 'eta': 220, 'route': 'R5'}, {'id': '1A04', 'cls': 3, 'sched': 120, 'eta': 220, 'route': 'R2'}, {'id': '6A05', 'cls': 3, 'sched': 120, 'eta': 180, 'route': 'R1'}], 'conflicts': [['R1', 'R5'], ['R2', 'R1'], ['R3', 'R4']]}, ['1A01']), ('control 43', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '7A00', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R5'}, {'id': '3A01', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R2'}], 'conflicts': [['R3', 'R2'], ['R5', 'R4']]}, ['7A00'])], [('regression: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('sampled regression 45', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 3, 'sched': 150, 'eta': 180, 'route': 'R1'}, {'id': '2A01', 'cls': 3, 'sched': 120, 'eta': 80, 'route': 'R1'}, {'id': '2A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '5A03', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R2'}, {'id': '9A04', 'cls': 3, 'sched': 200, 'eta': 130, 'route': 'R1'}, {'id': '5A05', 'cls': 1, 'sched': 120, 'eta': 130, 'route': 'R1'}], 'conflicts': [['R5', 'R4'], ['R3', 'R2'], ['R1', 'R5'], ['R3', 'R4']]}, ['5A03', '5A05']), ('sampled regression 58', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '8A00', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R5'}, {'id': '9A01', 'cls': 3, 'sched': 120, 'eta': 80, 'route': 'R4'}, {'id': '7A02', 'cls': 3, 'sched': 200, 'eta': 180, 'route': 'R1'}, {'id': '8A03', 'cls': 1, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '7A04', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R5'}, {'id': '6A05', 'cls': 3, 'sched': 200, 'eta': 180, 'route': 'R1'}], 'conflicts': [['R4', 'R3'], ['R1', 'R2']]}, ['8A03', '9A01', '6A05']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('boundary: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('sampled regression 48', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '5A00', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R1'}, {'id': '2A01', 'cls': 3, 'sched': 100, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R4', 'R5'], ['R3', 'R4'], ['R1', 'R2']]}, ['5A00', '2A01']), ('control 51', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '5A00', 'cls': 3, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '6A01', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R2'}, {'id': '3A02', 'cls': 2, 'sched': 120, 'eta': 160, 'route': 'R4'}, {'id': '6A03', 'cls': 2, 'sched': 100, 'eta': 400, 'route': 'R2'}], 'conflicts': [['R3', 'R2'], ['R4', 'R5']]}, ['3A02']), ('sampled regression 54', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '7A00', 'cls': 3, 'sched': 150, 'eta': 100, 'route': 'R2'}, {'id': '5A01', 'cls': 3, 'sched': 150, 'eta': 400, 'route': 'R3'}, {'id': '9A02', 'cls': 1, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '7A03', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R3'}, {'id': '4A04', 'cls': 3, 'sched': 120, 'eta': 180, 'route': 'R3'}], 'conflicts': [['R3', 'R2'], ['R1', 'R5'], ['R4', 'R3']]}, ['9A02', '7A00'])]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A lower-class train wins a conflicting route over a higher-class train.

Trains whose eta is at most lookahead_s after now are candidates, including overdue trains (eta before now). Candidates are ordered by class (1 highest), then scheduled time, then id. Routes are granted greedily in that order; a candidate is skipped if its route equals or conflicts with (conflict pairs are unordered) any route already granted. Return granted train ids in grant order.

split: open-access


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

N = 1
observations = []
def solve(x):
    now = x['now']
    cand = [t for t in x['trains'] if t['eta'] - now <= x['lookahead_s']]
    cand.sort(key=lambda t: (t['cls'], t['sched'], t['id']))
    pairs = set()
    for a, b in x['conflicts']:
        pairs.add((a, b))
    granted = []
    routes = []
    for t in cand:
        if any(t['route'] == r or (t['route'], r) in pairs for r in routes):
            continue
        granted.append(t['id'])
        routes.append(t['route'])
    return granted
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('sampled regression 4', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R3'}, {'id': '8A01', 'cls': 1, 'sched': 100, 'eta': 80, 'route': 'R5'}, {'id': '7A02', 'cls': 3, 'sched': 100, 'eta': 80, 'route': 'R2'}], 'conflicts': [['R1', 'R5'], ['R1', 'R2'], ['R3', 'R2'], ['R4', 'R5']]}, ['8A01', '2A00']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('boundary: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('boundary: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('control 1', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '7A01', 'cls': 1, 'sched': 120, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R4', 'R3'], ['R2', 'R3'], ['R4', 'R5'], ['R2', 'R1']]}, []), ('control 7', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R5'}, {'id': '9A01', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R1'}], 'conflicts': [['R3', 'R2']]}, ['9A01']), ('control 10', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '1A00', 'cls': 1, 'sched': 200, 'eta': 220, 'route': 'R1'}, {'id': '6A01', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R5'}, {'id': '7A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R2'}, {'id': '4A03', 'cls': 3, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '1A04', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '8A05', 'cls': 3, 'sched': 150, 'eta': 400, 'route': 'R4'}], 'conflicts': [['R2', 'R1'], ['R3', 'R4'], ['R5', 'R4']]}, ['1A04', '1A00'])], [('regression: conflict with an earlier grant', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R5']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 110, 'eta': 120, 'route': 'R4'}, {'id': '1A03', 'cls': 1, 'sched': 130, 'eta': 130, 'route': 'R5'}]}, ['1A01', '1A02']), ('regression: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('sampled regression 40', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R4'}, {'id': '2A01', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R4'}, {'id': '8A02', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R3'}, {'id': '1A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R3'}, {'id': '8A04', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A05', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R1', 'R5'], ['R2', 'R3'], ['R1', 'R2'], ['R3', 'R4']]}, ['8A02', '3A05']), ('sampled regression 63', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '7A00', 'cls': 3, 'sched': 100, 'eta': 80, 'route': 'R3'}, {'id': '3A01', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R4'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '3A03', 'cls': 2, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '2A04', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R1'}, {'id': '2A05', 'cls': 2, 'sched': 150, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R1', 'R2'], ['R5', 'R1'], ['R2', 'R3'], ['R4', 'R3']]}, ['6A02', '2A05', '3A01']), ('boundary: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('control 12', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '8A00', 'cls': 1, 'sched': 100, 'eta': 180, 'route': 'R3'}, {'id': '8A01', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R3', 'R4']]}, ['8A00', '8A01']), ('control 15', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 200, 'eta': 180, 'route': 'R5'}, {'id': '6A01', 'cls': 3, 'sched': 150, 'eta': 80, 'route': 'R2'}, {'id': '7A02', 'cls': 3, 'sched': 100, 'eta': 130, 'route': 'R5'}, {'id': '5A03', 'cls': 3, 'sched': 120, 'eta': 160, 'route': 'R1'}], 'conflicts': [['R2', 'R1'], ['R4', 'R5'], ['R1', 'R5']]}, ['7A02', '6A01']), ('sampled regression 18', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '8A01', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R3'}], 'conflicts': [['R5', 'R4'], ['R1', 'R2'], ['R4', 'R3']]}, ['9A00'])], [('regression: late running train keeps schedule order', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 150, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 105, 'route': 'R2'}]}, ['1A01']), ('regression: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('sampled regression 14', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '3A00', 'cls': 2, 'sched': 200, 'eta': 180, 'route': 'R2'}, {'id': '5A01', 'cls': 3, 'sched': 100, 'eta': 100, 'route': 'R4'}, {'id': '1A02', 'cls': 2, 'sched': 150, 'eta': 180, 'route': 'R3'}, {'id': '6A03', 'cls': 3, 'sched': 200, 'eta': 400, 'route': 'R1'}, {'id': '3A04', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R1', 'R2'], ['R2', 'R3'], ['R5', 'R4']]}, ['3A04']), ('boundary: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('regression: conflict with an earlier grant', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R5']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 110, 'eta': 120, 'route': 'R4'}, {'id': '1A03', 'cls': 1, 'sched': 130, 'eta': 130, 'route': 'R5'}]}, ['1A01', '1A02']), ('control 23', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 200, 'eta': 180, 'route': 'R4'}, {'id': '1A01', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R2'}, {'id': '5A02', 'cls': 2, 'sched': 120, 'eta': 160, 'route': 'R5'}, {'id': '2A03', 'cls': 2, 'sched': 150, 'eta': 80, 'route': 'R2'}], 'conflicts': [['R1', 'R5'], ['R2', 'R1']]}, ['9A00', '5A02', '2A03']), ('control 26', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R2'}, {'id': '7A01', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '2A02', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R2'}, {'id': '1A03', 'cls': 3, 'sched': 150, 'eta': 130, 'route': 'R1'}, {'id': '7A04', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R1'}, {'id': '1A05', 'cls': 2, 'sched': 150, 'eta': 400, 'route': 'R3'}], 'conflicts': [['R1', 'R5']]}, ['9A00', '7A04']), ('control 29', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '6A00', 'cls': 2, 'sched': 200, 'eta': 80, 'route': 'R5'}, {'id': '8A01', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R1'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '2A03', 'cls': 1, 'sched': 200, 'eta': 400, 'route': 'R3'}], 'conflicts': [['R5', 'R4'], ['R4', 'R3'], ['R2', 'R1'], ['R3', 'R2']]}, ['6A00'])], [('regression: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('sampled regression 33', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '7A00', 'cls': 1, 'sched': 200, 'eta': 130, 'route': 'R2'}, {'id': '1A01', 'cls': 2, 'sched': 120, 'eta': 400, 'route': 'R3'}, {'id': '9A02', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R2', 'R3'], ['R4', 'R3'], ['R4', 'R5']]}, ['7A00']), ('regression: conflict with an earlier grant', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R5']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 110, 'eta': 120, 'route': 'R4'}, {'id': '1A03', 'cls': 1, 'sched': 130, 'eta': 130, 'route': 'R5'}]}, ['1A01', '1A02']), ('regression: late running train keeps schedule order', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 150, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 105, 'route': 'R2'}]}, ['1A01']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('control 34', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 150, 'eta': 160, 'route': 'R5'}, {'id': '6A01', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R4', 'R3']]}, ['2A00']), ('control 37', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '4A00', 'cls': 3, 'sched': 120, 'eta': 400, 'route': 'R4'}, {'id': '1A01', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R4'}, {'id': '6A02', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A03', 'cls': 2, 'sched': 150, 'eta': 220, 'route': 'R5'}, {'id': '1A04', 'cls': 3, 'sched': 120, 'eta': 220, 'route': 'R2'}, {'id': '6A05', 'cls': 3, 'sched': 120, 'eta': 180, 'route': 'R1'}], 'conflicts': [['R1', 'R5'], ['R2', 'R1'], ['R3', 'R4']]}, ['1A01']), ('sampled regression 40', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R4'}, {'id': '2A01', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R4'}, {'id': '8A02', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R3'}, {'id': '1A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R3'}, {'id': '8A04', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A05', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R1', 'R5'], ['R2', 'R3'], ['R1', 'R2'], ['R3', 'R4']]}, ['8A02', '3A05'])], [('regression: conflict with an earlier grant', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R5']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 110, 'eta': 120, 'route': 'R4'}, {'id': '1A03', 'cls': 1, 'sched': 130, 'eta': 130, 'route': 'R5'}]}, ['1A01', '1A02']), ('regression: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('sampled regression 50', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 3, 'sched': 100, 'eta': 130, 'route': 'R5'}, {'id': '4A01', 'cls': 3, 'sched': 200, 'eta': 180, 'route': 'R4'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R3'}, {'id': '4A03', 'cls': 3, 'sched': 100, 'eta': 180, 'route': 'R3'}, {'id': '7A04', 'cls': 3, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '2A05', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R4'}], 'conflicts': [['R1', 'R2'], ['R1', 'R5'], ['R4', 'R3']]}, ['2A05', '9A00']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('boundary: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('control 45', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 3, 'sched': 150, 'eta': 180, 'route': 'R1'}, {'id': '2A01', 'cls': 3, 'sched': 120, 'eta': 80, 'route': 'R1'}, {'id': '2A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '5A03', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R2'}, {'id': '9A04', 'cls': 3, 'sched': 200, 'eta': 130, 'route': 'R1'}, {'id': '5A05', 'cls': 1, 'sched': 120, 'eta': 130, 'route': 'R1'}], 'conflicts': [['R5', 'R4'], ['R3', 'R2'], ['R1', 'R5'], ['R3', 'R4']]}, ['5A03', '5A05']), ('control 48', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '5A00', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R1'}, {'id': '2A01', 'cls': 3, 'sched': 100, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R4', 'R5'], ['R3', 'R4'], ['R1', 'R2']]}, ['5A00', '2A01']), ('control 51', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '5A00', 'cls': 3, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '6A01', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R2'}, {'id': '3A02', 'cls': 2, 'sched': 120, 'eta': 160, 'route': 'R4'}, {'id': '6A03', 'cls': 2, 'sched': 100, 'eta': 400, 'route': 'R2'}], 'conflicts': [['R3', 'R2'], ['R4', 'R5']]}, ['3A02'])]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

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

N = 1
observations = []
def solve(x):
    now = x['now']
    cand = [t for t in x['trains'] if t['eta'] - now <= x['lookahead_s']]
    cand.sort(key=lambda t: (t['cls'], t['sched'], t['id']))
    pairs = set()
    for a, b in x['conflicts']:
        pairs.add((a, b))
        if a < b:
            pairs.add((b, a))
    granted = []
    routes = []
    for t in cand:
        if any(t['route'] == r or (t['route'], r) in pairs for r in routes):
            continue
        granted.append(t['id'])
        routes.append(t['route'])
    return granted
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('sampled regression 4', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R3'}, {'id': '8A01', 'cls': 1, 'sched': 100, 'eta': 80, 'route': 'R5'}, {'id': '7A02', 'cls': 3, 'sched': 100, 'eta': 80, 'route': 'R2'}], 'conflicts': [['R1', 'R5'], ['R1', 'R2'], ['R3', 'R2'], ['R4', 'R5']]}, ['8A01', '2A00']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('boundary: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('boundary: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('control 1', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '7A01', 'cls': 1, 'sched': 120, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R4', 'R3'], ['R2', 'R3'], ['R4', 'R5'], ['R2', 'R1']]}, []), ('control 7', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R5'}, {'id': '9A01', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R1'}], 'conflicts': [['R3', 'R2']]}, ['9A01']), ('control 10', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '1A00', 'cls': 1, 'sched': 200, 'eta': 220, 'route': 'R1'}, {'id': '6A01', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R5'}, {'id': '7A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R2'}, {'id': '4A03', 'cls': 3, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '1A04', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '8A05', 'cls': 3, 'sched': 150, 'eta': 400, 'route': 'R4'}], 'conflicts': [['R2', 'R1'], ['R3', 'R4'], ['R5', 'R4']]}, ['1A04', '1A00'])], [('regression: conflict with an earlier grant', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R5']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 110, 'eta': 120, 'route': 'R4'}, {'id': '1A03', 'cls': 1, 'sched': 130, 'eta': 130, 'route': 'R5'}]}, ['1A01', '1A02']), ('regression: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('sampled regression 40', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R4'}, {'id': '2A01', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R4'}, {'id': '8A02', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R3'}, {'id': '1A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R3'}, {'id': '8A04', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A05', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R1', 'R5'], ['R2', 'R3'], ['R1', 'R2'], ['R3', 'R4']]}, ['8A02', '3A05']), ('sampled regression 63', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '7A00', 'cls': 3, 'sched': 100, 'eta': 80, 'route': 'R3'}, {'id': '3A01', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R4'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '3A03', 'cls': 2, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '2A04', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R1'}, {'id': '2A05', 'cls': 2, 'sched': 150, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R1', 'R2'], ['R5', 'R1'], ['R2', 'R3'], ['R4', 'R3']]}, ['6A02', '2A05', '3A01']), ('boundary: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('control 12', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '8A00', 'cls': 1, 'sched': 100, 'eta': 180, 'route': 'R3'}, {'id': '8A01', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R3', 'R4']]}, ['8A00', '8A01']), ('control 15', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 200, 'eta': 180, 'route': 'R5'}, {'id': '6A01', 'cls': 3, 'sched': 150, 'eta': 80, 'route': 'R2'}, {'id': '7A02', 'cls': 3, 'sched': 100, 'eta': 130, 'route': 'R5'}, {'id': '5A03', 'cls': 3, 'sched': 120, 'eta': 160, 'route': 'R1'}], 'conflicts': [['R2', 'R1'], ['R4', 'R5'], ['R1', 'R5']]}, ['7A02', '6A01']), ('sampled regression 18', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '8A01', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R3'}], 'conflicts': [['R5', 'R4'], ['R1', 'R2'], ['R4', 'R3']]}, ['9A00'])], [('regression: late running train keeps schedule order', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 150, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 105, 'route': 'R2'}]}, ['1A01']), ('regression: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('sampled regression 14', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '3A00', 'cls': 2, 'sched': 200, 'eta': 180, 'route': 'R2'}, {'id': '5A01', 'cls': 3, 'sched': 100, 'eta': 100, 'route': 'R4'}, {'id': '1A02', 'cls': 2, 'sched': 150, 'eta': 180, 'route': 'R3'}, {'id': '6A03', 'cls': 3, 'sched': 200, 'eta': 400, 'route': 'R1'}, {'id': '3A04', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R1', 'R2'], ['R2', 'R3'], ['R5', 'R4']]}, ['3A04']), ('boundary: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('regression: conflict with an earlier grant', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R5']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 110, 'eta': 120, 'route': 'R4'}, {'id': '1A03', 'cls': 1, 'sched': 130, 'eta': 130, 'route': 'R5'}]}, ['1A01', '1A02']), ('control 23', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 200, 'eta': 180, 'route': 'R4'}, {'id': '1A01', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R2'}, {'id': '5A02', 'cls': 2, 'sched': 120, 'eta': 160, 'route': 'R5'}, {'id': '2A03', 'cls': 2, 'sched': 150, 'eta': 80, 'route': 'R2'}], 'conflicts': [['R1', 'R5'], ['R2', 'R1']]}, ['9A00', '5A02', '2A03']), ('control 26', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R2'}, {'id': '7A01', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '2A02', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R2'}, {'id': '1A03', 'cls': 3, 'sched': 150, 'eta': 130, 'route': 'R1'}, {'id': '7A04', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R1'}, {'id': '1A05', 'cls': 2, 'sched': 150, 'eta': 400, 'route': 'R3'}], 'conflicts': [['R1', 'R5']]}, ['9A00', '7A04']), ('control 29', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '6A00', 'cls': 2, 'sched': 200, 'eta': 80, 'route': 'R5'}, {'id': '8A01', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R1'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '2A03', 'cls': 1, 'sched': 200, 'eta': 400, 'route': 'R3'}], 'conflicts': [['R5', 'R4'], ['R4', 'R3'], ['R2', 'R1'], ['R3', 'R2']]}, ['6A00'])], [('regression: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('sampled regression 33', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '7A00', 'cls': 1, 'sched': 200, 'eta': 130, 'route': 'R2'}, {'id': '1A01', 'cls': 2, 'sched': 120, 'eta': 400, 'route': 'R3'}, {'id': '9A02', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R2', 'R3'], ['R4', 'R3'], ['R4', 'R5']]}, ['7A00']), ('regression: conflict with an earlier grant', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R5']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 110, 'eta': 120, 'route': 'R4'}, {'id': '1A03', 'cls': 1, 'sched': 130, 'eta': 130, 'route': 'R5'}]}, ['1A01', '1A02']), ('regression: late running train keeps schedule order', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 150, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 105, 'route': 'R2'}]}, ['1A01']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('control 34', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 150, 'eta': 160, 'route': 'R5'}, {'id': '6A01', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R4', 'R3']]}, ['2A00']), ('control 37', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '4A00', 'cls': 3, 'sched': 120, 'eta': 400, 'route': 'R4'}, {'id': '1A01', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R4'}, {'id': '6A02', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A03', 'cls': 2, 'sched': 150, 'eta': 220, 'route': 'R5'}, {'id': '1A04', 'cls': 3, 'sched': 120, 'eta': 220, 'route': 'R2'}, {'id': '6A05', 'cls': 3, 'sched': 120, 'eta': 180, 'route': 'R1'}], 'conflicts': [['R1', 'R5'], ['R2', 'R1'], ['R3', 'R4']]}, ['1A01']), ('sampled regression 40', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R4'}, {'id': '2A01', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R4'}, {'id': '8A02', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R3'}, {'id': '1A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R3'}, {'id': '8A04', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A05', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R1', 'R5'], ['R2', 'R3'], ['R1', 'R2'], ['R3', 'R4']]}, ['8A02', '3A05'])], [('regression: conflict with an earlier grant', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R5']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 110, 'eta': 120, 'route': 'R4'}, {'id': '1A03', 'cls': 1, 'sched': 130, 'eta': 130, 'route': 'R5'}]}, ['1A01', '1A02']), ('regression: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('sampled regression 50', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 3, 'sched': 100, 'eta': 130, 'route': 'R5'}, {'id': '4A01', 'cls': 3, 'sched': 200, 'eta': 180, 'route': 'R4'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R3'}, {'id': '4A03', 'cls': 3, 'sched': 100, 'eta': 180, 'route': 'R3'}, {'id': '7A04', 'cls': 3, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '2A05', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R4'}], 'conflicts': [['R1', 'R2'], ['R1', 'R5'], ['R4', 'R3']]}, ['2A05', '9A00']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('boundary: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('control 45', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 3, 'sched': 150, 'eta': 180, 'route': 'R1'}, {'id': '2A01', 'cls': 3, 'sched': 120, 'eta': 80, 'route': 'R1'}, {'id': '2A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '5A03', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R2'}, {'id': '9A04', 'cls': 3, 'sched': 200, 'eta': 130, 'route': 'R1'}, {'id': '5A05', 'cls': 1, 'sched': 120, 'eta': 130, 'route': 'R1'}], 'conflicts': [['R5', 'R4'], ['R3', 'R2'], ['R1', 'R5'], ['R3', 'R4']]}, ['5A03', '5A05']), ('control 48', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '5A00', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R1'}, {'id': '2A01', 'cls': 3, 'sched': 100, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R4', 'R5'], ['R3', 'R4'], ['R1', 'R2']]}, ['5A00', '2A01']), ('control 51', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '5A00', 'cls': 3, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '6A01', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R2'}, {'id': '3A02', 'cls': 2, 'sched': 120, 'eta': 160, 'route': 'R4'}, {'id': '6A03', 'cls': 2, 'sched': 100, 'eta': 400, 'route': 'R2'}], 'conflicts': [['R3', 'R2'], ['R4', 'R5']]}, ['3A02'])]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Two conflicting routes are both granted when the conflict is listed in the other order.

Trains whose eta is at most lookahead_s after now are candidates, including overdue trains (eta before now). Candidates are ordered by class (1 highest), then scheduled time, then id. Routes are granted greedily in that order; a candidate is skipped if its route equals or conflicts with (conflict pairs are unordered) any route already granted. Return granted train ids in grant order.

split: open-access


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

N = 1
observations = []
def solve(x):
    now = x['now']
    cand = [t for t in x['trains'] if t['eta'] - now <= x['lookahead_s']]
    cand.sort(key=lambda t: (t['cls'], t['sched'], t['id']))
    pairs = set()
    for a, b in x['conflicts']:
        pairs.add((a, b))
        pairs.add((b, a))
    granted = []
    routes = []
    for t in cand:
        if any((t['route'], r) in pairs for r in routes):
            continue
        granted.append(t['id'])
        routes.append(t['route'])
    return granted
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('sampled regression 3', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '3A00', 'cls': 1, 'sched': 150, 'eta': 160, 'route': 'R1'}, {'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 160, 'route': 'R4'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 100, 'route': 'R4'}, {'id': '1A03', 'cls': 3, 'sched': 150, 'eta': 80, 'route': 'R1'}, {'id': '9A04', 'cls': 3, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '7A05', 'cls': 3, 'sched': 120, 'eta': 220, 'route': 'R3'}], 'conflicts': [['R2', 'R1'], ['R1', 'R5'], ['R3', 'R4']]}, ['6A02', '3A00']), ('sampled regression 6', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '5A01', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R3'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 100, 'route': 'R3'}, {'id': '5A03', 'cls': 1, 'sched': 120, 'eta': 180, 'route': 'R5'}, {'id': '2A04', 'cls': 1, 'sched': 200, 'eta': 130, 'route': 'R4'}, {'id': '6A05', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R1'}], 'conflicts': [['R4', 'R3'], ['R2', 'R1'], ['R5', 'R1'], ['R3', 'R2']]}, ['5A03', '6A02']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('boundary: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('control 1', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '7A01', 'cls': 1, 'sched': 120, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R4', 'R3'], ['R2', 'R3'], ['R4', 'R5'], ['R2', 'R1']]}, []), ('control 4', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R3'}, {'id': '8A01', 'cls': 1, 'sched': 100, 'eta': 80, 'route': 'R5'}, {'id': '7A02', 'cls': 3, 'sched': 100, 'eta': 80, 'route': 'R2'}], 'conflicts': [['R1', 'R5'], ['R1', 'R2'], ['R3', 'R2'], ['R4', 'R5']]}, ['8A01', '2A00']), ('control 7', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R5'}, {'id': '9A01', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R1'}], 'conflicts': [['R3', 'R2']]}, ['9A01'])], [('regression: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('sampled regression 6', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '5A01', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R3'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 100, 'route': 'R3'}, {'id': '5A03', 'cls': 1, 'sched': 120, 'eta': 180, 'route': 'R5'}, {'id': '2A04', 'cls': 1, 'sched': 200, 'eta': 130, 'route': 'R4'}, {'id': '6A05', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R1'}], 'conflicts': [['R4', 'R3'], ['R2', 'R1'], ['R5', 'R1'], ['R3', 'R2']]}, ['5A03', '6A02']), ('sampled regression 22', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 150, 'eta': 80, 'route': 'R3'}, {'id': '5A01', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R3'}], 'conflicts': [['R1', 'R2'], ['R5', 'R1']]}, ['5A01']), ('sampled regression 38', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '8A00', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R4'}, {'id': '7A01', 'cls': 3, 'sched': 100, 'eta': 400, 'route': 'R5'}, {'id': '9A02', 'cls': 2, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '6A03', 'cls': 3, 'sched': 100, 'eta': 100, 'route': 'R2'}, {'id': '9A04', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R1'}, {'id': '4A05', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R1'}], 'conflicts': [['R5', 'R4'], ['R2', 'R1']]}, ['4A05', '8A00']), ('boundary: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('control 12', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '8A00', 'cls': 1, 'sched': 100, 'eta': 180, 'route': 'R3'}, {'id': '8A01', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R3', 'R4']]}, ['8A00', '8A01']), ('control 15', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 200, 'eta': 180, 'route': 'R5'}, {'id': '6A01', 'cls': 3, 'sched': 150, 'eta': 80, 'route': 'R2'}, {'id': '7A02', 'cls': 3, 'sched': 100, 'eta': 130, 'route': 'R5'}, {'id': '5A03', 'cls': 3, 'sched': 120, 'eta': 160, 'route': 'R1'}], 'conflicts': [['R2', 'R1'], ['R4', 'R5'], ['R1', 'R5']]}, ['7A02', '6A01']), ('control 18', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '8A01', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R3'}], 'conflicts': [['R5', 'R4'], ['R1', 'R2'], ['R4', 'R3']]}, ['9A00'])], [('regression: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('sampled regression 10', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '1A00', 'cls': 1, 'sched': 200, 'eta': 220, 'route': 'R1'}, {'id': '6A01', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R5'}, {'id': '7A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R2'}, {'id': '4A03', 'cls': 3, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '1A04', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '8A05', 'cls': 3, 'sched': 150, 'eta': 400, 'route': 'R4'}], 'conflicts': [['R2', 'R1'], ['R3', 'R4'], ['R5', 'R4']]}, ['1A04', '1A00']), ('sampled regression 40', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R4'}, {'id': '2A01', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R4'}, {'id': '8A02', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R3'}, {'id': '1A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R3'}, {'id': '8A04', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A05', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R1', 'R5'], ['R2', 'R3'], ['R1', 'R2'], ['R3', 'R4']]}, ['8A02', '3A05']), ('sampled regression 69', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '4A00', 'cls': 3, 'sched': 200, 'eta': 100, 'route': 'R1'}, {'id': '9A01', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R3'}, {'id': '4A02', 'cls': 3, 'sched': 200, 'eta': 130, 'route': 'R4'}, {'id': '9A03', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R4'}, {'id': '9A04', 'cls': 1, 'sched': 150, 'eta': 160, 'route': 'R3'}, {'id': '1A05', 'cls': 1, 'sched': 200, 'eta': 100, 'route': 'R2'}], 'conflicts': [['R3', 'R4'], ['R1', 'R5'], ['R2', 'R3'], ['R5', 'R4']]}, ['9A03', '1A05', '4A00']), ('boundary: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('control 23', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 200, 'eta': 180, 'route': 'R4'}, {'id': '1A01', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R2'}, {'id': '5A02', 'cls': 2, 'sched': 120, 'eta': 160, 'route': 'R5'}, {'id': '2A03', 'cls': 2, 'sched': 150, 'eta': 80, 'route': 'R2'}], 'conflicts': [['R1', 'R5'], ['R2', 'R1']]}, ['9A00', '5A02', '2A03']), ('sampled regression 26', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R2'}, {'id': '7A01', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '2A02', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R2'}, {'id': '1A03', 'cls': 3, 'sched': 150, 'eta': 130, 'route': 'R1'}, {'id': '7A04', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R1'}, {'id': '1A05', 'cls': 2, 'sched': 150, 'eta': 400, 'route': 'R3'}], 'conflicts': [['R1', 'R5']]}, ['9A00', '7A04']), ('control 29', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '6A00', 'cls': 2, 'sched': 200, 'eta': 80, 'route': 'R5'}, {'id': '8A01', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R1'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '2A03', 'cls': 1, 'sched': 200, 'eta': 400, 'route': 'R3'}], 'conflicts': [['R5', 'R4'], ['R4', 'R3'], ['R2', 'R1'], ['R3', 'R2']]}, ['6A00'])], [('regression: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('sampled regression 17', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R1'}, {'id': '9A01', 'cls': 3, 'sched': 200, 'eta': 160, 'route': 'R5'}, {'id': '4A02', 'cls': 3, 'sched': 200, 'eta': 160, 'route': 'R4'}, {'id': '3A03', 'cls': 3, 'sched': 100, 'eta': 100, 'route': 'R3'}, {'id': '1A04', 'cls': 1, 'sched': 200, 'eta': 400, 'route': 'R4'}, {'id': '4A05', 'cls': 3, 'sched': 150, 'eta': 130, 'route': 'R1'}], 'conflicts': [['R2', 'R3'], ['R5', 'R1'], ['R2', 'R1']]}, ['9A00', '3A03', '4A02']), ('sampled regression 59', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 150, 'eta': 220, 'route': 'R2'}, {'id': '3A01', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R3'}, {'id': '5A02', 'cls': 1, 'sched': 200, 'eta': 160, 'route': 'R3'}, {'id': '9A03', 'cls': 1, 'sched': 200, 'eta': 80, 'route': 'R3'}], 'conflicts': [['R2', 'R1']]}, ['5A02']), ('boundary: conflict with an earlier grant', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R5']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 110, 'eta': 120, 'route': 'R4'}, {'id': '1A03', 'cls': 1, 'sched': 130, 'eta': 130, 'route': 'R5'}]}, ['1A01', '1A02']), ('boundary: late running train keeps schedule order', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 150, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 105, 'route': 'R2'}]}, ['1A01']), ('sampled regression 34', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 150, 'eta': 160, 'route': 'R5'}, {'id': '6A01', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R4', 'R3']]}, ['2A00']), ('sampled regression 37', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '4A00', 'cls': 3, 'sched': 120, 'eta': 400, 'route': 'R4'}, {'id': '1A01', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R4'}, {'id': '6A02', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A03', 'cls': 2, 'sched': 150, 'eta': 220, 'route': 'R5'}, {'id': '1A04', 'cls': 3, 'sched': 120, 'eta': 220, 'route': 'R2'}, {'id': '6A05', 'cls': 3, 'sched': 120, 'eta': 180, 'route': 'R1'}], 'conflicts': [['R1', 'R5'], ['R2', 'R1'], ['R3', 'R4']]}, ['1A01']), ('sampled regression 40', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R4'}, {'id': '2A01', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R4'}, {'id': '8A02', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R3'}, {'id': '1A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R3'}, {'id': '8A04', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A05', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R1', 'R5'], ['R2', 'R3'], ['R1', 'R2'], ['R3', 'R4']]}, ['8A02', '3A05'])], [('regression: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('sampled regression 19', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 150, 'eta': 80, 'route': 'R3'}, {'id': '8A01', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R3'}, {'id': '7A02', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R1'}, {'id': '8A03', 'cls': 1, 'sched': 200, 'eta': 80, 'route': 'R5'}], 'conflicts': [['R1', 'R2'], ['R2', 'R3'], ['R4', 'R5'], ['R3', 'R4']]}, ['8A01', '8A03']), ('sampled regression 78', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R2'}, {'id': '9A01', 'cls': 3, 'sched': 120, 'eta': 180, 'route': 'R2'}, {'id': '9A02', 'cls': 2, 'sched': 200, 'eta': 160, 'route': 'R3'}, {'id': '2A03', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R3'}, {'id': '8A04', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R3', 'R2'], ['R5', 'R4'], ['R4', 'R3']]}, ['8A04']), ('sampled regression 50', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 3, 'sched': 100, 'eta': 130, 'route': 'R5'}, {'id': '4A01', 'cls': 3, 'sched': 200, 'eta': 180, 'route': 'R4'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R3'}, {'id': '4A03', 'cls': 3, 'sched': 100, 'eta': 180, 'route': 'R3'}, {'id': '7A04', 'cls': 3, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '2A05', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R4'}], 'conflicts': [['R1', 'R2'], ['R1', 'R5'], ['R4', 'R3']]}, ['2A05', '9A00']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('sampled regression 45', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 3, 'sched': 150, 'eta': 180, 'route': 'R1'}, {'id': '2A01', 'cls': 3, 'sched': 120, 'eta': 80, 'route': 'R1'}, {'id': '2A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '5A03', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R2'}, {'id': '9A04', 'cls': 3, 'sched': 200, 'eta': 130, 'route': 'R1'}, {'id': '5A05', 'cls': 1, 'sched': 120, 'eta': 130, 'route': 'R1'}], 'conflicts': [['R5', 'R4'], ['R3', 'R2'], ['R1', 'R5'], ['R3', 'R4']]}, ['5A03', '5A05']), ('control 48', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '5A00', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R1'}, {'id': '2A01', 'cls': 3, 'sched': 100, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R4', 'R5'], ['R3', 'R4'], ['R1', 'R2']]}, ['5A00', '2A01']), ('control 51', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '5A00', 'cls': 3, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '6A01', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R2'}, {'id': '3A02', 'cls': 2, 'sched': 120, 'eta': 160, 'route': 'R4'}, {'id': '6A03', 'cls': 2, 'sched': 100, 'eta': 400, 'route': 'R2'}], 'conflicts': [['R3', 'R2'], ['R4', 'R5']]}, ['3A02'])]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    now = x['now']
    cand = [t for t in x['trains'] if t['eta'] - now <= x['lookahead_s']]
    cand.sort(key=lambda t: (t['cls'], t['sched'], t['id']))
    pairs = set()
    for a, b in x['conflicts']:
        pairs.add((a, b))
        pairs.add((b, a))
    granted = []
    routes = []
    for t in cand:
        if routes[-1:] == [t['route']] or any((t['route'], r) in pairs for r in routes):
            continue
        granted.append(t['id'])
        routes.append(t['route'])
    return granted
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('sampled regression 3', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '3A00', 'cls': 1, 'sched': 150, 'eta': 160, 'route': 'R1'}, {'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 160, 'route': 'R4'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 100, 'route': 'R4'}, {'id': '1A03', 'cls': 3, 'sched': 150, 'eta': 80, 'route': 'R1'}, {'id': '9A04', 'cls': 3, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '7A05', 'cls': 3, 'sched': 120, 'eta': 220, 'route': 'R3'}], 'conflicts': [['R2', 'R1'], ['R1', 'R5'], ['R3', 'R4']]}, ['6A02', '3A00']), ('sampled regression 6', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '5A01', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R3'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 100, 'route': 'R3'}, {'id': '5A03', 'cls': 1, 'sched': 120, 'eta': 180, 'route': 'R5'}, {'id': '2A04', 'cls': 1, 'sched': 200, 'eta': 130, 'route': 'R4'}, {'id': '6A05', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R1'}], 'conflicts': [['R4', 'R3'], ['R2', 'R1'], ['R5', 'R1'], ['R3', 'R2']]}, ['5A03', '6A02']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('boundary: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('control 1', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '7A01', 'cls': 1, 'sched': 120, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R4', 'R3'], ['R2', 'R3'], ['R4', 'R5'], ['R2', 'R1']]}, []), ('control 4', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R3'}, {'id': '8A01', 'cls': 1, 'sched': 100, 'eta': 80, 'route': 'R5'}, {'id': '7A02', 'cls': 3, 'sched': 100, 'eta': 80, 'route': 'R2'}], 'conflicts': [['R1', 'R5'], ['R1', 'R2'], ['R3', 'R2'], ['R4', 'R5']]}, ['8A01', '2A00']), ('control 7', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R5'}, {'id': '9A01', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R1'}], 'conflicts': [['R3', 'R2']]}, ['9A01'])], [('regression: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('sampled regression 6', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '5A01', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R3'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 100, 'route': 'R3'}, {'id': '5A03', 'cls': 1, 'sched': 120, 'eta': 180, 'route': 'R5'}, {'id': '2A04', 'cls': 1, 'sched': 200, 'eta': 130, 'route': 'R4'}, {'id': '6A05', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R1'}], 'conflicts': [['R4', 'R3'], ['R2', 'R1'], ['R5', 'R1'], ['R3', 'R2']]}, ['5A03', '6A02']), ('sampled regression 22', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 150, 'eta': 80, 'route': 'R3'}, {'id': '5A01', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R3'}], 'conflicts': [['R1', 'R2'], ['R5', 'R1']]}, ['5A01']), ('sampled regression 38', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '8A00', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R4'}, {'id': '7A01', 'cls': 3, 'sched': 100, 'eta': 400, 'route': 'R5'}, {'id': '9A02', 'cls': 2, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '6A03', 'cls': 3, 'sched': 100, 'eta': 100, 'route': 'R2'}, {'id': '9A04', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R1'}, {'id': '4A05', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R1'}], 'conflicts': [['R5', 'R4'], ['R2', 'R1']]}, ['4A05', '8A00']), ('boundary: class beats schedule', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 150, 'eta': 150, 'route': 'R2'}]}, ['1A02']), ('control 12', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '8A00', 'cls': 1, 'sched': 100, 'eta': 180, 'route': 'R3'}, {'id': '8A01', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R2'}], 'conflicts': [['R3', 'R4']]}, ['8A00', '8A01']), ('control 15', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 200, 'eta': 180, 'route': 'R5'}, {'id': '6A01', 'cls': 3, 'sched': 150, 'eta': 80, 'route': 'R2'}, {'id': '7A02', 'cls': 3, 'sched': 100, 'eta': 130, 'route': 'R5'}, {'id': '5A03', 'cls': 3, 'sched': 120, 'eta': 160, 'route': 'R1'}], 'conflicts': [['R2', 'R1'], ['R4', 'R5'], ['R1', 'R5']]}, ['7A02', '6A01']), ('control 18', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '8A01', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R3'}], 'conflicts': [['R5', 'R4'], ['R1', 'R2'], ['R4', 'R3']]}, ['9A00'])], [('regression: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('sampled regression 10', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '1A00', 'cls': 1, 'sched': 200, 'eta': 220, 'route': 'R1'}, {'id': '6A01', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R5'}, {'id': '7A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R2'}, {'id': '4A03', 'cls': 3, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '1A04', 'cls': 1, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '8A05', 'cls': 3, 'sched': 150, 'eta': 400, 'route': 'R4'}], 'conflicts': [['R2', 'R1'], ['R3', 'R4'], ['R5', 'R4']]}, ['1A04', '1A00']), ('sampled regression 40', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R4'}, {'id': '2A01', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R4'}, {'id': '8A02', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R3'}, {'id': '1A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R3'}, {'id': '8A04', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A05', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R1', 'R5'], ['R2', 'R3'], ['R1', 'R2'], ['R3', 'R4']]}, ['8A02', '3A05']), ('sampled regression 69', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '4A00', 'cls': 3, 'sched': 200, 'eta': 100, 'route': 'R1'}, {'id': '9A01', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R3'}, {'id': '4A02', 'cls': 3, 'sched': 200, 'eta': 130, 'route': 'R4'}, {'id': '9A03', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R4'}, {'id': '9A04', 'cls': 1, 'sched': 150, 'eta': 160, 'route': 'R3'}, {'id': '1A05', 'cls': 1, 'sched': 200, 'eta': 100, 'route': 'R2'}], 'conflicts': [['R3', 'R4'], ['R1', 'R5'], ['R2', 'R3'], ['R5', 'R4']]}, ['9A03', '1A05', '4A00']), ('boundary: reverse-listed conflict granted in reverse', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R3', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R2'}]}, ['1A01']), ('control 23', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 200, 'eta': 180, 'route': 'R4'}, {'id': '1A01', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R2'}, {'id': '5A02', 'cls': 2, 'sched': 120, 'eta': 160, 'route': 'R5'}, {'id': '2A03', 'cls': 2, 'sched': 150, 'eta': 80, 'route': 'R2'}], 'conflicts': [['R1', 'R5'], ['R2', 'R1']]}, ['9A00', '5A02', '2A03']), ('sampled regression 26', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R2'}, {'id': '7A01', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R4'}, {'id': '2A02', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R2'}, {'id': '1A03', 'cls': 3, 'sched': 150, 'eta': 130, 'route': 'R1'}, {'id': '7A04', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R1'}, {'id': '1A05', 'cls': 2, 'sched': 150, 'eta': 400, 'route': 'R3'}], 'conflicts': [['R1', 'R5']]}, ['9A00', '7A04']), ('control 29', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '6A00', 'cls': 2, 'sched': 200, 'eta': 80, 'route': 'R5'}, {'id': '8A01', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R1'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '2A03', 'cls': 1, 'sched': 200, 'eta': 400, 'route': 'R3'}], 'conflicts': [['R5', 'R4'], ['R4', 'R3'], ['R2', 'R1'], ['R3', 'R2']]}, ['6A00'])], [('regression: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('sampled regression 17', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '9A00', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R1'}, {'id': '9A01', 'cls': 3, 'sched': 200, 'eta': 160, 'route': 'R5'}, {'id': '4A02', 'cls': 3, 'sched': 200, 'eta': 160, 'route': 'R4'}, {'id': '3A03', 'cls': 3, 'sched': 100, 'eta': 100, 'route': 'R3'}, {'id': '1A04', 'cls': 1, 'sched': 200, 'eta': 400, 'route': 'R4'}, {'id': '4A05', 'cls': 3, 'sched': 150, 'eta': 130, 'route': 'R1'}], 'conflicts': [['R2', 'R3'], ['R5', 'R1'], ['R2', 'R1']]}, ['9A00', '3A03', '4A02']), ('sampled regression 59', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 150, 'eta': 220, 'route': 'R2'}, {'id': '3A01', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R3'}, {'id': '5A02', 'cls': 1, 'sched': 200, 'eta': 160, 'route': 'R3'}, {'id': '9A03', 'cls': 1, 'sched': 200, 'eta': 80, 'route': 'R3'}], 'conflicts': [['R2', 'R1']]}, ['5A02']), ('boundary: conflict with an earlier grant', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R5']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 110, 'eta': 120, 'route': 'R4'}, {'id': '1A03', 'cls': 1, 'sched': 130, 'eta': 130, 'route': 'R5'}]}, ['1A01', '1A02']), ('boundary: late running train keeps schedule order', {'now': 100, 'lookahead_s': 60, 'conflicts': [['R1', 'R2']], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 150, 'route': 'R1'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 105, 'route': 'R2'}]}, ['1A01']), ('sampled regression 34', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 150, 'eta': 160, 'route': 'R5'}, {'id': '6A01', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R4', 'R3']]}, ['2A00']), ('sampled regression 37', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '4A00', 'cls': 3, 'sched': 120, 'eta': 400, 'route': 'R4'}, {'id': '1A01', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R4'}, {'id': '6A02', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A03', 'cls': 2, 'sched': 150, 'eta': 220, 'route': 'R5'}, {'id': '1A04', 'cls': 3, 'sched': 120, 'eta': 220, 'route': 'R2'}, {'id': '6A05', 'cls': 3, 'sched': 120, 'eta': 180, 'route': 'R1'}], 'conflicts': [['R1', 'R5'], ['R2', 'R1'], ['R3', 'R4']]}, ['1A01']), ('sampled regression 40', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R4'}, {'id': '2A01', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R4'}, {'id': '8A02', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R3'}, {'id': '1A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R3'}, {'id': '8A04', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R4'}, {'id': '3A05', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}], 'conflicts': [['R1', 'R5'], ['R2', 'R3'], ['R1', 'R2'], ['R3', 'R4']]}, ['8A02', '3A05'])], [('regression: two trains for one route', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 110, 'route': 'R3'}, {'id': '1A02', 'cls': 1, 'sched': 120, 'eta': 120, 'route': 'R3'}]}, ['1A01']), ('sampled regression 19', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '2A00', 'cls': 2, 'sched': 150, 'eta': 80, 'route': 'R3'}, {'id': '8A01', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R3'}, {'id': '7A02', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R1'}, {'id': '8A03', 'cls': 1, 'sched': 200, 'eta': 80, 'route': 'R5'}], 'conflicts': [['R1', 'R2'], ['R2', 'R3'], ['R4', 'R5'], ['R3', 'R4']]}, ['8A01', '8A03']), ('sampled regression 78', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R2'}, {'id': '9A01', 'cls': 3, 'sched': 120, 'eta': 180, 'route': 'R2'}, {'id': '9A02', 'cls': 2, 'sched': 200, 'eta': 160, 'route': 'R3'}, {'id': '2A03', 'cls': 2, 'sched': 120, 'eta': 180, 'route': 'R3'}, {'id': '8A04', 'cls': 1, 'sched': 150, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R3', 'R2'], ['R5', 'R4'], ['R4', 'R3']]}, ['8A04']), ('sampled regression 50', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '9A00', 'cls': 3, 'sched': 100, 'eta': 130, 'route': 'R5'}, {'id': '4A01', 'cls': 3, 'sched': 200, 'eta': 180, 'route': 'R4'}, {'id': '6A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R3'}, {'id': '4A03', 'cls': 3, 'sched': 100, 'eta': 180, 'route': 'R3'}, {'id': '7A04', 'cls': 3, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '2A05', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R4'}], 'conflicts': [['R1', 'R2'], ['R1', 'R5'], ['R4', 'R3']]}, ['2A05', '9A00']), ('boundary: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('sampled regression 45', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '6A00', 'cls': 3, 'sched': 150, 'eta': 180, 'route': 'R1'}, {'id': '2A01', 'cls': 3, 'sched': 120, 'eta': 80, 'route': 'R1'}, {'id': '2A02', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R1'}, {'id': '5A03', 'cls': 1, 'sched': 120, 'eta': 160, 'route': 'R2'}, {'id': '9A04', 'cls': 3, 'sched': 200, 'eta': 130, 'route': 'R1'}, {'id': '5A05', 'cls': 1, 'sched': 120, 'eta': 130, 'route': 'R1'}], 'conflicts': [['R5', 'R4'], ['R3', 'R2'], ['R1', 'R5'], ['R3', 'R4']]}, ['5A03', '5A05']), ('control 48', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '5A00', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R1'}, {'id': '2A01', 'cls': 3, 'sched': 100, 'eta': 100, 'route': 'R3'}], 'conflicts': [['R4', 'R5'], ['R3', 'R4'], ['R1', 'R2']]}, ['5A00', '2A01']), ('control 51', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '5A00', 'cls': 3, 'sched': 150, 'eta': 220, 'route': 'R4'}, {'id': '6A01', 'cls': 1, 'sched': 120, 'eta': 400, 'route': 'R2'}, {'id': '3A02', 'cls': 2, 'sched': 120, 'eta': 160, 'route': 'R4'}, {'id': '6A03', 'cls': 2, 'sched': 100, 'eta': 400, 'route': 'R2'}], 'conflicts': [['R3', 'R2'], ['R4', 'R5']]}, ['3A02'])]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The same route is granted to two trains.

Trains whose eta is at most lookahead_s after now are candidates, including overdue trains (eta before now). Candidates are ordered by class (1 highest), then scheduled time, then id. Routes are granted greedily in that order; a candidate is skipped if its route equals or conflicts with (conflict pairs are unordered) any route already granted. Return granted train ids in grant order.

split: open-access