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

N = 1
observations = []
def solve(x):
    target = {1: 0, 2: 10, 3: 30, 4: 60, 5: 120}
    rows, overdue = [], []
    for p in x['patients']:
        lvl = p['level']
        wait = x['now'] - p['arrival']
        pr = lvl
        if wait > target[lvl]:
            overdue.append(p['id'])
            pr = max(1, lvl - 1)
        rows.append((pr, p['arrival'], p['id']))
    rows.sort()
    return {'order': [r[2] for r in rows], 'overdue': sorted(overdue)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 91, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 81, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 79, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 93, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 98, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 91, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 194, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 11, 'retriage': None}]},
   {'order': ['c', 'a1', 'b1'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 92, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 82, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 78, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 94, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 97, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 92, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 193, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 12, 'retriage': None}]},
   {'order': ['c', 'a2', 'b2'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 93, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 83, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 77, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 96, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 93, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 192, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 13, 'retriage': None}]},
   {'order': ['c', 'a3', 'b3'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 94, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 84, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 76, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 96, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 191, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 14, 'retriage': None}]},
   {'order': ['c', 'a4', 'b4'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 85, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 75, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 97, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 190, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 15, 'retriage': None}]},
   {'order': ['c', 'a5', 'b5'], 'overdue': []})]]
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):
    target = {1: 0, 2: 10, 3: 30, 4: 60, 5: 120}
    rows, overdue = [], []
    for p in x['patients']:
        lvl = min(p['level'], p['retriage'] or 9)
        wait = x['now'] - p['arrival']
        pr = lvl
        if wait > target[lvl]:
            overdue.append(p['id'])
            pr = max(1, lvl - 1)
        rows.append((pr, p['arrival'], p['id']))
    rows.sort()
    return {'order': [r[2] for r in rows], 'overdue': sorted(overdue)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 91, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 81, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 79, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 93, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 98, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 91, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 194, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 11, 'retriage': None}]},
   {'order': ['c', 'a1', 'b1'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 92, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 82, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 78, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 94, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 97, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 92, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 193, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 12, 'retriage': None}]},
   {'order': ['c', 'a2', 'b2'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 93, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 83, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 77, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 96, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 93, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 192, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 13, 'retriage': None}]},
   {'order': ['c', 'a3', 'b3'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 94, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 84, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 76, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 96, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 191, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 14, 'retriage': None}]},
   {'order': ['c', 'a4', 'b4'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 85, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 75, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 97, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 190, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 15, 'retriage': None}]},
   {'order': ['c', 'a5', 'b5'], 'overdue': []})]]
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 patient re-triaged after a change in condition keeps their arrival level.

Input {now, patients [{id, level, arrival, retriage (or null)}]}. Effective level = retriage when present (up or down), else level. Target waits: 1:0, 2:10, 3:30, 4:60, 5:120 minutes. A patient whose wait strictly exceeds the target of the effective level is overdue and moves up one priority (never above 1). Order by (priority, arrival, id). Return {order, overdue (sorted ids)}.

split: open-access


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

N = 1
observations = []
def solve(x):
    target = {1: 0, 2: 10, 3: 30, 4: 60, 5: 120}
    rows, overdue = [], []
    for p in x['patients']:
        lvl = p['retriage'] if p['retriage'] is not None else p['level']
        wait = x['now'] - p['arrival']
        pr = lvl
        if wait > target[p['level']]:
            overdue.append(p['id'])
            pr = max(1, lvl - 1)
        rows.append((pr, p['arrival'], p['id']))
    rows.sort()
    return {'order': [r[2] for r in rows], 'overdue': sorted(overdue)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 91, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 81, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 79, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 93, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 98, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 91, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 194, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 11, 'retriage': None}]},
   {'order': ['c', 'a1', 'b1'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 92, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 82, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 78, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 94, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 97, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 92, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 193, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 12, 'retriage': None}]},
   {'order': ['c', 'a2', 'b2'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 93, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 83, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 77, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 96, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 93, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 192, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 13, 'retriage': None}]},
   {'order': ['c', 'a3', 'b3'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 94, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 84, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 76, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 96, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 191, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 14, 'retriage': None}]},
   {'order': ['c', 'a4', 'b4'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 85, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 75, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 97, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 190, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 15, 'retriage': None}]},
   {'order': ['c', 'a5', 'b5'], 'overdue': []})]]
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):
    target = {1: 0, 2: 10, 3: 30, 4: 60, 5: 120}
    rows, overdue = [], []
    for p in x['patients']:
        lvl = p['retriage'] if p['retriage'] is not None else p['level']
        wait = x['now'] - p['arrival']
        pr = lvl
        if wait > target[min(lvl, p['level'])]:
            overdue.append(p['id'])
            pr = max(1, lvl - 1)
        rows.append((pr, p['arrival'], p['id']))
    rows.sort()
    return {'order': [r[2] for r in rows], 'overdue': sorted(overdue)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 91, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 81, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 79, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 93, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 98, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 91, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 194, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 11, 'retriage': None}]},
   {'order': ['c', 'a1', 'b1'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 92, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 82, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 78, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 94, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 97, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 92, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 193, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 12, 'retriage': None}]},
   {'order': ['c', 'a2', 'b2'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 93, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 83, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 77, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 96, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 93, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 192, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 13, 'retriage': None}]},
   {'order': ['c', 'a3', 'b3'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 94, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 84, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 76, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 96, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 191, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 14, 'retriage': None}]},
   {'order': ['c', 'a4', 'b4'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 85, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 75, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 97, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 190, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 15, 'retriage': None}]},
   {'order': ['c', 'a5', 'b5'], 'overdue': []})]]
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 downgraded patient is flagged overdue against the old, shorter target.

