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

N = 1
observations = []
def solve(x):
    set_pts = {}
    held = []
    locks = x['point_locks']
    for p, pos in sorted(x['route_points'].items()):
        for fp, fpos, fsig in x['flank'].get(p + pos, []):
            lk = locks.get(fp, set_pts.get(fp))
            if lk is None or lk == fpos:
                set_pts[fp] = fpos
            elif fsig is not None and fsig in x['holdable']:
                held.append(fsig)
            else:
                return {'result': 'refused:' + fp, 'set_points': {}, 'held_signals': []}
    return {'result': 'protected', 'set_points': set_pts, 'held_signals': held}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: same signal held twice', {'route_points': {'P1': 'N', 'P2': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P8': 'N', 'P9': 'N'}, 'holdable': ['S20', 'S22']}, {'result': 'protected', 'set_points': {'P8': 'N'}, 'held_signals': ['S20', 'S22']}), ('boundary: flank point locked in the protecting lie', {'route_points': {'P1': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'R', 'P8': 'N'}, 'held_signals': []}), ('boundary: flank point free', {'route_points': {'P1': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'R', 'P8': 'N'}, 'held_signals': []}), ('boundary: two route points disagree on a flank point', {'route_points': {'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'N'}, 'held_signals': []}), ('boundary: flank signal not holdable', {'route_points': {'P1': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R'}, 'holdable': []}, {'result': 'refused:P7', 'set_points': {}, 'held_signals': []}), ('control 1', {'route_points': {'P3': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R', 'P9': 'N'}, 'holdable': ['S23', 'S24']}, {'result': 'refused:P9', 'set_points': {}, 'held_signals': []}), ('control 4', {'route_points': {'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S21', 'S22']}, {'result': 'protected', 'set_points': {'P9': 'N', 'P7': 'N'}, 'held_signals': []}), ('control 7', {'route_points': {'P2': 'R', 'P1': 'R', 'P3': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R', 'P8': 'R', 'P9': 'R'}, 'holdable': ['S22', 'S24']}, {'result': 'refused:P7', 'set_points': {}, 'held_signals': []})], [('regression: same signal held twice', {'route_points': {'P1': 'N', 'P2': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P8': 'N', 'P9': 'N'}, 'holdable': ['S20', 'S22']}, {'result': 'protected', 'set_points': {'P8': 'N'}, 'held_signals': ['S20', 'S22']}), ('boundary: two route points disagree on a flank point', {'route_points': {'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'N'}, 'held_signals': []}), ('boundary: flank signal not holdable', {'route_points': {'P1': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R'}, 'holdable': []}, {'result': 'refused:P7', 'set_points': {}, 'held_signals': []}), ('boundary: no flank signal available', {'route_points': {'P3': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'N'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'refused:P9', 'set_points': {}, 'held_signals': []}), ('boundary: reverse lie flank entry', {'route_points': {'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'R'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N'}, 'held_signals': ['S23']}), ('control 12', {'route_points': {'P1': 'R', 'P3': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S21', 'S22', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P8': 'N'}, 'held_signals': []}), ('control 15', {'route_points': {'P1': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'N'}, 'holdable': ['S24']}, {'result': 'protected', 'set_points': {'P7': 'N'}, 'held_signals': []}), ('control 18', {'route_points': {'P3': 'R', 'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P9': 'N'}, 'holdable': ['S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'N', 'P8': 'N'}, 'held_signals': []})], [('regression: same signal held twice', {'route_points': {'P1': 'N', 'P2': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P8': 'N', 'P9': 'N'}, 'holdable': ['S20', 'S22']}, {'result': 'protected', 'set_points': {'P8': 'N'}, 'held_signals': ['S20', 'S22']}), ('boundary: no flank signal available', {'route_points': {'P3': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'N'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'refused:P9', 'set_points': {}, 'held_signals': []}), ('boundary: reverse lie flank entry', {'route_points': {'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'R'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N'}, 'held_signals': ['S23']}), ('boundary: own conflicting demand with a holdable signal', {'route_points': {'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S21', 'S23']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'N'}, 'held_signals': []}), ('boundary: own conflicting demand without a signal', {'route_points': {'P1': 'N', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'refused:P7', 'set_points': {}, 'held_signals': []}), ('control 23', {'route_points': {'P1': 'R', 'P2': 'N', 'P3': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P8': 'R', 'P9': 'R'}, 'held_signals': ['S24']}), ('control 26', {'route_points': {'P3': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R', 'P8': 'N'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P9': 'R'}, 'held_signals': []}), ('control 29', {'route_points': {'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'R'}, 'holdable': ['S20', 'S23']}, {'result': 'protected', 'set_points': {'P7': 'N'}, 'held_signals': ['S23']})], [('regression: same signal held twice', {'route_points': {'P1': 'N', 'P2': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P8': 'N', 'P9': 'N'}, 'holdable': ['S20', 'S22']}, {'result': 'protected', 'set_points': {'P8': 'N'}, 'held_signals': ['S20', 'S22']}), ('boundary: own conflicting demand with a holdable signal', {'route_points': {'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S21', 'S23']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'N'}, 'held_signals': []}), ('boundary: own conflicting demand without a signal', {'route_points': {'P1': 'N', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'refused:P7', 'set_points': {}, 'held_signals': []}), ('boundary: flank point locked in the protecting lie', {'route_points': {'P1': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'R', 'P8': 'N'}, 'held_signals': []}), ('boundary: flank point free', {'route_points': {'P1': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'R', 'P8': 'N'}, 'held_signals': []}), ('control 34', {'route_points': {'P3': 'R', 'P2': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R', 'P9': 'N'}, 'holdable': ['S20', 'S21', 'S24']}, {'result': 'protected', 'set_points': {'P8': 'R'}, 'held_signals': ['S20', 'S24']}), ('control 37', {'route_points': {'P2': 'N', 'P1': 'R', 'P3': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P8': 'R'}, 'holdable': ['S21', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P8': 'R', 'P9': 'R'}, 'held_signals': []}), ('control 40', {'route_points': {'P3': 'R', 'P1': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P8': 'N'}, 'held_signals': []})], [('regression: same signal held twice', {'route_points': {'P1': 'N', 'P2': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P8': 'N', 'P9': 'N'}, 'holdable': ['S20', 'S22']}, {'result': 'protected', 'set_points': {'P8': 'N'}, 'held_signals': ['S20', 'S22']}), ('boundary: own conflicting demand without a signal', {'route_points': {'P1': 'N', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'refused:P7', 'set_points': {}, 'held_signals': []}), ('boundary: flank point locked in the protecting lie', {'route_points': {'P1': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'R', 'P8': 'N'}, 'held_signals': []}), ('boundary: flank point free', {'route_points': {'P1': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'R', 'P8': 'N'}, 'held_signals': []}), ('boundary: two route points disagree on a flank point', {'route_points': {'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'N'}, 'held_signals': []}), ('control 45', {'route_points': {'P2': 'N', 'P1': 'N', 'P3': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R', 'P8': 'R'}, 'holdable': ['S20', 'S21', 'S23', 'S24']}, {'result': 'refused:P8', 'set_points': {}, 'held_signals': []}), ('control 48', {'route_points': {'P2': 'R', 'P3': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P8': 'N'}, 'holdable': ['S20', 'S21', 'S22']}, {'result': 'protected', 'set_points': {'P9': 'N', 'P7': 'N', 'P8': 'N'}, 'held_signals': []}), ('control 51', {'route_points': {'P3': 'N', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'R'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'R'}, 'held_signals': ['S23']})]]
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):
    set_pts = {}
    held = []
    locks = x['point_locks']
    for p, pos in sorted(x['route_points'].items()):
        for fp, fpos, fsig in x['flank'].get(p + pos, []):
            lk = locks.get(fp, set_pts.get(fp))
            if lk is None or lk == fpos:
                set_pts[fp] = fpos
            elif fsig is not None and fsig in x['holdable']:
                held.append(fsig)
            else:
                return {'result': 'refused:' + fp, 'set_points': {}, 'held_signals': []}
    return {'result': 'protected', 'set_points': set_pts, 'held_signals': sorted(held)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: same signal held twice', {'route_points': {'P1': 'N', 'P2': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P8': 'N', 'P9': 'N'}, 'holdable': ['S20', 'S22']}, {'result': 'protected', 'set_points': {'P8': 'N'}, 'held_signals': ['S20', 'S22']}), ('boundary: flank point locked in the protecting lie', {'route_points': {'P1': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'R', 'P8': 'N'}, 'held_signals': []}), ('boundary: flank point free', {'route_points': {'P1': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'R', 'P8': 'N'}, 'held_signals': []}), ('boundary: two route points disagree on a flank point', {'route_points': {'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'N'}, 'held_signals': []}), ('boundary: flank signal not holdable', {'route_points': {'P1': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R'}, 'holdable': []}, {'result': 'refused:P7', 'set_points': {}, 'held_signals': []}), ('control 1', {'route_points': {'P3': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R', 'P9': 'N'}, 'holdable': ['S23', 'S24']}, {'result': 'refused:P9', 'set_points': {}, 'held_signals': []}), ('control 4', {'route_points': {'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S21', 'S22']}, {'result': 'protected', 'set_points': {'P9': 'N', 'P7': 'N'}, 'held_signals': []}), ('control 7', {'route_points': {'P2': 'R', 'P1': 'R', 'P3': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R', 'P8': 'R', 'P9': 'R'}, 'holdable': ['S22', 'S24']}, {'result': 'refused:P7', 'set_points': {}, 'held_signals': []})], [('regression: same signal held twice', {'route_points': {'P1': 'N', 'P2': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P8': 'N', 'P9': 'N'}, 'holdable': ['S20', 'S22']}, {'result': 'protected', 'set_points': {'P8': 'N'}, 'held_signals': ['S20', 'S22']}), ('boundary: two route points disagree on a flank point', {'route_points': {'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'N'}, 'held_signals': []}), ('boundary: flank signal not holdable', {'route_points': {'P1': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R'}, 'holdable': []}, {'result': 'refused:P7', 'set_points': {}, 'held_signals': []}), ('boundary: no flank signal available', {'route_points': {'P3': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'N'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'refused:P9', 'set_points': {}, 'held_signals': []}), ('boundary: reverse lie flank entry', {'route_points': {'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'R'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N'}, 'held_signals': ['S23']}), ('control 12', {'route_points': {'P1': 'R', 'P3': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S21', 'S22', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P8': 'N'}, 'held_signals': []}), ('control 15', {'route_points': {'P1': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'N'}, 'holdable': ['S24']}, {'result': 'protected', 'set_points': {'P7': 'N'}, 'held_signals': []}), ('control 18', {'route_points': {'P3': 'R', 'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P9': 'N'}, 'holdable': ['S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'N', 'P8': 'N'}, 'held_signals': []})], [('regression: same signal held twice', {'route_points': {'P1': 'N', 'P2': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P8': 'N', 'P9': 'N'}, 'holdable': ['S20', 'S22']}, {'result': 'protected', 'set_points': {'P8': 'N'}, 'held_signals': ['S20', 'S22']}), ('boundary: no flank signal available', {'route_points': {'P3': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'N'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'refused:P9', 'set_points': {}, 'held_signals': []}), ('boundary: reverse lie flank entry', {'route_points': {'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'R'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N'}, 'held_signals': ['S23']}), ('boundary: own conflicting demand with a holdable signal', {'route_points': {'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S21', 'S23']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'N'}, 'held_signals': []}), ('boundary: own conflicting demand without a signal', {'route_points': {'P1': 'N', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'refused:P7', 'set_points': {}, 'held_signals': []}), ('control 23', {'route_points': {'P1': 'R', 'P2': 'N', 'P3': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P8': 'R', 'P9': 'R'}, 'held_signals': ['S24']}), ('control 26', {'route_points': {'P3': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R', 'P8': 'N'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P9': 'R'}, 'held_signals': []}), ('control 29', {'route_points': {'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'R'}, 'holdable': ['S20', 'S23']}, {'result': 'protected', 'set_points': {'P7': 'N'}, 'held_signals': ['S23']})], [('regression: same signal held twice', {'route_points': {'P1': 'N', 'P2': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P8': 'N', 'P9': 'N'}, 'holdable': ['S20', 'S22']}, {'result': 'protected', 'set_points': {'P8': 'N'}, 'held_signals': ['S20', 'S22']}), ('boundary: own conflicting demand with a holdable signal', {'route_points': {'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S21', 'S23']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'N'}, 'held_signals': []}), ('boundary: own conflicting demand without a signal', {'route_points': {'P1': 'N', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'refused:P7', 'set_points': {}, 'held_signals': []}), ('boundary: flank point locked in the protecting lie', {'route_points': {'P1': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'R', 'P8': 'N'}, 'held_signals': []}), ('boundary: flank point free', {'route_points': {'P1': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'R', 'P8': 'N'}, 'held_signals': []}), ('control 34', {'route_points': {'P3': 'R', 'P2': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R', 'P9': 'N'}, 'holdable': ['S20', 'S21', 'S24']}, {'result': 'protected', 'set_points': {'P8': 'R'}, 'held_signals': ['S20', 'S24']}), ('control 37', {'route_points': {'P2': 'N', 'P1': 'R', 'P3': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P8': 'R'}, 'holdable': ['S21', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P8': 'R', 'P9': 'R'}, 'held_signals': []}), ('control 40', {'route_points': {'P3': 'R', 'P1': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P8': 'N'}, 'held_signals': []})], [('regression: same signal held twice', {'route_points': {'P1': 'N', 'P2': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P8': 'N', 'P9': 'N'}, 'holdable': ['S20', 'S22']}, {'result': 'protected', 'set_points': {'P8': 'N'}, 'held_signals': ['S20', 'S22']}), ('boundary: own conflicting demand without a signal', {'route_points': {'P1': 'N', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'refused:P7', 'set_points': {}, 'held_signals': []}), ('boundary: flank point locked in the protecting lie', {'route_points': {'P1': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'R', 'P8': 'N'}, 'held_signals': []}), ('boundary: flank point free', {'route_points': {'P1': 'N'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'R', 'P8': 'N'}, 'held_signals': []}), ('boundary: two route points disagree on a flank point', {'route_points': {'P1': 'R', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'N'}, 'held_signals': []}), ('control 45', {'route_points': {'P2': 'N', 'P1': 'N', 'P3': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'R', 'P8': 'R'}, 'holdable': ['S20', 'S21', 'S23', 'S24']}, {'result': 'refused:P8', 'set_points': {}, 'held_signals': []}), ('control 48', {'route_points': {'P2': 'R', 'P3': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P7': 'N', 'P8': 'N'}, 'holdable': ['S20', 'S21', 'S22']}, {'result': 'protected', 'set_points': {'P9': 'N', 'P7': 'N', 'P8': 'N'}, 'held_signals': []}), ('control 51', {'route_points': {'P3': 'N', 'P2': 'R'}, 'flank': {'P1N': [['P7', 'R', 'S20'], ['P8', 'N', None]], 'P1R': [['P7', 'N', 'S21']], 'P2N': [['P8', 'R', 'S22'], ['P9', 'R', 'S20']], 'P2R': [['P9', 'N', 'S23'], ['P7', 'N', None]], 'P3N': [['P9', 'R', None]], 'P3R': [['P8', 'N', 'S24']]}, 'point_locks': {'P9': 'R'}, 'holdable': ['S20', 'S21', 'S22', 'S23', 'S24']}, {'result': 'protected', 'set_points': {'P7': 'N', 'P9': 'R'}, 'held_signals': ['S23']})]]
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 held signal list repeats a signal or is out of order.

