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

N = 1
observations = []
def solve(x):
    if x['lifesaving']:
        return {'level': 1, 'upgraded': False}
    if x['high_risk'] or x['confused'] or x['pain'] >= 7:
        return {'level': 2, 'upgraded': False}
    r, a = x['resources'], x['age_years']
    if a < 1:
        hr_lim, rr_lim = 180, 50
    elif a < 3:
        hr_lim, rr_lim = 160, 40
    elif a <= 8:
        hr_lim, rr_lim = 140, 30
    else:
        hr_lim, rr_lim = 100, 20
    danger = x['hr'] > hr_lim or x['rr'] > rr_lim or x['spo2'] < 92
    if r >= 2 and danger:
        return {'level': 2, 'upgraded': True}
    return {'level': 3 if r >= 2 else 4 if r == 1 else 5, 'upgraded': False}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 41,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 106,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 111,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 116,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 131,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 61,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 1,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 42,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 0,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 2,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 107,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 90},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 112,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 117,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 132,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 62,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 2,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 43,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 108,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 113,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 118,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 133,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 63,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 3,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 44,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 0,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 2,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 109,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 90},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 114,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 119,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 134,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 64,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 45,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 110,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 115,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 120,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 135,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 65,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 5,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})]]
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):
    if x['lifesaving']:
        return {'level': 1, 'upgraded': False}
    if x['high_risk'] or x['confused'] or x['pain'] >= 7:
        return {'level': 2, 'upgraded': False}
    r, a = x['resources'], x['age_years']
    if a < 1:
        hr_lim, rr_lim = 180, 50
    elif a < 3:
        hr_lim, rr_lim = 160, 40
    elif a < 18:
        hr_lim, rr_lim = 140, 30
    else:
        hr_lim, rr_lim = 100, 20
    danger = x['hr'] > hr_lim or x['rr'] > rr_lim or x['spo2'] < 92
    if r >= 2 and danger:
        return {'level': 2, 'upgraded': True}
    return {'level': 3 if r >= 2 else 4 if r == 1 else 5, 'upgraded': False}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 41,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 106,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 111,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 116,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 131,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 61,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 1,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 42,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 0,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 2,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 107,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 90},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 112,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 117,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 132,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 62,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 2,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 43,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 108,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 113,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 118,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 133,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 63,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 3,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 44,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 0,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 2,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 109,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 90},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 114,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 119,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 134,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 64,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 45,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 110,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 115,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 120,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 135,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 65,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 5,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})]]
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 8-year-old with HR 112 is judged against preschool limits and not upgraded.

Input {lifesaving, high_risk, confused, pain (0-10), resources, age_years, hr, rr, spo2}. Toy algorithm: lifesaving intervention -> 1; high risk, confusion or pain >= 7 -> 2; otherwise resources 0 -> 5, 1 -> 4, >= 2 -> 3, except that patients needing >= 2 resources with danger-zone vitals are upgraded to 2. Danger zone: spo2 < 92 or hr/rr above age limits (age < 1: 180/50; 1-2: 160/40; 3-7: 140/30; 8+: 100/20). Return {level, upgraded}.

split: open-access


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

N = 1
observations = []
def solve(x):
    if x['lifesaving']:
        return {'level': 1, 'upgraded': False}
    if x['high_risk'] or x['confused'] or x['pain'] >= 7:
        return {'level': 2, 'upgraded': False}
    r, a = x['resources'], x['age_years']
    if a < 1:
        hr_lim, rr_lim = 180, 50
    elif a < 3:
        hr_lim, rr_lim = 160, 40
    elif a < 8:
        hr_lim, rr_lim = 140, 30
    else:
        hr_lim, rr_lim = 100, 20
    danger = x['hr'] > hr_lim or x['rr'] > rr_lim or x['spo2'] <= 92
    if r >= 2 and danger:
        return {'level': 2, 'upgraded': True}
    return {'level': 3 if r >= 2 else 4 if r == 1 else 5, 'upgraded': False}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 41,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 106,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 111,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 116,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 131,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 61,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 1,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 42,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 0,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 2,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 107,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 90},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 112,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 117,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 132,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 62,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 2,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 43,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 108,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 113,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 118,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 133,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 63,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 3,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 44,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 0,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 2,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 109,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 90},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 114,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 119,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 134,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 64,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 45,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 110,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 115,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 120,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 135,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 65,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 5,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})]]
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):
    if x['lifesaving']:
        return {'level': 1, 'upgraded': False}
    if x['high_risk'] or x['confused'] or x['pain'] >= 7:
        return {'level': 2, 'upgraded': False}
    r, a = x['resources'], x['age_years']
    if a < 1:
        hr_lim, rr_lim = 180, 50
    elif a < 3:
        hr_lim, rr_lim = 160, 40
    elif a < 8:
        hr_lim, rr_lim = 140, 30
    else:
        hr_lim, rr_lim = 100, 20
    danger = x['hr'] > hr_lim or x['rr'] > rr_lim or (x['spo2'] < 92 and a >= 8)
    if r >= 2 and danger:
        return {'level': 2, 'upgraded': True}
    return {'level': 3 if r >= 2 else 4 if r == 1 else 5, 'upgraded': False}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 41,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 106,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 111,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 116,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 131,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 61,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 1,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 42,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 0,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 2,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 107,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 90},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 112,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 117,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 132,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 62,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 2,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 43,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 108,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 113,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 118,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 133,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 63,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 3,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 44,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 0,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 2,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 109,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 90},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 114,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 119,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 134,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 64,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 45,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 110,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 115,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 120,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 135,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 65,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 5,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})]]
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 saturation of exactly 92% upgrades the patient.

