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

N = 1
observations = []
def solve(x):
    route = x['route']
    seen = set()
    released = []
    fault = False
    occ = set()
    for sec, ev in x['events']:
        if sec not in route:
            continue
        if ev == 'occ':
            occ.add(sec)
            seen.add(sec)
        else:
            if sec not in seen:
                fault = True
            occ.discard(sec)
            i = route.index(sec)
            nxt_ok = i == len(route) - 1 or route[i + 1] in seen
            prev_ok = i == 0 or route[i - 1] in released
            if not fault and nxt_ok and prev_ok and sec not in released:
                released.append(sec)
    return {'released': released, 'fault': fault}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: normal passage', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 63', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 1', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 4', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 7', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'clr'], ['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True})], [('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('regression: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 4', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('boundary: phantom clear then passage', {'route': ['T1', 'T2'], 'events': [['T2', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': [], 'fault': True}), ('control 12', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('control 15', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T4', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 18', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: foreign section noise', {'route': ['T1', 'T2'], 'events': [['Z9', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('sampled regression 25', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 12', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('control 23', {'route': ['T1', 'T2', 'T3'], 'events': [['T3', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True}), ('control 26', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T1', 'clr'], ['T4', 'clr'], ['T4', 'occ']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 29', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: normal passage', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 79', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['Z9', 'clr'], ['T2', 'clr'], ['T4', 'occ'], ['T4', 'clr'], ['T3', 'clr'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 18', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('control 34', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'occ'], ['Z9', 'clr'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 37', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4', 'T5'], 'fault': False}), ('control 40', {'route': ['T1', 'T2'], 'events': [['T2', 'occ'], ['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T2', 'clr']]}, {'released': [], 'fault': False})], [('regression: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 29', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('boundary: normal passage', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('control 45', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['Z9', 'occ'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 48', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['Z9', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('control 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False})]]
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):
    route = x['route']
    seen = set()
    released = []
    fault = False
    occ = set()
    for sec, ev in x['events']:
        if sec not in route:
            continue
        if ev == 'occ':
            occ.add(sec)
            seen.add(sec)
        else:
            if sec not in seen:
                fault = True
            occ.discard(sec)
            i = route.index(sec)
            nxt_ok = route[i + 1] in occ if i < len(route) - 1 else False
            prev_ok = i == 0 or route[i - 1] in released
            if not fault and nxt_ok and prev_ok and sec not in released:
                released.append(sec)
    return {'released': released, 'fault': fault}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: normal passage', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 63', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 1', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 4', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 7', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'clr'], ['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True})], [('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('regression: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 4', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('boundary: phantom clear then passage', {'route': ['T1', 'T2'], 'events': [['T2', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': [], 'fault': True}), ('control 12', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('control 15', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T4', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 18', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: foreign section noise', {'route': ['T1', 'T2'], 'events': [['Z9', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('sampled regression 25', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 12', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('control 23', {'route': ['T1', 'T2', 'T3'], 'events': [['T3', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True}), ('control 26', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T1', 'clr'], ['T4', 'clr'], ['T4', 'occ']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 29', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: normal passage', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 79', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['Z9', 'clr'], ['T2', 'clr'], ['T4', 'occ'], ['T4', 'clr'], ['T3', 'clr'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 18', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('control 34', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'occ'], ['Z9', 'clr'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 37', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4', 'T5'], 'fault': False}), ('control 40', {'route': ['T1', 'T2'], 'events': [['T2', 'occ'], ['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T2', 'clr']]}, {'released': [], 'fault': False})], [('regression: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 29', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('boundary: normal passage', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('control 45', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['Z9', 'occ'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 48', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['Z9', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('control 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False})]]
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 section is released although the train never moved on into the next section.

Sections of a locked route are released in route order as the train passes. A section is released when it clears, it was seen occupied earlier, its predecessor is already released (or it is the first), and the next route section is currently occupied (or it is the last). A clear of a never-occupied route section latches a fault that stops all further release. Events for sections outside the route are ignored. Output released sections in release order and the fault flag.

split: open-access


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

N = 1
observations = []
def solve(x):
    route = x['route']
    seen = set()
    released = []
    fault = False
    occ = set()
    for sec, ev in x['events']:
        if sec not in route:
            continue
        if ev == 'occ':
            occ.add(sec)
            seen.add(sec)
        else:
            if sec not in seen:
                fault = True
            occ.discard(sec)
            i = route.index(sec)
            nxt_ok = i == len(route) - 1 or route[i + 1] in occ
            prev_ok = i == 0 or route[i - 1] in seen
            if not fault and nxt_ok and prev_ok and sec not in released:
                released.append(sec)
    return {'released': released, 'fault': fault}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 6', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'clr'], ['T3', 'occ'], ['T3', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 24', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T5', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 25', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 1', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 4', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 7', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'clr'], ['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True})], [('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 25', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 55', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('control 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: phantom clear then passage', {'route': ['T1', 'T2'], 'events': [['T2', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': [], 'fault': True}), ('control 12', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('control 15', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T4', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 18', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 31', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T4', 'occ'], ['T2', 'occ'], ['T5', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4', 'T5'], 'fault': False}), ('sampled regression 79', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['Z9', 'clr'], ['T2', 'clr'], ['T4', 'occ'], ['T4', 'clr'], ['T3', 'clr'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 72', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T3', 'occ'], ['T4', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('boundary: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('control 23', {'route': ['T1', 'T2', 'T3'], 'events': [['T3', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True}), ('control 26', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T1', 'clr'], ['T4', 'clr'], ['T4', 'occ']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 29', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 44', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['Z9', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 40', {'route': ['T1', 'T2'], 'events': [['T2', 'occ'], ['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T2', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 25', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('control 34', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'occ'], ['Z9', 'clr'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 37', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4', 'T5'], 'fault': False}), ('control 43', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 46', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['Z9', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T2', 'clr'], ['T3', 'occ'], ['T3', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 63', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 45', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['Z9', 'occ'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 48', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['Z9', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('control 54', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False})]]
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):
    route = x['route']
    seen = set()
    released = []
    fault = False
    occ = set()
    for sec, ev in x['events']:
        if sec not in route:
            continue
        if ev == 'occ':
            occ.add(sec)
            seen.add(sec)
        else:
            if sec not in seen:
                fault = True
            occ.discard(sec)
            i = route.index(sec)
            nxt_ok = i == len(route) - 1 or route[i + 1] in occ
            prev_ok = i == 0 or route[i - 1] not in occ
            if not fault and nxt_ok and prev_ok and sec not in released:
                released.append(sec)
    return {'released': released, 'fault': fault}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 6', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'clr'], ['T3', 'occ'], ['T3', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 24', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T5', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 25', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 1', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 4', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 7', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'clr'], ['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True})], [('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 25', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 55', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('control 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: phantom clear then passage', {'route': ['T1', 'T2'], 'events': [['T2', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': [], 'fault': True}), ('control 12', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('control 15', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T4', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 18', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 31', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T4', 'occ'], ['T2', 'occ'], ['T5', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4', 'T5'], 'fault': False}), ('sampled regression 79', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['Z9', 'clr'], ['T2', 'clr'], ['T4', 'occ'], ['T4', 'clr'], ['T3', 'clr'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 72', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T3', 'occ'], ['T4', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('boundary: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('control 23', {'route': ['T1', 'T2', 'T3'], 'events': [['T3', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True}), ('control 26', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T1', 'clr'], ['T4', 'clr'], ['T4', 'occ']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 29', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 44', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['Z9', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 40', {'route': ['T1', 'T2'], 'events': [['T2', 'occ'], ['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T2', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 25', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('control 34', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'occ'], ['Z9', 'clr'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 37', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4', 'T5'], 'fault': False}), ('control 43', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 46', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['Z9', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T2', 'clr'], ['T3', 'occ'], ['T3', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 63', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 45', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['Z9', 'occ'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 48', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['Z9', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('control 54', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False})]]
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 middle section is released while the section behind it is still locked.

Sections of a locked route are released in route order as the train passes. A section is released when it clears, it was seen occupied earlier, its predecessor is already released (or it is the first), and the next route section is currently occupied (or it is the last). A clear of a never-occupied route section latches a fault that stops all further release. Events for sections outside the route are ignored. Output released sections in release order and the fault flag.

split: open-access


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

N = 1
observations = []
def solve(x):
    route = x['route']
    seen = set()
    released = []
    fault = False
    occ = set()
    for sec, ev in x['events']:
        if sec not in route:
            continue
        if ev == 'occ':
            occ.add(sec)
            seen.add(sec)
        else:
            fault = sec not in seen
            occ.discard(sec)
            i = route.index(sec)
            nxt_ok = i == len(route) - 1 or route[i + 1] in occ
            prev_ok = i == 0 or route[i - 1] in released
            if not fault and nxt_ok and prev_ok and sec not in released:
                released.append(sec)
    return {'released': released, 'fault': fault}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: phantom clear then passage', {'route': ['T1', 'T2'], 'events': [['T2', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': [], 'fault': True}), ('regression: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 7', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'clr'], ['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 58', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ'], ['T2', 'occ']]}, {'released': [], 'fault': True}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 1', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 4', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 10', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 48', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['Z9', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('boundary: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('regression: phantom clear then passage', {'route': ['T1', 'T2'], 'events': [['T2', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': [], 'fault': True}), ('boundary: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('control 12', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('control 15', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T4', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 18', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: phantom clear then passage', {'route': ['T1', 'T2'], 'events': [['T2', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': [], 'fault': True}), ('regression: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 13', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T4', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['Z9', 'occ'], ['T1', 'occ'], ['T4', 'occ'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 58', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ'], ['T2', 'occ']]}, {'released': [], 'fault': True}), ('boundary: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('sampled regression 23', {'route': ['T1', 'T2', 'T3'], 'events': [['T3', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True}), ('control 26', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T1', 'clr'], ['T4', 'clr'], ['T4', 'occ']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 29', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 58', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ'], ['T2', 'occ']]}, {'released': [], 'fault': True}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('boundary: foreign section noise', {'route': ['T1', 'T2'], 'events': [['Z9', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 34', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'occ'], ['Z9', 'clr'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 37', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4', 'T5'], 'fault': False}), ('control 40', {'route': ['T1', 'T2'], 'events': [['T2', 'occ'], ['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T2', 'clr']]}, {'released': [], 'fault': False})], [('regression: phantom clear then passage', {'route': ['T1', 'T2'], 'events': [['T2', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': [], 'fault': True}), ('regression: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 19', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T4', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 58', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ'], ['T2', 'occ']]}, {'released': [], 'fault': True}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 45', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['Z9', 'occ'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('sampled regression 48', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['Z9', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('control 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False})]]
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):
    route = x['route']
    seen = set()
    released = []
    fault = False
    occ = set()
    for sec, ev in x['events']:
        if sec not in route:
            continue
        if ev == 'occ':
            occ.add(sec)
            seen.add(sec)
        else:
            if sec not in seen and sec != route[0]:
                fault = True
            occ.discard(sec)
            i = route.index(sec)
            nxt_ok = i == len(route) - 1 or route[i + 1] in occ
            prev_ok = i == 0 or route[i - 1] in released
            if not fault and nxt_ok and prev_ok and sec not in released:
                released.append(sec)
    return {'released': released, 'fault': fault}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: phantom clear then passage', {'route': ['T1', 'T2'], 'events': [['T2', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': [], 'fault': True}), ('regression: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 7', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'clr'], ['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 58', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ'], ['T2', 'occ']]}, {'released': [], 'fault': True}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 1', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 4', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 10', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 48', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['Z9', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('boundary: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('regression: phantom clear then passage', {'route': ['T1', 'T2'], 'events': [['T2', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': [], 'fault': True}), ('boundary: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('control 12', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('control 15', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T4', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 18', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: phantom clear then passage', {'route': ['T1', 'T2'], 'events': [['T2', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': [], 'fault': True}), ('regression: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 13', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T4', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['Z9', 'occ'], ['T1', 'occ'], ['T4', 'occ'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 58', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ'], ['T2', 'occ']]}, {'released': [], 'fault': True}), ('boundary: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('sampled regression 23', {'route': ['T1', 'T2', 'T3'], 'events': [['T3', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True}), ('control 26', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T1', 'clr'], ['T4', 'clr'], ['T4', 'occ']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 29', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 58', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ'], ['T2', 'occ']]}, {'released': [], 'fault': True}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('boundary: foreign section noise', {'route': ['T1', 'T2'], 'events': [['Z9', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 34', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'occ'], ['Z9', 'clr'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 37', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4', 'T5'], 'fault': False}), ('control 40', {'route': ['T1', 'T2'], 'events': [['T2', 'occ'], ['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T2', 'clr']]}, {'released': [], 'fault': False})], [('regression: phantom clear then passage', {'route': ['T1', 'T2'], 'events': [['T2', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': [], 'fault': True}), ('regression: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 19', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T4', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 58', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ'], ['T2', 'occ']]}, {'released': [], 'fault': True}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 45', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['Z9', 'occ'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('sampled regression 48', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['Z9', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('control 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False})]]
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:
Release resumes after a track circuit clears without having been occupied.

Sections of a locked route are released in route order as the train passes. A section is released when it clears, it was seen occupied earlier, its predecessor is already released (or it is the first), and the next route section is currently occupied (or it is the last). A clear of a never-occupied route section latches a fault that stops all further release. Events for sections outside the route are ignored. Output released sections in release order and the fault flag.

split: open-access


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

N = 1
observations = []
def solve(x):
    route = x['route']
    seen = set()
    released = []
    fault = False
    occ = set()
    for sec, ev in x['events']:
        if sec not in route:
            continue
        if ev == 'occ':
            occ.add(sec)
            seen.add(sec)
        else:
            if sec not in seen:
                fault = True
            occ.discard(sec)
            i = route.index(sec)
            nxt_ok = i == len(route) - 1 or route[i + 1] in occ
            prev_ok = i == 0 or route[i - 1] in released
            if not fault and nxt_ok and prev_ok :
                released.append(sec)
    return {'released': released, 'fault': fault}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('sampled regression 44', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['Z9', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 14', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T3', 'clr'], ['Z9', 'occ']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 1', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 4', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 7', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'clr'], ['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True})], [('regression: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('sampled regression 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 50', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T2', 'clr'], ['T4', 'clr'], ['T5', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4', 'T5'], 'fault': False}), ('sampled regression 44', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['Z9', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 12', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('control 15', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T4', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('sampled regression 18', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('sampled regression 71', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['Z9', 'clr'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'occ'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('sampled regression 2', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ']]}, {'released': ['T1', 'T2'], 'fault': False}), ('boundary: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('control 23', {'route': ['T1', 'T2', 'T3'], 'events': [['T3', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True}), ('control 26', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T1', 'clr'], ['T4', 'clr'], ['T4', 'occ']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 29', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('sampled regression 44', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['Z9', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 45', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['Z9', 'occ'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('sampled regression 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('control 34', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'occ'], ['Z9', 'clr'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 37', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4', 'T5'], 'fault': False}), ('control 40', {'route': ['T1', 'T2'], 'events': [['T2', 'occ'], ['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T2', 'clr']]}, {'released': [], 'fault': False})], [('regression: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('sampled regression 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 1', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('sampled regression 44', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['Z9', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 45', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['Z9', 'occ'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 48', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['Z9', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 54', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False})]]
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):
    route = x['route']
    seen = set()
    released = []
    fault = False
    occ = set()
    for sec, ev in x['events']:
        if sec not in route:
            continue
        if ev == 'occ':
            occ.add(sec)
            seen.add(sec)
        else:
            if sec not in seen:
                fault = True
            occ.discard(sec)
            i = route.index(sec)
            nxt_ok = i == len(route) - 1 or route[i + 1] in occ
            prev_ok = i == 0 or route[i - 1] in released
            if not fault and nxt_ok and prev_ok and released[-1:] != [sec]:
                released.append(sec)
    return {'released': released, 'fault': fault}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('sampled regression 44', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['Z9', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 14', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T3', 'clr'], ['Z9', 'occ']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 1', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 4', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 7', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'clr'], ['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True})], [('regression: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('sampled regression 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 50', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T2', 'clr'], ['T4', 'clr'], ['T5', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4', 'T5'], 'fault': False}), ('sampled regression 44', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['Z9', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: middle section clears before first', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('control 12', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('control 15', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T4', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('sampled regression 18', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('sampled regression 71', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['Z9', 'clr'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'occ'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('sampled regression 2', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ']]}, {'released': ['T1', 'T2'], 'fault': False}), ('boundary: phantom clear on first section', {'route': ['T1', 'T2'], 'events': [['T1', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('control 23', {'route': ['T1', 'T2', 'T3'], 'events': [['T3', 'clr'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': [], 'fault': True}), ('control 26', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T1', 'clr'], ['T4', 'clr'], ['T4', 'occ']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 29', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False})], [('regression: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('sampled regression 44', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['Z9', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 45', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['Z9', 'occ'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('sampled regression 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: single section route', {'route': ['T1'], 'events': [['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('control 34', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'occ'], ['Z9', 'clr'], ['T1', 'clr'], ['T2', 'clr'], ['Z9', 'occ']]}, {'released': ['T1', 'T2'], 'fault': False}), ('control 37', {'route': ['T1', 'T2', 'T3', 'T4', 'T5'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['T3', 'clr'], ['T5', 'occ'], ['T4', 'clr'], ['T5', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4', 'T5'], 'fault': False}), ('control 40', {'route': ['T1', 'T2'], 'events': [['T2', 'occ'], ['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T2', 'clr']]}, {'released': [], 'fault': False})], [('regression: re-occupation after release', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1'], 'fault': False}), ('sampled regression 51', {'route': ['T1', 'T2', 'T3'], 'events': [['Z9', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('sampled regression 1', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T2', 'occ'], ['Z9', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T2', 'clr']]}, {'released': ['T1', 'T2'], 'fault': False}), ('sampled regression 44', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['Z9', 'clr'], ['T3', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False}), ('boundary: next section flickered earlier', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T2', 'occ'], ['T2', 'clr'], ['T1', 'clr']]}, {'released': [], 'fault': False}), ('sampled regression 45', {'route': ['T1', 'T2', 'T3', 'T4'], 'events': [['T1', 'occ'], ['T3', 'occ'], ['T2', 'occ'], ['T1', 'clr'], ['T1', 'clr'], ['T3', 'occ'], ['T2', 'clr'], ['T4', 'occ'], ['Z9', 'occ'], ['T3', 'clr'], ['T4', 'clr']]}, {'released': ['T1', 'T2', 'T3', 'T4'], 'fault': False}), ('control 48', {'route': ['T1', 'T2'], 'events': [['T1', 'occ'], ['Z9', 'occ'], ['T2', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T1', 'occ'], ['T1', 'clr']]}, {'released': [], 'fault': True}), ('sampled regression 54', {'route': ['T1', 'T2', 'T3'], 'events': [['T1', 'occ'], ['T1', 'occ'], ['T1', 'clr'], ['T2', 'occ'], ['T1', 'clr'], ['T3', 'occ'], ['T1', 'clr'], ['T2', 'clr'], ['T3', 'clr']]}, {'released': ['T1', 'T2', 'T3'], 'fault': False})]]
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 section appears twice in the release list after being re-occupied.

Sections of a locked route are released in route order as the train passes. A section is released when it clears, it was seen occupied earlier, its predecessor is already released (or it is the first), and the next route section is currently occupied (or it is the last). A clear of a never-occupied route section latches a fault that stops all further release. Events for sections outside the route are ignored. Output released sections in release order and the fault flag.

split: open-access


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

N = 1
observations = []
def solve(x):
    count = 0
    disturbed = False
    prep = False
    swept = False
    out = []
    for ev in x['events']:
        if ev == 'in':
            count += 1
            if prep:
                swept = True
        elif ev == 'out':
            count -= 1
            if count < 0:
                count = 0
            if prep and swept and count == 0:
                prep = False
        elif ev == 'reset':
            if disturbed or count != 0:
                count = 0
                disturbed = False
                prep = True
                swept = False
        elif ev == 'query':
            if disturbed:
                out.append('disturbed')
            elif prep:
                out.append('prep')
            elif count > 0:
                out.append('occupied')
            else:
                out.append('clear')
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: under-count latches disturbance', {'events': ['out', 'query', 'in', 'query']}, ['disturbed', 'disturbed']), ('regression: under-count during preparation', {'events': ['in', 'reset', 'out', 'query']}, ['disturbed']), ('sampled regression 3', {'events': ['out', 'reset', 'out', 'in', 'query', 'in', 'query', 'out', 'query']}, ['disturbed', 'disturbed', 'disturbed']), ('boundary: reset of a miscounted section', {'events': ['in', 'in', 'out', 'reset', 'query', 'in', 'in', 'query', 'out', 'out', 'query']}, ['prep', 'prep', 'clear']), ('regression: reset of a disturbed section', {'events': ['out', 'reset', 'query', 'in', 'out', 'query']}, ['prep', 'clear']), ('sampled regression 1', {'events': ['out', 'in', 'out', 'query', 'out', 'query', 'in', 'out', 'in', 'out', 'query', 'query', 'in', 'query', 'query']}, ['disturbed', 'disturbed', 'disturbed', 'disturbed', 'disturbed', 'disturbed']), ('control 4', {'events': ['in', 'out', 'query', 'query', 'query']}, ['clear', 'clear', 'clear']), ('sampled regression 7', {'events': ['query', 'out', 'in', 'query', 'out', 'query', 'reset', 'reset', 'in', 'reset', 'query', 'reset', 'query']}, ['clear', 'disturbed', 'disturbed', 'prep', 'prep'])], [('regression: reset of a disturbed section', {'events': ['out', 'reset', 'query', 'in', 'out', 'query']}, ['prep', 'clear']), ('regression: under-count during preparation', {'events': ['in', 'reset', 'out', 'query']}, ['disturbed']), ('sampled regression 10', {'events': ['reset', 'out', 'in', 'query', 'reset', 'in', 'query']}, ['disturbed', 'prep']), ('sampled regression 64', {'events': ['out', 'out', 'in', 'query', 'query', 'reset', 'out', 'out', 'query']}, ['disturbed', 'disturbed', 'disturbed']), ('boundary: reset refused on a clear section', {'events': ['reset', 'query', 'in', 'query']}, ['clear', 'occupied']), ('sampled regression 12', {'events': ['query', 'out', 'reset', 'out', 'query']}, ['clear', 'disturbed']), ('control 15', {'events': ['out', 'out', 'in', 'reset', 'query', 'query', 'query']}, ['prep', 'prep', 'prep']), ('sampled regression 18', {'events': ['out', 'query', 'query', 'reset', 'in', 'query', 'query']}, ['disturbed', 'disturbed', 'prep', 'prep'])], [('regression: sweep train partly through', {'events': ['out', 'reset', 'in', 'in', 'out', 'query', 'out', 'query']}, ['prep', 'clear']), ('regression: under-count during preparation', {'events': ['in', 'reset', 'out', 'query']}, ['disturbed']), ('sampled regression 21', {'events': ['out', 'in', 'out', 'in', 'query', 'query', 'out', 'query', 'out', 'query', 'query']}, ['disturbed', 'disturbed', 'disturbed', 'disturbed', 'disturbed']), ('boundary: normal passage', {'events': ['in', 'in', 'query', 'out', 'out', 'query']}, ['occupied', 'clear']), ('regression: under-count latches disturbance', {'events': ['out', 'query', 'in', 'query']}, ['disturbed', 'disturbed']), ('sampled regression 23', {'events': ['query', 'out', 'query', 'out', 'in', 'in', 'in', 'query', 'out', 'in', 'in', 'query']}, ['clear', 'disturbed', 'disturbed', 'disturbed']), ('control 26', {'events': ['reset', 'query', 'in', 'query']}, ['clear', 'occupied']), ('control 29', {'events': ['out', 'in', 'in', 'out', 'out', 'in', 'reset', 'out', 'reset', 'in', 'query', 'in', 'query']}, ['prep', 'prep'])], [('regression: under-count during preparation', {'events': ['in', 'reset', 'out', 'query']}, ['disturbed']), ('sampled regression 37', {'events': ['reset', 'query', 'out', 'query', 'query', 'out', 'in', 'out', 'in', 'out', 'in', 'query']}, ['clear', 'disturbed', 'disturbed', 'disturbed']), ('sampled regression 59', {'events': ['query', 'query', 'in', 'out', 'out', 'reset', 'reset', 'out', 'in', 'query', 'reset', 'query', 'query']}, ['clear', 'clear', 'disturbed', 'prep', 'prep']), ('boundary: normal passage', {'events': ['in', 'in', 'query', 'out', 'out', 'query']}, ['occupied', 'clear']), ('regression: under-count latches disturbance', {'events': ['out', 'query', 'in', 'query']}, ['disturbed', 'disturbed']), ('control 34', {'events': ['in', 'out', 'in', 'out', 'in', 'in', 'query']}, ['occupied']), ('control 40', {'events': ['in', 'in', 'query', 'out', 'query']}, ['occupied', 'occupied']), ('sampled regression 43', {'events': ['out', 'in', 'query', 'in', 'query', 'reset', 'reset', 'out', 'reset', 'in', 'out', 'out', 'query']}, ['disturbed', 'disturbed', 'disturbed'])], [('regression: under-count latches disturbance', {'events': ['out', 'query', 'in', 'query']}, ['disturbed', 'disturbed']), ('regression: under-count during preparation', {'events': ['in', 'reset', 'out', 'query']}, ['disturbed']), ('sampled regression 50', {'events': ['out', 'out', 'in', 'query']}, ['disturbed']), ('sampled regression 75', {'events': ['out', 'out', 'out', 'reset', 'out', 'in', 'in', 'query']}, ['disturbed']), ('boundary: reset of a miscounted section', {'events': ['in', 'in', 'out', 'reset', 'query', 'in', 'in', 'query', 'out', 'out', 'query']}, ['prep', 'prep', 'clear']), ('sampled regression 45', {'events': ['out', 'query', 'out', 'in', 'query', 'in', 'in', 'in', 'reset', 'in', 'query']}, ['disturbed', 'disturbed', 'prep']), ('sampled regression 48', {'events': ['out', 'query', 'in', 'out', 'reset', 'in', 'query', 'query', 'query', 'query']}, ['disturbed', 'prep', 'prep', 'prep', 'prep']), ('control 51', {'events': ['reset', 'query', 'in', 'out', 'query', 'in', 'in', 'query', 'query', 'query', 'query']}, ['clear', 'clear', 'occupied', 'occupied', 'occupied', 'occupied'])]]
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):
    count = 0
    disturbed = False
    prep = False
    swept = False
    out = []
    for ev in x['events']:
        if ev == 'in':
            count += 1
            if prep:
                swept = True
        elif ev == 'out':
            count -= 1
            if count < 0 and not prep:
                disturbed = True
                count = 0
            if prep and swept and count == 0:
                prep = False
        elif ev == 'reset':
            if disturbed or count != 0:
                count = 0
                disturbed = False
                prep = True
                swept = False
        elif ev == 'query':
            if disturbed:
                out.append('disturbed')
            elif prep:
                out.append('prep')
            elif count > 0:
                out.append('occupied')
            else:
                out.append('clear')
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: under-count latches disturbance', {'events': ['out', 'query', 'in', 'query']}, ['disturbed', 'disturbed']), ('regression: under-count during preparation', {'events': ['in', 'reset', 'out', 'query']}, ['disturbed']), ('sampled regression 3', {'events': ['out', 'reset', 'out', 'in', 'query', 'in', 'query', 'out', 'query']}, ['disturbed', 'disturbed', 'disturbed']), ('boundary: reset of a miscounted section', {'events': ['in', 'in', 'out', 'reset', 'query', 'in', 'in', 'query', 'out', 'out', 'query']}, ['prep', 'prep', 'clear']), ('regression: reset of a disturbed section', {'events': ['out', 'reset', 'query', 'in', 'out', 'query']}, ['prep', 'clear']), ('sampled regression 1', {'events': ['out', 'in', 'out', 'query', 'out', 'query', 'in', 'out', 'in', 'out', 'query', 'query', 'in', 'query', 'query']}, ['disturbed', 'disturbed', 'disturbed', 'disturbed', 'disturbed', 'disturbed']), ('control 4', {'events': ['in', 'out', 'query', 'query', 'query']}, ['clear', 'clear', 'clear']), ('sampled regression 7', {'events': ['query', 'out', 'in', 'query', 'out', 'query', 'reset', 'reset', 'in', 'reset', 'query', 'reset', 'query']}, ['clear', 'disturbed', 'disturbed', 'prep', 'prep'])], [('regression: reset of a disturbed section', {'events': ['out', 'reset', 'query', 'in', 'out', 'query']}, ['prep', 'clear']), ('regression: under-count during preparation', {'events': ['in', 'reset', 'out', 'query']}, ['disturbed']), ('sampled regression 10', {'events': ['reset', 'out', 'in', 'query', 'reset', 'in', 'query']}, ['disturbed', 'prep']), ('sampled regression 64', {'events': ['out', 'out', 'in', 'query', 'query', 'reset', 'out', 'out', 'query']}, ['disturbed', 'disturbed', 'disturbed']), ('boundary: reset refused on a clear section', {'events': ['reset', 'query', 'in', 'query']}, ['clear', 'occupied']), ('sampled regression 12', {'events': ['query', 'out', 'reset', 'out', 'query']}, ['clear', 'disturbed']), ('control 15', {'events': ['out', 'out', 'in', 'reset', 'query', 'query', 'query']}, ['prep', 'prep', 'prep']), ('sampled regression 18', {'events': ['out', 'query', 'query', 'reset', 'in', 'query', 'query']}, ['disturbed', 'disturbed', 'prep', 'prep'])], [('regression: sweep train partly through', {'events': ['out', 'reset', 'in', 'in', 'out', 'query', 'out', 'query']}, ['prep', 'clear']), ('regression: under-count during preparation', {'events': ['in', 'reset', 'out', 'query']}, ['disturbed']), ('sampled regression 21', {'events': ['out', 'in', 'out', 'in', 'query', 'query', 'out', 'query', 'out', 'query', 'query']}, ['disturbed', 'disturbed', 'disturbed', 'disturbed', 'disturbed']), ('boundary: normal passage', {'events': ['in', 'in', 'query', 'out', 'out', 'query']}, ['occupied', 'clear']), ('regression: under-count latches disturbance', {'events': ['out', 'query', 'in', 'query']}, ['disturbed', 'disturbed']), ('sampled regression 23', {'events': ['query', 'out', 'query', 'out', 'in', 'in', 'in', 'query', 'out', 'in', 'in', 'query']}, ['clear', 'disturbed', 'disturbed', 'disturbed']), ('control 26', {'events': ['reset', 'query', 'in', 'query']}, ['clear', 'occupied']), ('control 29', {'events': ['out', 'in', 'in', 'out', 'out', 'in', 'reset', 'out', 'reset', 'in', 'query', 'in', 'query']}, ['prep', 'prep'])], [('regression: under-count during preparation', {'events': ['in', 'reset', 'out', 'query']}, ['disturbed']), ('sampled regression 37', {'events': ['reset', 'query', 'out', 'query', 'query', 'out', 'in', 'out', 'in', 'out', 'in', 'query']}, ['clear', 'disturbed', 'disturbed', 'disturbed']), ('sampled regression 59', {'events': ['query', 'query', 'in', 'out', 'out', 'reset', 'reset', 'out', 'in', 'query', 'reset', 'query', 'query']}, ['clear', 'clear', 'disturbed', 'prep', 'prep']), ('boundary: normal passage', {'events': ['in', 'in', 'query', 'out', 'out', 'query']}, ['occupied', 'clear']), ('regression: under-count latches disturbance', {'events': ['out', 'query', 'in', 'query']}, ['disturbed', 'disturbed']), ('control 34', {'events': ['in', 'out', 'in', 'out', 'in', 'in', 'query']}, ['occupied']), ('control 40', {'events': ['in', 'in', 'query', 'out', 'query']}, ['occupied', 'occupied']), ('sampled regression 43', {'events': ['out', 'in', 'query', 'in', 'query', 'reset', 'reset', 'out', 'reset', 'in', 'out', 'out', 'query']}, ['disturbed', 'disturbed', 'disturbed'])], [('regression: under-count latches disturbance', {'events': ['out', 'query', 'in', 'query']}, ['disturbed', 'disturbed']), ('regression: under-count during preparation', {'events': ['in', 'reset', 'out', 'query']}, ['disturbed']), ('sampled regression 50', {'events': ['out', 'out', 'in', 'query']}, ['disturbed']), ('sampled regression 75', {'events': ['out', 'out', 'out', 'reset', 'out', 'in', 'in', 'query']}, ['disturbed']), ('boundary: reset of a miscounted section', {'events': ['in', 'in', 'out', 'reset', 'query', 'in', 'in', 'query', 'out', 'out', 'query']}, ['prep', 'prep', 'clear']), ('sampled regression 45', {'events': ['out', 'query', 'out', 'in', 'query', 'in', 'in', 'in', 'reset', 'in', 'query']}, ['disturbed', 'disturbed', 'prep']), ('sampled regression 48', {'events': ['out', 'query', 'in', 'out', 'reset', 'in', 'query', 'query', 'query', 'query']}, ['disturbed', 'prep', 'prep', 'prep', 'prep']), ('control 51', {'events': ['reset', 'query', 'in', 'out', 'query', 'in', 'in', 'query', 'query', 'query', 'query']}, ['clear', 'clear', 'occupied', 'occupied', 'occupied', 'occupied'])]]
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 section that counted out more axles than in is reported clear.

An axle-counter section counts axles in and out. Counting out below zero latches disturbed (count forced to 0). A reset is accepted only when the section is disturbed or the count is non-zero; it zeroes the count and enters preparatory state, which ends only when a sweep train has counted in and the count returns to zero on an out-count. query reports disturbed, prep, occupied (count>0) or clear, in that precedence.

split: open-access