Input {now, patients [{id, level, arrival, retriage (or null)}]}. Effective level = retriage when present (up or down), else level. Target waits: 1:0, 2:10, 3:30, 4:60, 5:120 minutes. A patient whose wait strictly exceeds the target of the effective level is overdue and moves up one priority (never above 1). Order by (priority, arrival, id). Return {order, overdue (sorted ids)}.

split: open-access


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

N = 1
observations = []
def solve(x):
    target = {1: 0, 2: 10, 3: 30, 4: 60, 5: 120}
    rows, overdue = [], []
    for p in x['patients']:
        lvl = p['retriage'] if p['retriage'] is not None else p['level']
        wait = x['now'] - p['arrival']
        pr = lvl
        if wait >= target[lvl]:
            overdue.append(p['id'])
            pr = max(1, lvl - 1)
        rows.append((pr, p['arrival'], p['id']))
    rows.sort()
    return {'order': [r[2] for r in rows], 'overdue': sorted(overdue)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 91, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 81, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 79, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 93, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 98, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 91, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 194, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 11, 'retriage': None}]},
   {'order': ['c', 'a1', 'b1'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 92, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 82, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 78, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 94, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 97, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 92, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 193, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 12, 'retriage': None}]},
   {'order': ['c', 'a2', 'b2'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 93, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 83, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 77, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 96, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 93, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 192, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 13, 'retriage': None}]},
   {'order': ['c', 'a3', 'b3'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 94, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 84, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 76, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 96, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 191, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 14, 'retriage': None}]},
   {'order': ['c', 'a4', 'b4'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 85, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 75, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 97, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 190, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 15, 'retriage': None}]},
   {'order': ['c', 'a5', 'b5'], 'overdue': []})]]
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):
    target = {1: 0, 2: 10, 3: 30, 4: 60, 5: 120}
    rows, overdue = [], []
    for p in x['patients']:
        lvl = p['retriage'] if p['retriage'] is not None else p['level']
        wait = x['now'] - p['arrival']
        pr = lvl
        if wait > target[lvl] and lvl > 1:
            overdue.append(p['id'])
            pr = max(1, lvl - 1)
        rows.append((pr, p['arrival'], p['id']))
    rows.sort()
    return {'order': [r[2] for r in rows], 'overdue': sorted(overdue)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 91, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 81, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 79, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 93, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 98, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 91, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 194, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 11, 'retriage': None}]},
   {'order': ['c', 'a1', 'b1'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 92, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 82, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 78, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 94, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 97, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 92, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 193, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 12, 'retriage': None}]},
   {'order': ['c', 'a2', 'b2'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 93, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 83, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 77, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 96, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 93, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 192, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 13, 'retriage': None}]},
   {'order': ['c', 'a3', 'b3'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 94, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 84, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 76, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 96, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 191, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 14, 'retriage': None}]},
   {'order': ['c', 'a4', 'b4'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 85, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 75, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 97, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 190, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 15, 'retriage': None}]},
   {'order': ['c', 'a5', 'b5'], 'overdue': []})]]
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:
Patients exactly at their target wait are flagged overdue, including level 1 patients just arrived.

Input {now, patients [{id, level, arrival, retriage (or null)}]}. Effective level = retriage when present (up or down), else level. Target waits: 1:0, 2:10, 3:30, 4:60, 5:120 minutes. A patient whose wait strictly exceeds the target of the effective level is overdue and moves up one priority (never above 1). Order by (priority, arrival, id). Return {order, overdue (sorted ids)}.

split: open-access


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