Input {lifesaving, high_risk, confused, pain (0-10), resources, age_years, hr, rr, spo2}. Toy algorithm: lifesaving intervention -> 1; high risk, confusion or pain >= 7 -> 2; otherwise resources 0 -> 5, 1 -> 4, >= 2 -> 3, except that patients needing >= 2 resources with danger-zone vitals are upgraded to 2. Danger zone: spo2 < 92 or hr/rr above age limits (age < 1: 180/50; 1-2: 160/40; 3-7: 140/30; 8+: 100/20). Return {level, upgraded}.

split: open-access


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

N = 1
observations = []
def solve(x):
    if x['lifesaving']:
        return {'level': 1, 'upgraded': False}
    if x['high_risk'] or x['confused'] or x['pain'] >= 7:
        return {'level': 2, 'upgraded': False}
    r, a = x['resources'], x['age_years']
    if a < 1:
        hr_lim, rr_lim = 180, 50
    elif a < 3:
        hr_lim, rr_lim = 160, 40
    elif a < 8:
        hr_lim, rr_lim = 140, 30
    else:
        hr_lim, rr_lim = 100, 20
    danger = x['hr'] > hr_lim or x['rr'] > rr_lim or x['spo2'] < 92
    if r >= 2 and danger:
        return {'level': 2, 'upgraded': True}
    return {'level': 3 if r >= 2 else 4 if r == 1 else 5, 'upgraded': danger}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 41,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 106,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 111,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 116,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 131,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 61,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 1,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 42,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 0,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 2,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 107,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 90},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 112,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 117,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 132,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 62,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 2,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 43,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 108,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 113,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 118,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 133,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 63,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 3,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 44,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 0,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 2,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 109,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 90},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 114,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 119,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 134,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 64,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 45,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 110,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 115,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 120,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 135,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 65,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 5,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})]]
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):
    if x['lifesaving']:
        return {'level': 1, 'upgraded': False}
    if x['high_risk'] or x['confused'] or x['pain'] >= 7:
        return {'level': 2, 'upgraded': False}
    r, a = x['resources'], x['age_years']
    if a < 1:
        hr_lim, rr_lim = 180, 50
    elif a < 3:
        hr_lim, rr_lim = 160, 40
    elif a < 8:
        hr_lim, rr_lim = 140, 30
    else:
        hr_lim, rr_lim = 100, 20
    danger = x['hr'] > hr_lim or x['rr'] > rr_lim or x['spo2'] < 92
    if r >= 2 and danger:
        return {'level': 2, 'upgraded': True}
    return {'level': 3 if r >= 2 else 4 if r == 1 else 5, 'upgraded': danger and r >= 1}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 41,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 106,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 111,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 116,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 131,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 61,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 1,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 42,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 0,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 2,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 107,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 90},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 112,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 117,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 132,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 62,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 2,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 43,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 108,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 113,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 118,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 133,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 63,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 3,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 44,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 0,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 2,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 109,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 90},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 114,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 119,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 134,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 64,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})],
 [('lifesaving',
   {'lifesaving': True,
    'high_risk': True,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 45,
    'hr': 130,
    'rr': 30,
    'spo2': 85},
   {'level': 1, 'upgraded': False}),
  ('severe pain exactly 7',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 7,
    'resources': 1,
    'age_years': 30,
    'hr': 90,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': False}),
  ('two resources normal vitals',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 4,
    'resources': 3,
    'age_years': 35,
    'hr': 88,
    'rr': 18,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('one resource tachycardic stays 4',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 2,
    'resources': 1,
    'age_years': 25,
    'hr': 110,
    'rr': 18,
    'spo2': 97},
   {'level': 4, 'upgraded': False}),
  ('no resources hypoxic stays 5',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 0,
    'resources': 0,
    'age_years': 50,
    'hr': 80,
    'rr': 18,
    'spo2': 89},
   {'level': 5, 'upgraded': False}),
  ('eight-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 8,
    'hr': 115,
    'rr': 22,
    'spo2': 97},
   {'level': 2, 'upgraded': True}),
  ('twelve-year-old uses adult limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 3,
    'age_years': 12,
    'hr': 120,
    'rr': 18,
    'spo2': 98},
   {'level': 2, 'upgraded': True}),
  ('preschool child within limits',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 4,
    'hr': 135,
    'rr': 28,
    'spo2': 97},
   {'level': 3, 'upgraded': False}),
  ('saturation exactly 92',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 65,
    'hr': 80,
    'rr': 18,
    'spo2': 92},
   {'level': 3, 'upgraded': False}),
  ('hypoxic child with two resources',
   {'lifesaving': False,
    'high_risk': False,
    'confused': False,
    'pain': 3,
    'resources': 2,
    'age_years': 5,
    'hr': 120,
    'rr': 25,
    'spo2': 90},
   {'level': 2, 'upgraded': True})]]
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:
Level 4 patients with abnormal vitals are reported as upgraded.

Input {lifesaving, high_risk, confused, pain (0-10), resources, age_years, hr, rr, spo2}. Toy algorithm: lifesaving intervention -> 1; high risk, confusion or pain >= 7 -> 2; otherwise resources 0 -> 5, 1 -> 4, >= 2 -> 3, except that patients needing >= 2 resources with danger-zone vitals are upgraded to 2. Danger zone: spo2 < 92 or hr/rr above age limits (age < 1: 180/50; 1-2: 160/40; 3-7: 140/30; 8+: 100/20). Return {level, upgraded}.

split: open-access