For each route point (sorted) in its lie, the flank table (key point+lie) lists flank points with the protecting lie and an optional flank signal. A flank point is set to its protecting lie if it is free, locked in that lie by another route, or already demanded in that lie by this route; if it is unavailable, its flank signal is held at danger when the signal exists and is holdable; otherwise the route is refused (refused:<flank point>, nothing set or held). Held signals are returned sorted and unique.

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 0 <= 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 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: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('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']]}, []), ('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']), ('control 2', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '1A00', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R2'}, {'id': '7A01', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R2'}, {'id': '7A02', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R2'}], 'conflicts': [['R5', 'R4'], ['R4', 'R3'], ['R2', 'R1'], ['R3', 'R2']]}, ['1A00']), ('regression: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('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']), ('sampled regression 13', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 3, 'sched': 200, 'eta': 80, 'route': 'R4'}, {'id': '3A01', 'cls': 1, 'sched': 200, 'eta': 400, 'route': 'R4'}, {'id': '2A02', 'cls': 3, 'sched': 120, 'eta': 160, 'route': 'R5'}, {'id': '9A03', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R5'}, {'id': '3A04', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R4'}], 'conflicts': [['R3', 'R2'], ['R1', 'R2'], ['R4', 'R5']]}, ['3A04'])], [('regression: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('control 2', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '1A00', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R2'}, {'id': '7A01', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R2'}, {'id': '7A02', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R2'}], 'conflicts': [['R5', 'R4'], ['R4', 'R3'], ['R2', 'R1'], ['R3', 'R2']]}, ['1A00']), ('sampled regression 16', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '9A00', 'cls': 3, 'sched': 100, 'eta': 220, 'route': 'R2'}, {'id': '3A01', 'cls': 2, 'sched': 150, 'eta': 400, 'route': 'R2'}, {'id': '8A02', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R1'}, {'id': '3A03', 'cls': 1, 'sched': 100, 'eta': 100, 'route': 'R1'}], 'conflicts': [['R1', 'R5']]}, ['3A03', '9A00']), ('control 11', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '1A00', 'cls': 3, 'sched': 120, 'eta': 180, 'route': 'R3'}, {'id': '3A01', 'cls': 3, 'sched': 150, 'eta': 220, 'route': 'R2'}, {'id': '5A02', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R5'}, {'id': '1A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R1'}, {'id': '3A04', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R4'}], 'conflicts': [['R2', 'R1'], ['R1', 'R5'], ['R4', 'R5'], ['R2', 'R3']]}, ['1A03']), ('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']), ('sampled regression 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']), ('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'])], [('regression: 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 5', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '3A00', 'cls': 1, 'sched': 200, 'eta': 180, 'route': 'R5'}, {'id': '4A01', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R2'}], 'conflicts': [['R2', 'R1'], ['R4', 'R5'], ['R2', 'R3']]}, ['3A00', '4A01']), ('sampled regression 28', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 3, 'sched': 120, 'eta': 400, 'route': 'R4'}, {'id': '6A01', 'cls': 3, 'sched': 100, 'eta': 80, 'route': 'R3'}, {'id': '3A02', 'cls': 2, 'sched': 150, 'eta': 100, 'route': 'R1'}], 'conflicts': [['R3', 'R4'], ['R2', 'R1'], ['R2', 'R3']]}, ['3A02', '6A01']), ('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']), ('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']), ('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']), ('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']), ('sampled regression 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: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('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 42', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '8A00', 'cls': 1, 'sched': 150, 'eta': 130, 'route': 'R5'}, {'id': '2A01', 'cls': 2, 'sched': 150, 'eta': 80, 'route': 'R4'}, {'id': '6A02', 'cls': 2, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '4A03', 'cls': 1, 'sched': 100, 'eta': 80, 'route': 'R5'}, {'id': '1A04', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R4'}, {'id': '7A05', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R5'}], 'conflicts': [['R2', 'R1'], ['R4', 'R3']]}, ['4A03', '1A04']), ('control 27', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '8A00', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R5'}, {'id': '4A01', 'cls': 3, 'sched': 150, 'eta': 130, 'route': 'R1'}], 'conflicts': [['R3', 'R4'], ['R2', 'R3']]}, ['4A01']), ('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']), ('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']), ('control 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: 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 9', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '9A00', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R4'}, {'id': '7A01', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '7A02', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R2'}, {'id': '1A03', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R2'}, {'id': '1A04', 'cls': 3, 'sched': 120, 'eta': 220, 'route': 'R3'}], 'conflicts': [['R2', 'R1']]}, ['1A03', '1A04', '7A01', '9A00']), ('sampled regression 56', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '8A00', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R4'}, {'id': '1A01', 'cls': 1, 'sched': 200, 'eta': 220, 'route': 'R1'}, {'id': '3A02', 'cls': 2, 'sched': 200, 'eta': 400, 'route': 'R4'}, {'id': '1A03', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R5'}], 'conflicts': [['R1', 'R2'], ['R3', 'R2'], ['R5', 'R1']]}, ['8A00', '1A03']), ('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']), ('regression: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('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)

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['sched'] - 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 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: eta exactly at the lookahead', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '1A01', 'cls': 1, 'sched': 100, 'eta': 160, 'route': 'R1'}]}, ['1A01']), ('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']]}, []), ('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']), ('control 2', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '1A00', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R2'}, {'id': '7A01', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R2'}, {'id': '7A02', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R2'}], 'conflicts': [['R5', 'R4'], ['R4', 'R3'], ['R2', 'R1'], ['R3', 'R2']]}, ['1A00']), ('regression: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('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']), ('sampled regression 13', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '2A00', 'cls': 3, 'sched': 200, 'eta': 80, 'route': 'R4'}, {'id': '3A01', 'cls': 1, 'sched': 200, 'eta': 400, 'route': 'R4'}, {'id': '2A02', 'cls': 3, 'sched': 120, 'eta': 160, 'route': 'R5'}, {'id': '9A03', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R5'}, {'id': '3A04', 'cls': 2, 'sched': 100, 'eta': 80, 'route': 'R4'}], 'conflicts': [['R3', 'R2'], ['R1', 'R2'], ['R4', 'R5']]}, ['3A04'])], [('regression: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('control 2', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '1A00', 'cls': 3, 'sched': 100, 'eta': 160, 'route': 'R2'}, {'id': '7A01', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R2'}, {'id': '7A02', 'cls': 3, 'sched': 150, 'eta': 160, 'route': 'R2'}], 'conflicts': [['R5', 'R4'], ['R4', 'R3'], ['R2', 'R1'], ['R3', 'R2']]}, ['1A00']), ('sampled regression 16', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '9A00', 'cls': 3, 'sched': 100, 'eta': 220, 'route': 'R2'}, {'id': '3A01', 'cls': 2, 'sched': 150, 'eta': 400, 'route': 'R2'}, {'id': '8A02', 'cls': 2, 'sched': 200, 'eta': 130, 'route': 'R1'}, {'id': '3A03', 'cls': 1, 'sched': 100, 'eta': 100, 'route': 'R1'}], 'conflicts': [['R1', 'R5']]}, ['3A03', '9A00']), ('control 11', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '1A00', 'cls': 3, 'sched': 120, 'eta': 180, 'route': 'R3'}, {'id': '3A01', 'cls': 3, 'sched': 150, 'eta': 220, 'route': 'R2'}, {'id': '5A02', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R5'}, {'id': '1A03', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R1'}, {'id': '3A04', 'cls': 2, 'sched': 100, 'eta': 220, 'route': 'R4'}], 'conflicts': [['R2', 'R1'], ['R1', 'R5'], ['R4', 'R5'], ['R2', 'R3']]}, ['1A03']), ('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']), ('sampled regression 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']), ('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'])], [('regression: 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 5', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '3A00', 'cls': 1, 'sched': 200, 'eta': 180, 'route': 'R5'}, {'id': '4A01', 'cls': 2, 'sched': 200, 'eta': 100, 'route': 'R2'}], 'conflicts': [['R2', 'R1'], ['R4', 'R5'], ['R2', 'R3']]}, ['3A00', '4A01']), ('sampled regression 28', {'now': 100, 'lookahead_s': 60, 'trains': [{'id': '2A00', 'cls': 3, 'sched': 120, 'eta': 400, 'route': 'R4'}, {'id': '6A01', 'cls': 3, 'sched': 100, 'eta': 80, 'route': 'R3'}, {'id': '3A02', 'cls': 2, 'sched': 150, 'eta': 100, 'route': 'R1'}], 'conflicts': [['R3', 'R4'], ['R2', 'R1'], ['R2', 'R3']]}, ['3A02', '6A01']), ('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']), ('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']), ('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']), ('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']), ('sampled regression 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: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('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 42', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '8A00', 'cls': 1, 'sched': 150, 'eta': 130, 'route': 'R5'}, {'id': '2A01', 'cls': 2, 'sched': 150, 'eta': 80, 'route': 'R4'}, {'id': '6A02', 'cls': 2, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '4A03', 'cls': 1, 'sched': 100, 'eta': 80, 'route': 'R5'}, {'id': '1A04', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R4'}, {'id': '7A05', 'cls': 2, 'sched': 200, 'eta': 220, 'route': 'R5'}], 'conflicts': [['R2', 'R1'], ['R4', 'R3']]}, ['4A03', '1A04']), ('control 27', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '8A00', 'cls': 2, 'sched': 120, 'eta': 220, 'route': 'R5'}, {'id': '4A01', 'cls': 3, 'sched': 150, 'eta': 130, 'route': 'R1'}], 'conflicts': [['R3', 'R4'], ['R2', 'R3']]}, ['4A01']), ('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']), ('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']), ('control 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: 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 9', {'now': 100, 'lookahead_s': 120, 'trains': [{'id': '9A00', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R4'}, {'id': '7A01', 'cls': 3, 'sched': 120, 'eta': 130, 'route': 'R5'}, {'id': '7A02', 'cls': 1, 'sched': 100, 'eta': 400, 'route': 'R2'}, {'id': '1A03', 'cls': 2, 'sched': 100, 'eta': 130, 'route': 'R2'}, {'id': '1A04', 'cls': 3, 'sched': 120, 'eta': 220, 'route': 'R3'}], 'conflicts': [['R2', 'R1']]}, ['1A03', '1A04', '7A01', '9A00']), ('sampled regression 56', {'now': 100, 'lookahead_s': 80, 'trains': [{'id': '8A00', 'cls': 1, 'sched': 150, 'eta': 180, 'route': 'R4'}, {'id': '1A01', 'cls': 1, 'sched': 200, 'eta': 220, 'route': 'R1'}, {'id': '3A02', 'cls': 2, 'sched': 200, 'eta': 400, 'route': 'R4'}, {'id': '1A03', 'cls': 2, 'sched': 120, 'eta': 100, 'route': 'R5'}], 'conflicts': [['R1', 'R2'], ['R3', 'R2'], ['R5', 'R1']]}, ['8A00', '1A03']), ('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']), ('regression: overdue train', {'now': 100, 'lookahead_s': 60, 'conflicts': [], 'trains': [{'id': '2A01', 'cls': 2, 'sched': 60, 'eta': 70, 'route': 'R2'}]}, ['2A01']), ('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:
An overdue train or one arriving exactly at the horizon gets no route.

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