N = 1
observations = []
def solve(x):
    target = {1: 0, 2: 10, 3: 30, 4: 60, 5: 120}
    rows, overdue = [], []
    for p in x['patients']:
        lvl = p['retriage'] if p['retriage'] is not None else p['level']
        wait = x['now'] - p['arrival']
        pr = lvl
        if wait > target[lvl]:
            overdue.append(p['id'])
            pr = lvl - 1
        rows.append((pr, p['arrival'], p['id']))
    rows.sort()
    return {'order': [r[2] for r in rows], 'overdue': sorted(overdue)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 91, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 81, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 79, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 93, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 98, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 91, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 194, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 11, 'retriage': None}]},
   {'order': ['c', 'a1', 'b1'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 92, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 82, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 78, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 94, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 97, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 92, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 193, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 12, 'retriage': None}]},
   {'order': ['c', 'a2', 'b2'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 93, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 83, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 77, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 96, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 93, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 192, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 13, 'retriage': None}]},
   {'order': ['c', 'a3', 'b3'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 94, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 84, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 76, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 96, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 191, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 14, 'retriage': None}]},
   {'order': ['c', 'a4', 'b4'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 85, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 75, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 97, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 190, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 15, 'retriage': None}]},
   {'order': ['c', 'a5', 'b5'], 'overdue': []})]]
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):
    target = {1: 0, 2: 10, 3: 30, 4: 60, 5: 120}
    rows, overdue = [], []
    for p in x['patients']:
        lvl = p['retriage'] if p['retriage'] is not None else p['level']
        wait = x['now'] - p['arrival']
        pr = lvl
        if wait > target[lvl]:
            overdue.append(p['id'])
            pr = lvl - 1 if lvl > 2 else lvl
        rows.append((pr, p['arrival'], p['id']))
    rows.sort()
    return {'order': [r[2] for r in rows], 'overdue': sorted(overdue)}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 91, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 81, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 79, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 93, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 98, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 91, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 194, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a1', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 11, 'retriage': None}]},
   {'order': ['c', 'a1', 'b1'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 92, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 82, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 78, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 94, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 97, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 92, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 193, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a2', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 12, 'retriage': None}]},
   {'order': ['c', 'a2', 'b2'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 93, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 83, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 77, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 96, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 93, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 192, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a3', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 13, 'retriage': None}]},
   {'order': ['c', 'a3', 'b3'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 94, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 84, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 76, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 96, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 191, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a4', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 14, 'retriage': None}]},
   {'order': ['c', 'a4', 'b4'], 'overdue': []})],
 [('downgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 95, 'retriage': 4},
                 {'id': 'b', 'level': 3, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('upgraded on re-triage',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 4, 'arrival': 90, 'retriage': 2},
                 {'id': 'b', 'level': 3, 'arrival': 85, 'retriage': None}]},
   {'order': ['a', 'b'], 'overdue': []}),
  ('downgraded patient overdue by new target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 2, 'arrival': 75, 'retriage': 3},
                 {'id': 'b', 'level': 2, 'arrival': 95, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': []}),
  ('wait exactly at target',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 3, 'arrival': 70, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 97, 'retriage': None},
                 {'id': 'c', 'level': 1, 'arrival': 100, 'retriage': None}]},
   {'order': ['c', 'b', 'a'], 'overdue': []}),
  ('level one waiting',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 94, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 50, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated level two ties with level one',
   {'now': 100,
    'patients': [{'id': 'a', 'level': 1, 'arrival': 95, 'retriage': None},
                 {'id': 'b', 'level': 2, 'arrival': 80, 'retriage': None}]},
   {'order': ['b', 'a'], 'overdue': ['a', 'b']}),
  ('escalated three ties with native two',
   {'now': 200,
    'patients': [{'id': 'x', 'level': 2, 'arrival': 190, 'retriage': None},
                 {'id': 'y', 'level': 3, 'arrival': 150, 'retriage': None}]},
   {'order': ['y', 'x'], 'overdue': ['y']}),
  ('same arrival id tie-break',
   {'now': 60,
    'patients': [{'id': 'b5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'a5', 'level': 4, 'arrival': 30, 'retriage': None},
                 {'id': 'c', 'level': 4, 'arrival': 15, 'retriage': None}]},
   {'order': ['c', 'a5', 'b5'], 'overdue': []})]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
An overdue level 1 patient gets priority 0 and jumps ahead of earlier arrivals at priority 1.

Input {now, patients [{id, level, arrival, retriage (or null)}]}. Effective level = retriage when present (up or down), else level. Target waits: 1:0, 2:10, 3:30, 4:60, 5:120 minutes. A patient whose wait strictly exceeds the target of the effective level is overdue and moves up one priority (never above 1). Order by (priority, arrival, id). Return {order, overdue (sorted ids)}.

split: open-access