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

N = 1
observations = []
def solve(profiles, t, session_start, hw_max):
    best = {}
    for p in profiles:
        if not (p['valid_from'] <= t and (p['valid_to'] is None or t < p['valid_to'])):
            continue
        if p['kind'] == 'relative':
            if session_start is None:
                continue
            origin = session_start
        else:
            origin = p['valid_from']
        el = t - origin
        lim = None
        for off, val in sorted(p['periods']):
            if off <= el:
                lim = val
        if lim is None:
            continue
        cur = best.get(p['purpose'])
        if cur is None or p['stack'] > cur[0]:
            best[p['purpose']] = [p['stack'], lim]
    cands = []
    if 'max' in best:
        cands.append(['max', best['max'][1]])
    if 'tx' in best:
        cands.append(['tx', best['tx'][1]])
    elif 'default' in best:
        cands.append(['default', best['default'][1]])
    cands.append(['hw', hw_max])
    win = cands[0]
    for c in cands[1:]:
        if c[1] <= win[1]:
            win = c
    return [win[1], win[0]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: bounding source tie order',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 11,
      'valid_to': 58,
      'kind': 'absolute',
      'periods': [[20, 32], [30, 20]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 23,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [20, 16]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 57,
      'valid_to': 98,
      'kind': 'relative',
      'periods': [[10, 6], [0, 10], [0, 20]]}],
    100, 56, 16],
   [16, 'tx']],
  ['regression: bounding source tie order (partial repair)',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 45,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 32], [45, 20], [5, 16]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 58,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 10], [45, 32], [0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 42,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 10], [0, 32]]}],
    56, None, 24],
   [24, 'hw']],
  ['control 1',
   [[{'purpose': 'max',
      'stack': 1,
      'valid_from': 44,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 10], [0, 10], [5, 10]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 7,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 10], [30, 20]]}],
    3, None, 24],
   [24, 'hw']],
  ['control 2',
   [[{'purpose': 'max',
      'stack': 2,
      'valid_from': 11,
      'valid_to': 21,
      'kind': 'relative',
      'periods': [[30, 32], [10, 20], [45, 6]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 14,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 32]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 19,
      'valid_to': 56,
      'kind': 'absolute',
      'periods': [[0, 6], [45, 10], [20, 16]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 53,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 10], [0, 32], [0, 20]]}],
    97, None, 16],
   [16, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: bounding source tie order',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 1,
      'valid_to': 59,
      'kind': 'absolute',
      'periods': [[20, 10]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 3,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 32]]}],
    69, 16, 32],
   [32, 'default']],
  ['regression: bounding source tie order (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 60,
      'valid_to': 108,
      'kind': 'absolute',
      'periods': [[45, 16]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 29,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[45, 20]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 35,
      'valid_to': 81,
      'kind': 'relative',
      'periods': [[5, 16]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 19,
      'valid_to': 86,
      'kind': 'absolute',
      'periods': [[10, 16], [30, 32]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 50,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [5, 10], [10, 10]]}],
    55, None, 24],
   [24, 'hw']],
  ['control 1',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 20,
      'valid_to': 56,
      'kind': 'relative',
      'periods': [[20, 10], [10, 20]]}],
    53, 38, 24],
   [20, 'default']],
  ['control 2',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 43,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 32]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 44,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 32], [45, 6], [20, 20]]}],
    88, 57, 24],
   [24, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: bounding source tie order',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 10], [0, 32], [45, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 31,
      'valid_to': 83,
      'kind': 'relative',
      'periods': [[5, 16], [45, 16], [0, 32]]}],
    42, 37, 32],
   [32, 'tx']],
  ['regression: bounding source tie order (partial repair)',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 5,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 6]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 56,
      'valid_to': 120,
      'kind': 'absolute',
      'periods': [[45, 32], [45, 16]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 56,
      'valid_to': 77,
      'kind': 'absolute',
      'periods': [[10, 10], [0, 32]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 4,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 16], [20, 16], [5, 6]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 53,
      'valid_to': 121,
      'kind': 'relative',
      'periods': [[0, 32], [0, 6]]}],
    58, 13, 16],
   [16, 'hw']],
  ['control 1',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 29,
      'valid_to': 52,
      'kind': 'absolute',
      'periods': [[30, 16], [45, 32], [0, 16]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 13,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [0, 10], [45, 16]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 58,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 16]]}],
    74, None, 24],
   [16, 'max']],
  ['control 2',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 8,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 6], [10, 6], [20, 32]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 59,
      'valid_to': 85,
      'kind': 'absolute',
      'periods': [[5, 6], [5, 20], [30, 20]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 29,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 10], [30, 10]]}],
    15, 48, 16],
   [16, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: bounding source tie order',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 31,
      'valid_to': 109,
      'kind': 'absolute',
      'periods': [[0, 6], [0, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 45,
      'valid_to': 77,
      'kind': 'relative',
      'periods': [[0, 32], [0, 16], [45, 10]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 34,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 16], [45, 10]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 8,
      'valid_to': 63,
      'kind': 'absolute',
      'periods': [[0, 20], [45, 32]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 51,
      'valid_to': 89,
      'kind': 'absolute',
      'periods': [[5, 32], [30, 6]]}],
    38, None, 32],
   [20, 'max']],
  ['regression: bounding source tie order (partial repair)',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 12,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 16], [5, 6]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 49,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 16], [0, 32], [20, 16]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 7,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 16], [10, 20], [45, 10]]}],
    58, None, 16],
   [16, 'hw']],
  ['control 1',
   [[{'purpose': 'max',
      'stack': 2,
      'valid_from': 22,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[30, 6]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 34,
      'valid_to': 106,
      'kind': 'absolute',
      'periods': [[30, 16], [30, 10]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 11,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 6], [30, 20]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 26,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[5, 6], [5, 6], [45, 10]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 12,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 32], [20, 6]]}],
    77, None, 24],
   [20, 'tx']],
  ['control 2',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 55,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[5, 10], [30, 10]]}],
    25, 14, 16],
   [16, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: bounding source tie order',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 18,
      'valid_to': 60,
      'kind': 'absolute',
      'periods': [[5, 6]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 47,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[20, 20], [0, 32], [20, 32]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 15,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 16], [0, 6], [0, 16]]}],
    100, 43, 32],
   [32, 'tx']],
  ['regression: bounding source tie order (partial repair)',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 6,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 32], [10, 16]]},
     {'purpose': 'max',
      'stack': 2,
      'valid_from': 18,
      'valid_to': 73,
      'kind': 'relative',
      'periods': [[30, 32], [0, 6]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 12,
      'valid_to': 36,
      'kind': 'relative',
      'periods': [[45, 16], [45, 20], [10, 6]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 2,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 32], [0, 6]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 13,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    73, 46, 16],
   [16, 'hw']],
  ['control 1',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 8,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 6]]}],
    71, 15, 16],
   [6, 'tx']],
  ['control 2',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 39,
      'valid_to': 108,
      'kind': 'absolute',
      'periods': [[20, 16], [30, 32], [20, 20]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 45,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[5, 20]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 21,
      'valid_to': 46,
      'kind': 'absolute',
      'periods': [[5, 20]]}],
    34, None, 24],
   [20, 'default']]]]
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(profiles, t, session_start, hw_max):
    best = {}
    for p in profiles:
        if not (p['valid_from'] <= t and (p['valid_to'] is None or t < p['valid_to'])):
            continue
        if p['kind'] == 'relative':
            if session_start is None:
                continue
            origin = session_start
        else:
            origin = p['valid_from']
        el = t - origin
        lim = None
        for off, val in sorted(p['periods']):
            if off <= el:
                lim = val
        if lim is None:
            continue
        cur = best.get(p['purpose'])
        if cur is None or p['stack'] > cur[0]:
            best[p['purpose']] = [p['stack'], lim]
    cands = []
    if 'max' in best:
        cands.append(['max', best['max'][1]])
    if 'tx' in best:
        cands.append(['tx', best['tx'][1]])
    elif 'default' in best:
        cands.append(['default', best['default'][1]])
    cands.append(['hw', hw_max])
    win = cands[0]
    for c in cands[1:]:
        if c[1] <= win[1] and c[0] != 'hw':
            win = c
    return [win[1], win[0]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: bounding source tie order',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 11,
      'valid_to': 58,
      'kind': 'absolute',
      'periods': [[20, 32], [30, 20]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 23,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [20, 16]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 57,
      'valid_to': 98,
      'kind': 'relative',
      'periods': [[10, 6], [0, 10], [0, 20]]}],
    100, 56, 16],
   [16, 'tx']],
  ['regression: bounding source tie order (partial repair)',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 45,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 32], [45, 20], [5, 16]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 58,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 10], [45, 32], [0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 42,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 10], [0, 32]]}],
    56, None, 24],
   [24, 'hw']],
  ['control 1',
   [[{'purpose': 'max',
      'stack': 1,
      'valid_from': 44,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 10], [0, 10], [5, 10]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 7,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 10], [30, 20]]}],
    3, None, 24],
   [24, 'hw']],
  ['control 2',
   [[{'purpose': 'max',
      'stack': 2,
      'valid_from': 11,
      'valid_to': 21,
      'kind': 'relative',
      'periods': [[30, 32], [10, 20], [45, 6]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 14,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 32]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 19,
      'valid_to': 56,
      'kind': 'absolute',
      'periods': [[0, 6], [45, 10], [20, 16]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 53,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 10], [0, 32], [0, 20]]}],
    97, None, 16],
   [16, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: bounding source tie order',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 1,
      'valid_to': 59,
      'kind': 'absolute',
      'periods': [[20, 10]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 3,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 32]]}],
    69, 16, 32],
   [32, 'default']],
  ['regression: bounding source tie order (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 60,
      'valid_to': 108,
      'kind': 'absolute',
      'periods': [[45, 16]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 29,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[45, 20]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 35,
      'valid_to': 81,
      'kind': 'relative',
      'periods': [[5, 16]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 19,
      'valid_to': 86,
      'kind': 'absolute',
      'periods': [[10, 16], [30, 32]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 50,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [5, 10], [10, 10]]}],
    55, None, 24],
   [24, 'hw']],
  ['control 1',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 20,
      'valid_to': 56,
      'kind': 'relative',
      'periods': [[20, 10], [10, 20]]}],
    53, 38, 24],
   [20, 'default']],
  ['control 2',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 43,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 32]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 44,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 32], [45, 6], [20, 20]]}],
    88, 57, 24],
   [24, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: bounding source tie order',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 10], [0, 32], [45, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 31,
      'valid_to': 83,
      'kind': 'relative',
      'periods': [[5, 16], [45, 16], [0, 32]]}],
    42, 37, 32],
   [32, 'tx']],
  ['regression: bounding source tie order (partial repair)',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 5,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 6]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 56,
      'valid_to': 120,
      'kind': 'absolute',
      'periods': [[45, 32], [45, 16]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 56,
      'valid_to': 77,
      'kind': 'absolute',
      'periods': [[10, 10], [0, 32]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 4,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 16], [20, 16], [5, 6]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 53,
      'valid_to': 121,
      'kind': 'relative',
      'periods': [[0, 32], [0, 6]]}],
    58, 13, 16],
   [16, 'hw']],
  ['control 1',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 29,
      'valid_to': 52,
      'kind': 'absolute',
      'periods': [[30, 16], [45, 32], [0, 16]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 13,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [0, 10], [45, 16]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 58,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 16]]}],
    74, None, 24],
   [16, 'max']],
  ['control 2',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 8,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 6], [10, 6], [20, 32]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 59,
      'valid_to': 85,
      'kind': 'absolute',
      'periods': [[5, 6], [5, 20], [30, 20]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 29,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 10], [30, 10]]}],
    15, 48, 16],
   [16, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: bounding source tie order',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 31,
      'valid_to': 109,
      'kind': 'absolute',
      'periods': [[0, 6], [0, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 45,
      'valid_to': 77,
      'kind': 'relative',
      'periods': [[0, 32], [0, 16], [45, 10]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 34,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 16], [45, 10]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 8,
      'valid_to': 63,
      'kind': 'absolute',
      'periods': [[0, 20], [45, 32]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 51,
      'valid_to': 89,
      'kind': 'absolute',
      'periods': [[5, 32], [30, 6]]}],
    38, None, 32],
   [20, 'max']],
  ['regression: bounding source tie order (partial repair)',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 12,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 16], [5, 6]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 49,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 16], [0, 32], [20, 16]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 7,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 16], [10, 20], [45, 10]]}],
    58, None, 16],
   [16, 'hw']],
  ['control 1',
   [[{'purpose': 'max',
      'stack': 2,
      'valid_from': 22,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[30, 6]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 34,
      'valid_to': 106,
      'kind': 'absolute',
      'periods': [[30, 16], [30, 10]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 11,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 6], [30, 20]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 26,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[5, 6], [5, 6], [45, 10]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 12,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 32], [20, 6]]}],
    77, None, 24],
   [20, 'tx']],
  ['control 2',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 55,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[5, 10], [30, 10]]}],
    25, 14, 16],
   [16, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: bounding source tie order',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 18,
      'valid_to': 60,
      'kind': 'absolute',
      'periods': [[5, 6]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 47,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[20, 20], [0, 32], [20, 32]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 15,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 16], [0, 6], [0, 16]]}],
    100, 43, 32],
   [32, 'tx']],
  ['regression: bounding source tie order (partial repair)',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 6,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 32], [10, 16]]},
     {'purpose': 'max',
      'stack': 2,
      'valid_from': 18,
      'valid_to': 73,
      'kind': 'relative',
      'periods': [[30, 32], [0, 6]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 12,
      'valid_to': 36,
      'kind': 'relative',
      'periods': [[45, 16], [45, 20], [10, 6]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 2,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 32], [0, 6]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 13,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    73, 46, 16],
   [16, 'hw']],
  ['control 1',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 8,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 6]]}],
    71, 15, 16],
   [6, 'tx']],
  ['control 2',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 39,
      'valid_to': 108,
      'kind': 'absolute',
      'periods': [[20, 16], [30, 32], [20, 20]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 45,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[5, 20]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 21,
      'valid_to': 46,
      'kind': 'absolute',
      'periods': [[5, 20]]}],
    34, None, 24],
   [20, 'default']]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The reported bounding source is the hardware limit even though a max profile equals it.

Each profile has purpose max/default/tx, stack level, validity [valid_from, valid_to) (valid_to None = open), kind absolute (origin valid_from) or relative (origin session_start; skipped when no session) and periods [[offset, amps]] in any order. A profile yields the amps of the last period with offset <= t-origin, or nothing before its first period. Per purpose the highest stack wins (ties: earlier profile). tx replaces default. Effective limit is the minimum of max, tx-or-default and hw_max, ties resolved in that order. Return [amps, source].

split: open-access


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

N = 1
observations = []
def solve(profiles, t, session_start, hw_max):
    best = {}
    for p in profiles:
        if not (p['valid_from'] <= t and (p['valid_to'] is None or t < p['valid_to'])):
            continue
        if p['kind'] == 'relative':
            if session_start is None:
                session_start = 0
            origin = session_start
        else:
            origin = p['valid_from']
        el = t - origin
        lim = None
        for off, val in sorted(p['periods']):
            if off <= el:
                lim = val
        if lim is None:
            continue
        cur = best.get(p['purpose'])
        if cur is None or p['stack'] > cur[0]:
            best[p['purpose']] = [p['stack'], lim]
    cands = []
    if 'max' in best:
        cands.append(['max', best['max'][1]])
    if 'tx' in best:
        cands.append(['tx', best['tx'][1]])
    elif 'default' in best:
        cands.append(['default', best['default'][1]])
    cands.append(['hw', hw_max])
    win = cands[0]
    for c in cands[1:]:
        if c[1] < win[1]:
            win = c
    return [win[1], win[0]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 45,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 32], [45, 20], [5, 16]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 58,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 10], [45, 32], [0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 42,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 10], [0, 32]]}],
    56, None, 24],
   [24, 'hw']],
  ['regression: relative profile without session (partial repair)',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 37,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 20], [30, 32]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 57,
      'valid_to': 117,
      'kind': 'absolute',
      'periods': [[5, 6], [10, 16]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 32,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 10]]},
     {'purpose': 'max',
      'stack': 2,
      'valid_from': 38,
      'valid_to': 95,
      'kind': 'absolute',
      'periods': [[45, 6], [45, 20]]}],
    64, None, 32],
   [6, 'default']],
  ['control 1',
   [[{'purpose': 'max',
      'stack': 1,
      'valid_from': 44,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 10], [0, 10], [5, 10]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 7,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 10], [30, 20]]}],
    3, None, 24],
   [24, 'hw']],
  ['control 2',
   [[{'purpose': 'max',
      'stack': 2,
      'valid_from': 11,
      'valid_to': 21,
      'kind': 'relative',
      'periods': [[30, 32], [10, 20], [45, 6]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 14,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 32]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 19,
      'valid_to': 56,
      'kind': 'absolute',
      'periods': [[0, 6], [45, 10], [20, 16]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 53,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 10], [0, 32], [0, 20]]}],
    97, None, 16],
   [16, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: relative profile without session',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 60,
      'valid_to': 108,
      'kind': 'absolute',
      'periods': [[45, 16]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 29,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[45, 20]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 35,
      'valid_to': 81,
      'kind': 'relative',
      'periods': [[5, 16]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 19,
      'valid_to': 86,
      'kind': 'absolute',
      'periods': [[10, 16], [30, 32]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 50,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [5, 10], [10, 10]]}],
    55, None, 24],
   [24, 'hw']],
  ['regression: relative profile without session (partial repair)',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 37,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 16], [20, 6]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 43,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 6], [45, 6]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 4,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 10], [45, 16]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 39,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 16]]}],
    5, None, 16],
   [16, 'hw']],
  ['control 1',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 20,
      'valid_to': 56,
      'kind': 'relative',
      'periods': [[20, 10], [10, 20]]}],
    53, 38, 24],
   [20, 'default']],
  ['control 2',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 43,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 32]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 44,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 32], [45, 6], [20, 20]]}],
    88, 57, 24],
   [24, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: relative profile without session',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 29,
      'valid_to': 101,
      'kind': 'relative',
      'periods': [[20, 16]]}],
    50, None, 32],
   [32, 'hw']],
  ['regression: relative profile without session (partial repair)',
   [[{'purpose': 'max',
      'stack': 1,
      'valid_from': 60,
      'valid_to': 84,
      'kind': 'absolute',
      'periods': [[20, 10], [20, 6]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 19,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 20], [10, 16]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 45,
      'valid_to': 120,
      'kind': 'relative',
      'periods': [[30, 16], [5, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 34,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 16]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 31,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 20], [0, 10]]}],
    90, None, 16],
   [16, 'tx']],
  ['control 1',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 29,
      'valid_to': 52,
      'kind': 'absolute',
      'periods': [[30, 16], [45, 32], [0, 16]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 13,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [0, 10], [45, 16]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 58,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 16]]}],
    74, None, 24],
   [16, 'max']],
  ['control 2',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 8,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 6], [10, 6], [20, 32]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 59,
      'valid_to': 85,
      'kind': 'absolute',
      'periods': [[5, 6], [5, 20], [30, 20]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 29,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 10], [30, 10]]}],
    15, 48, 16],
   [16, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: relative profile without session',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 12,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 16], [5, 6]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 49,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 16], [0, 32], [20, 16]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 7,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 16], [10, 20], [45, 10]]}],
    58, None, 16],
   [16, 'hw']],
  ['regression: relative profile without session (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 39,
      'valid_to': 47,
      'kind': 'relative',
      'periods': [[20, 20]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 43,
      'valid_to': 101,
      'kind': 'relative',
      'periods': [[5, 32]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 1,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 32], [30, 16], [0, 20]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 39,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 16]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 59,
      'valid_to': 64,
      'kind': 'absolute',
      'periods': [[0, 16], [5, 16]]}],
    8, None, 32],
   [32, 'hw']],
  ['control 1',
   [[{'purpose': 'max',
      'stack': 2,
      'valid_from': 22,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[30, 6]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 34,
      'valid_to': 106,
      'kind': 'absolute',
      'periods': [[30, 16], [30, 10]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 11,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 6], [30, 20]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 26,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[5, 6], [5, 6], [45, 10]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 12,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 32], [20, 6]]}],
    77, None, 24],
   [20, 'tx']],
  ['control 2',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 55,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[5, 10], [30, 10]]}],
    25, 14, 16],
   [16, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: relative profile without session',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 8,
      'valid_to': 18,
      'kind': 'relative',
      'periods': [[20, 6]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 21,
      'valid_to': 76,
      'kind': 'relative',
      'periods': [[10, 16]]}],
    57, None, 16],
   [16, 'hw']],
  ['regression: relative profile without session (partial repair)',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 30,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 10], [45, 20]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 36,
      'valid_to': 103,
      'kind': 'absolute',
      'periods': [[20, 32], [0, 16]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 1,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 10], [0, 16], [20, 16]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 45,
      'valid_to': 92,
      'kind': 'relative',
      'periods': [[45, 10], [20, 20], [10, 20]]}],
    69, None, 24],
   [24, 'hw']],
  ['control 1',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 8,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 6]]}],
    71, 15, 16],
   [6, 'tx']],
  ['control 2',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 39,
      'valid_to': 108,
      'kind': 'absolute',
      'periods': [[20, 16], [30, 32], [20, 20]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 45,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[5, 20]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 21,
      'valid_to': 46,
      'kind': 'absolute',
      'periods': [[5, 20]]}],
    34, None, 24],
   [20, 'default']]]]
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(profiles, t, session_start, hw_max):
    best = {}
    for p in profiles:
        if not (p['valid_from'] <= t and (p['valid_to'] is None or t < p['valid_to'])):
            continue
        if p['kind'] == 'relative':
            if session_start is None:
                session_start = t
            origin = session_start
        else:
            origin = p['valid_from']
        el = t - origin
        lim = None
        for off, val in sorted(p['periods']):
            if off <= el:
                lim = val
        if lim is None:
            continue
        cur = best.get(p['purpose'])
        if cur is None or p['stack'] > cur[0]:
            best[p['purpose']] = [p['stack'], lim]
    cands = []
    if 'max' in best:
        cands.append(['max', best['max'][1]])
    if 'tx' in best:
        cands.append(['tx', best['tx'][1]])
    elif 'default' in best:
        cands.append(['default', best['default'][1]])
    cands.append(['hw', hw_max])
    win = cands[0]
    for c in cands[1:]:
        if c[1] < win[1]:
            win = c
    return [win[1], win[0]]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 45,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 32], [45, 20], [5, 16]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 58,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 10], [45, 32], [0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 42,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 10], [0, 32]]}],
    56, None, 24],
   [24, 'hw']],
  ['regression: relative profile without session (partial repair)',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 37,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 20], [30, 32]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 57,
      'valid_to': 117,
      'kind': 'absolute',
      'periods': [[5, 6], [10, 16]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 32,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 10]]},
     {'purpose': 'max',
      'stack': 2,
      'valid_from': 38,
      'valid_to': 95,
      'kind': 'absolute',
      'periods': [[45, 6], [45, 20]]}],
    64, None, 32],
   [6, 'default']],
  ['control 1',
   [[{'purpose': 'max',
      'stack': 1,
      'valid_from': 44,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 10], [0, 10], [5, 10]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 7,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 10], [30, 20]]}],
    3, None, 24],
   [24, 'hw']],
  ['control 2',
   [[{'purpose': 'max',
      'stack': 2,
      'valid_from': 11,
      'valid_to': 21,
      'kind': 'relative',
      'periods': [[30, 32], [10, 20], [45, 6]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 14,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 32]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 19,
      'valid_to': 56,
      'kind': 'absolute',
      'periods': [[0, 6], [45, 10], [20, 16]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 53,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 10], [0, 32], [0, 20]]}],
    97, None, 16],
   [16, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: relative profile without session',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 60,
      'valid_to': 108,
      'kind': 'absolute',
      'periods': [[45, 16]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 29,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[45, 20]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 35,
      'valid_to': 81,
      'kind': 'relative',
      'periods': [[5, 16]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 19,
      'valid_to': 86,
      'kind': 'absolute',
      'periods': [[10, 16], [30, 32]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 50,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [5, 10], [10, 10]]}],
    55, None, 24],
   [24, 'hw']],
  ['regression: relative profile without session (partial repair)',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 37,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 16], [20, 6]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 43,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 6], [45, 6]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 4,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 10], [45, 16]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 39,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 16]]}],
    5, None, 16],
   [16, 'hw']],
  ['control 1',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 20,
      'valid_to': 56,
      'kind': 'relative',
      'periods': [[20, 10], [10, 20]]}],
    53, 38, 24],
   [20, 'default']],
  ['control 2',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 43,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 32]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 44,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 32], [45, 6], [20, 20]]}],
    88, 57, 24],
   [24, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: relative profile without session',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 29,
      'valid_to': 101,
      'kind': 'relative',
      'periods': [[20, 16]]}],
    50, None, 32],
   [32, 'hw']],
  ['regression: relative profile without session (partial repair)',
   [[{'purpose': 'max',
      'stack': 1,
      'valid_from': 60,
      'valid_to': 84,
      'kind': 'absolute',
      'periods': [[20, 10], [20, 6]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 19,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 20], [10, 16]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 45,
      'valid_to': 120,
      'kind': 'relative',
      'periods': [[30, 16], [5, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 34,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 16]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 31,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 20], [0, 10]]}],
    90, None, 16],
   [16, 'tx']],
  ['control 1',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 29,
      'valid_to': 52,
      'kind': 'absolute',
      'periods': [[30, 16], [45, 32], [0, 16]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 13,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [0, 10], [45, 16]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 58,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 16]]}],
    74, None, 24],
   [16, 'max']],
  ['control 2',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 8,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 6], [10, 6], [20, 32]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 59,
      'valid_to': 85,
      'kind': 'absolute',
      'periods': [[5, 6], [5, 20], [30, 20]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 29,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 10], [30, 10]]}],
    15, 48, 16],
   [16, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: relative profile without session',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 12,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 16], [5, 6]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 49,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 16], [0, 32], [20, 16]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 7,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 16], [10, 20], [45, 10]]}],
    58, None, 16],
   [16, 'hw']],
  ['regression: relative profile without session (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 39,
      'valid_to': 47,
      'kind': 'relative',
      'periods': [[20, 20]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 43,
      'valid_to': 101,
      'kind': 'relative',
      'periods': [[5, 32]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 1,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 32], [30, 16], [0, 20]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 39,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 16]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 59,
      'valid_to': 64,
      'kind': 'absolute',
      'periods': [[0, 16], [5, 16]]}],
    8, None, 32],
   [32, 'hw']],
  ['control 1',
   [[{'purpose': 'max',
      'stack': 2,
      'valid_from': 22,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[30, 6]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 34,
      'valid_to': 106,
      'kind': 'absolute',
      'periods': [[30, 16], [30, 10]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 11,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 6], [30, 20]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 26,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[5, 6], [5, 6], [45, 10]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 12,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 32], [20, 6]]}],
    77, None, 24],
   [20, 'tx']],
  ['control 2',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 55,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[5, 10], [30, 10]]}],
    25, 14, 16],
   [16, 'hw']]],
 [['boundary: profile expires at t',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': 30,
      'kind': 'absolute',
      'periods': [[0, 10]]}],
    30, None, 32],
   [32, 'hw']],
  ['boundary: relative profile without session',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 8]]}],
    20, None, 32],
   [32, 'hw']],
  ['boundary: tx overrides lower default',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 20]]}],
    5, 0, 32],
   [20, 'tx']],
  ['regression: relative profile without session',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 8,
      'valid_to': 18,
      'kind': 'relative',
      'periods': [[20, 6]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 21,
      'valid_to': 76,
      'kind': 'relative',
      'periods': [[10, 16]]}],
    57, None, 16],
   [16, 'hw']],
  ['regression: relative profile without session (partial repair)',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 30,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 10], [45, 20]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 36,
      'valid_to': 103,
      'kind': 'absolute',
      'periods': [[20, 32], [0, 16]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 1,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 10], [0, 16], [20, 16]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 45,
      'valid_to': 92,
      'kind': 'relative',
      'periods': [[45, 10], [20, 20], [10, 20]]}],
    69, None, 24],
   [24, 'hw']],
  ['control 1',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 8,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 6]]}],
    71, 15, 16],
   [6, 'tx']],
  ['control 2',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 39,
      'valid_to': 108,
      'kind': 'absolute',
      'periods': [[20, 16], [30, 32], [20, 20]]},
     {'purpose': 'max',
      'stack': 0,
      'valid_from': 45,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[5, 20]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 21,
      'valid_to': 46,
      'kind': 'absolute',
      'periods': [[5, 20]]}],
    34, None, 24],
   [20, 'default']]]]
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 relative profile applies before any transaction has started.

Each profile has purpose max/default/tx, stack level, validity [valid_from, valid_to) (valid_to None = open), kind absolute (origin valid_from) or relative (origin session_start; skipped when no session) and periods [[offset, amps]] in any order. A profile yields the amps of the last period with offset <= t-origin, or nothing before its first period. Per purpose the highest stack wins (ties: earlier profile). tx replaces default. Effective limit is the minimum of max, tx-or-default and hw_max, ties resolved in that order. Return [amps, source].

split: open-access


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

N = 1
observations = []
def solve(existing, req, buffer, n_conn):
    s, e, want = req
    if e <= s:
        return 'invalid'
    if want is not None and not 1 <= want <= n_conn:
        return 'invalid'
    conns = [want] if want is not None else list(range(1, n_conn + 1))
    for c in conns:
        clash = False
        for c2, s2, e2 in existing:
            if c2 == c and s <= e2 + buffer and s2 < e + buffer:
                clash = True
                break
        if not clash:
            return c
    return None
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: trailing buffer boundary (partial repair)',
   [[[1, 86, 145], [2, 154, 168]], [154, 199, None], 10, 2], None],
  ['control 1', [[[2, 157, 203], [1, 191, 236]], [179, 224, None], 5, 2], None],
  ['control 2',
   [[[3, 190, 210], [4, 11, 28], [2, 54, 68], [4, 54, 104], [3, 134, 162]], [79, 139, 4], 5, 4],
   None],
  ['control 3', [[[3, 29, 48], [3, 135, 149]], [-1, -11, 2], 0, 3], 'invalid']],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: trailing buffer boundary (partial repair)',
   [[[1, 162, 178], [1, 168, 179], [4, 176, 201], [3, 128, 179]], [187, 232, None], 10, 4], 2],
  ['control 1',
   [[[2, 130, 150], [3, 5, 36], [3, 174, 193], [2, 18, 78], [1, 61, 115], [4, 68, 95]],
    [171, 201, 2], 10, 4],
   2],
  ['control 2',
   [[[3, 116, 135], [1, 178, 208], [1, 83, 123], [1, 136, 157], [3, 111, 126], [1, 97, 137]],
    [217, 222, 2], 0, 3],
   2],
  ['control 3',
   [[[3, 146, 171], [4, 164, 212], [1, 43, 89], [4, 118, 155], [1, 93, 116], [4, 116, 130]],
    [171, 231, 5], 10, 4],
   'invalid']],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: trailing buffer boundary (partial repair)',
   [[[1, 152, 196], [4, 119, 151], [4, 121, 177], [3, 128, 156], [3, 113, 137], [3, 41, 66]],
    [199, 204, None], 5, 4],
   2],
  ['control 1', [[[3, 198, 211], [1, 147, 191], [4, 137, 178]], [148, 208, None], 10, 4], 2],
  ['control 2',
   [[[1, 75, 109], [1, 5, 46], [3, 128, 161], [3, 171, 198], [2, 190, 240], [1, 13, 36]],
    [58, 73, 1], 10, 3],
   None],
  ['control 3', [[[3, 91, 130], [3, 83, 131], [1, 156, 209]], [4, 19, None], 10, 3], 1]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: trailing buffer boundary (partial repair)',
   [[[1, 1, 57], [2, 28, 64]], [70, 75, 2], 15, 2], None],
  ['control 1', [[[3, 69, 129], [1, 134, 154], [4, 142, 167]], [67, 72, 0], 15, 4], 'invalid'],
  ['control 2', [[[2, 188, 212]], [190, 180, 2], 15, 2], 'invalid'],
  ['control 3', [[[1, 174, 218], [1, 116, 174], [3, 45, 62]], [74, 64, 1], 10, 4], 'invalid']],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: trailing buffer boundary (partial repair)',
   [[[1, 78, 106], [2, 99, 116], [1, 194, 210], [1, 175, 230]], [120, 180, None], 15, 2], None],
  ['control 1', [[[4, 7, 51], [4, 60, 108], [1, 80, 129]], [8, 68, None], 0, 4], 1],
  ['control 2',
   [[[1, 169, 199], [1, 193, 217], [1, 118, 149], [1, 4, 58], [1, 125, 169], [1, 1, 52]],
    [156, 146, None], 10, 1],
   'invalid'],
  ['control 3', [[], [214, 229, 2], 10, 2], 2]]]
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(existing, req, buffer, n_conn):
    s, e, want = req
    if e <= s:
        return 'invalid'
    if want is not None and not 1 <= want <= n_conn:
        return 'invalid'
    conns = [want] if want is not None else list(range(1, n_conn + 1))
    for c in conns:
        clash = False
        for c2, s2, e2 in existing:
            if c2 == c and s < e2 and s2 < e + buffer:
                clash = True
                break
        if not clash:
            return c
    return None
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: trailing buffer boundary (partial repair)',
   [[[1, 86, 145], [2, 154, 168]], [154, 199, None], 10, 2], None],
  ['control 1', [[[2, 157, 203], [1, 191, 236]], [179, 224, None], 5, 2], None],
  ['control 2',
   [[[3, 190, 210], [4, 11, 28], [2, 54, 68], [4, 54, 104], [3, 134, 162]], [79, 139, 4], 5, 4],
   None],
  ['control 3', [[[3, 29, 48], [3, 135, 149]], [-1, -11, 2], 0, 3], 'invalid']],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: trailing buffer boundary (partial repair)',
   [[[1, 162, 178], [1, 168, 179], [4, 176, 201], [3, 128, 179]], [187, 232, None], 10, 4], 2],
  ['control 1',
   [[[2, 130, 150], [3, 5, 36], [3, 174, 193], [2, 18, 78], [1, 61, 115], [4, 68, 95]],
    [171, 201, 2], 10, 4],
   2],
  ['control 2',
   [[[3, 116, 135], [1, 178, 208], [1, 83, 123], [1, 136, 157], [3, 111, 126], [1, 97, 137]],
    [217, 222, 2], 0, 3],
   2],
  ['control 3',
   [[[3, 146, 171], [4, 164, 212], [1, 43, 89], [4, 118, 155], [1, 93, 116], [4, 116, 130]],
    [171, 231, 5], 10, 4],
   'invalid']],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: trailing buffer boundary (partial repair)',
   [[[1, 152, 196], [4, 119, 151], [4, 121, 177], [3, 128, 156], [3, 113, 137], [3, 41, 66]],
    [199, 204, None], 5, 4],
   2],
  ['control 1', [[[3, 198, 211], [1, 147, 191], [4, 137, 178]], [148, 208, None], 10, 4], 2],
  ['control 2',
   [[[1, 75, 109], [1, 5, 46], [3, 128, 161], [3, 171, 198], [2, 190, 240], [1, 13, 36]],
    [58, 73, 1], 10, 3],
   None],
  ['control 3', [[[3, 91, 130], [3, 83, 131], [1, 156, 209]], [4, 19, None], 10, 3], 1]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: trailing buffer boundary (partial repair)',
   [[[1, 1, 57], [2, 28, 64]], [70, 75, 2], 15, 2], None],
  ['control 1', [[[3, 69, 129], [1, 134, 154], [4, 142, 167]], [67, 72, 0], 15, 4], 'invalid'],
  ['control 2', [[[2, 188, 212]], [190, 180, 2], 15, 2], 'invalid'],
  ['control 3', [[[1, 174, 218], [1, 116, 174], [3, 45, 62]], [74, 64, 1], 10, 4], 'invalid']],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: trailing buffer boundary (partial repair)',
   [[[1, 78, 106], [2, 99, 116], [1, 194, 210], [1, 175, 230]], [120, 180, None], 15, 2], None],
  ['control 1', [[[4, 7, 51], [4, 60, 108], [1, 80, 129]], [8, 68, None], 0, 4], 1],
  ['control 2',
   [[[1, 169, 199], [1, 193, 217], [1, 118, 149], [1, 4, 58], [1, 125, 169], [1, 1, 52]],
    [156, 146, None], 10, 1],
   'invalid'],
  ['control 3', [[], [214, 229, 2], 10, 2], 2]]]
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 reservation starting exactly one buffer after the previous one is rejected.

Connectors are numbered 1..n_conn. existing holds [connector, start, end) reservations. req is [start, end, connector or None]. end <= start or an out-of-range connector returns 'invalid'. Two reservations on the same connector conflict unless separated by at least buffer minutes on both sides. Return the requested connector if free, or the lowest free connector when None was requested, else None.

split: open-access


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

N = 1
observations = []
def solve(existing, req, buffer, n_conn):
    s, e, want = req
    if e <= s:
        return 'invalid'
    if want is not None and not 1 <= want <= n_conn:
        return 'invalid'
    conns = [want] if want is not None else list(range(1, n_conn + 1))
    for c in conns:
        clash = False
        for c2, s2, e2 in existing:
            if c2 == c and s < e2 + buffer and s2 < e:
                clash = True
                break
        if not clash:
            return c
    return None
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: leading buffer',
   [[[3, 167, 214], [1, 185, 245], [1, 49, 98], [3, 101, 158], [2, 29, 77], [4, 51, 78]],
    [11, 41, None], 10, 4],
   3],
  ['regression: leading buffer (partial repair)',
   [[[1, 75, 109], [1, 5, 46], [3, 128, 161], [3, 171, 198], [2, 190, 240], [1, 13, 36]],
    [58, 73, 1], 10, 3],
   None],
  ['control 1', [[[2, 157, 203], [1, 191, 236]], [179, 224, None], 5, 2], None],
  ['control 2',
   [[[3, 190, 210], [4, 11, 28], [2, 54, 68], [4, 54, 104], [3, 134, 162]], [79, 139, 4], 5, 4],
   None]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: leading buffer',
   [[[3, 167, 214], [1, 185, 245], [1, 49, 98], [3, 101, 158], [2, 29, 77], [4, 51, 78]],
    [11, 41, None], 10, 4],
   3],
  ['regression: leading buffer (partial repair)',
   [[[1, 75, 109], [1, 5, 46], [3, 128, 161], [3, 171, 198], [2, 190, 240], [1, 13, 36]],
    [58, 73, 1], 10, 3],
   None],
  ['control 1',
   [[[2, 130, 150], [3, 5, 36], [3, 174, 193], [2, 18, 78], [1, 61, 115], [4, 68, 95]],
    [171, 201, 2], 10, 4],
   2],
  ['control 2',
   [[[3, 116, 135], [1, 178, 208], [1, 83, 123], [1, 136, 157], [3, 111, 126], [1, 97, 137]],
    [217, 222, 2], 0, 3],
   2]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: leading buffer', [[[1, 136, 191], [1, 16, 74]], [120, 135, None], 15, 1], None],
  ['regression: leading buffer (partial repair)',
   [[[1, 73, 113], [1, 68, 107], [1, 36, 89], [3, 200, 258], [1, 22, 79], [3, 72, 84]],
    [4, 34, None], 15, 3],
   2],
  ['control 1', [[[3, 198, 211], [1, 147, 191], [4, 137, 178]], [148, 208, None], 10, 4], 2],
  ['control 2',
   [[[1, 75, 109], [1, 5, 46], [3, 128, 161], [3, 171, 198], [2, 190, 240], [1, 13, 36]],
    [58, 73, 1], 10, 3],
   None]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: leading buffer', [[[1, 136, 191], [1, 16, 74]], [120, 135, None], 15, 1], None],
  ['regression: leading buffer (partial repair)',
   [[[1, 73, 113], [1, 68, 107], [1, 36, 89], [3, 200, 258], [1, 22, 79], [3, 72, 84]],
    [4, 34, None], 15, 3],
   2],
  ['control 1', [[[3, 69, 129], [1, 134, 154], [4, 142, 167]], [67, 72, 0], 15, 4], 'invalid'],
  ['control 2', [[[2, 188, 212]], [190, 180, 2], 15, 2], 'invalid']],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: leading buffer', [[[1, 136, 191], [1, 16, 74]], [120, 135, None], 15, 1], None],
  ['regression: leading buffer (partial repair)',
   [[[1, 73, 113], [1, 68, 107], [1, 36, 89], [3, 200, 258], [1, 22, 79], [3, 72, 84]],
    [4, 34, None], 15, 3],
   2],
  ['control 1', [[[4, 7, 51], [4, 60, 108], [1, 80, 129]], [8, 68, None], 0, 4], 1],
  ['control 2',
   [[[1, 169, 199], [1, 193, 217], [1, 118, 149], [1, 4, 58], [1, 125, 169], [1, 1, 52]],
    [156, 146, None], 10, 1],
   'invalid']]]
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(existing, req, buffer, n_conn):
    s, e, want = req
    if e <= s:
        return 'invalid'
    if want is not None and not 1 <= want <= n_conn:
        return 'invalid'
    conns = [want] if want is not None else list(range(1, n_conn + 1))
    for c in conns:
        clash = False
        for c2, s2, e2 in existing:
            if c2 == c and s < e2 + buffer and s2 + buffer < e:
                clash = True
                break
        if not clash:
            return c
    return None
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: leading buffer',
   [[[3, 167, 214], [1, 185, 245], [1, 49, 98], [3, 101, 158], [2, 29, 77], [4, 51, 78]],
    [11, 41, None], 10, 4],
   3],
  ['regression: leading buffer (partial repair)',
   [[[1, 75, 109], [1, 5, 46], [3, 128, 161], [3, 171, 198], [2, 190, 240], [1, 13, 36]],
    [58, 73, 1], 10, 3],
   None],
  ['control 1', [[[2, 157, 203], [1, 191, 236]], [179, 224, None], 5, 2], None],
  ['control 2',
   [[[3, 190, 210], [4, 11, 28], [2, 54, 68], [4, 54, 104], [3, 134, 162]], [79, 139, 4], 5, 4],
   None]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: leading buffer',
   [[[3, 167, 214], [1, 185, 245], [1, 49, 98], [3, 101, 158], [2, 29, 77], [4, 51, 78]],
    [11, 41, None], 10, 4],
   3],
  ['regression: leading buffer (partial repair)',
   [[[1, 75, 109], [1, 5, 46], [3, 128, 161], [3, 171, 198], [2, 190, 240], [1, 13, 36]],
    [58, 73, 1], 10, 3],
   None],
  ['control 1',
   [[[2, 130, 150], [3, 5, 36], [3, 174, 193], [2, 18, 78], [1, 61, 115], [4, 68, 95]],
    [171, 201, 2], 10, 4],
   2],
  ['control 2',
   [[[3, 116, 135], [1, 178, 208], [1, 83, 123], [1, 136, 157], [3, 111, 126], [1, 97, 137]],
    [217, 222, 2], 0, 3],
   2]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: leading buffer', [[[1, 136, 191], [1, 16, 74]], [120, 135, None], 15, 1], None],
  ['regression: leading buffer (partial repair)',
   [[[1, 73, 113], [1, 68, 107], [1, 36, 89], [3, 200, 258], [1, 22, 79], [3, 72, 84]],
    [4, 34, None], 15, 3],
   2],
  ['control 1', [[[3, 198, 211], [1, 147, 191], [4, 137, 178]], [148, 208, None], 10, 4], 2],
  ['control 2',
   [[[1, 75, 109], [1, 5, 46], [3, 128, 161], [3, 171, 198], [2, 190, 240], [1, 13, 36]],
    [58, 73, 1], 10, 3],
   None]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: leading buffer', [[[1, 136, 191], [1, 16, 74]], [120, 135, None], 15, 1], None],
  ['regression: leading buffer (partial repair)',
   [[[1, 73, 113], [1, 68, 107], [1, 36, 89], [3, 200, 258], [1, 22, 79], [3, 72, 84]],
    [4, 34, None], 15, 3],
   2],
  ['control 1', [[[3, 69, 129], [1, 134, 154], [4, 142, 167]], [67, 72, 0], 15, 4], 'invalid'],
  ['control 2', [[[2, 188, 212]], [190, 180, 2], 15, 2], 'invalid']],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: leading buffer', [[[1, 136, 191], [1, 16, 74]], [120, 135, None], 15, 1], None],
  ['regression: leading buffer (partial repair)',
   [[[1, 73, 113], [1, 68, 107], [1, 36, 89], [3, 200, 258], [1, 22, 79], [3, 72, 84]],
    [4, 34, None], 15, 3],
   2],
  ['control 1', [[[4, 7, 51], [4, 60, 108], [1, 80, 129]], [8, 68, None], 0, 4], 1],
  ['control 2',
   [[[1, 169, 199], [1, 193, 217], [1, 118, 149], [1, 4, 58], [1, 125, 169], [1, 1, 52]],
    [156, 146, None], 10, 1],
   'invalid']]]
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 new reservation may end right as the next one starts, leaving no turnover time.

Connectors are numbered 1..n_conn. existing holds [connector, start, end) reservations. req is [start, end, connector or None]. end <= start or an out-of-range connector returns 'invalid'. Two reservations on the same connector conflict unless separated by at least buffer minutes on both sides. Return the requested connector if free, or the lowest free connector when None was requested, else None.

split: open-access


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

N = 1
observations = []
def solve(existing, req, buffer, n_conn):
    s, e, want = req
    if e <= s:
        return 'invalid'
    if want is not None and not 0 <= want < n_conn:
        return 'invalid'
    conns = [want] if want is not None else list(range(1, n_conn + 1))
    for c in conns:
        clash = False
        for c2, s2, e2 in existing:
            if c2 == c and s < e2 + buffer and s2 < e + buffer:
                clash = True
                break
        if not clash:
            return c
    return None
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: connector range', [[[2, 38, 60], [1, 98, 144], [2, 103, 163]], [15, 30, 2], 0, 2],
   2],
  ['regression: connector range (partial repair)', [[], [200, 215, 1], 0, 1], 1],
  ['control 1', [[[2, 157, 203], [1, 191, 236]], [179, 224, None], 5, 2], None],
  ['control 2',
   [[[3, 190, 210], [4, 11, 28], [2, 54, 68], [4, 54, 104], [3, 134, 162]], [79, 139, 4], 5, 4],
   None]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: connector range',
   [[[2, 99, 132], [2, 2, 55], [1, 39, 86], [1, 193, 252], [2, 195, 234], [1, 113, 173]],
    [150, 210, 2], 10, 2],
   None],
  ['regression: connector range (partial repair)',
   [[[1, 25, 81], [1, 152, 165], [1, 43, 90], [1, 62, 73]], [69, 84, 1], 10, 1], None],
  ['control 1',
   [[[2, 130, 150], [3, 5, 36], [3, 174, 193], [2, 18, 78], [1, 61, 115], [4, 68, 95]],
    [171, 201, 2], 10, 4],
   2],
  ['control 2',
   [[[3, 116, 135], [1, 178, 208], [1, 83, 123], [1, 136, 157], [3, 111, 126], [1, 97, 137]],
    [217, 222, 2], 0, 3],
   2]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: connector range',
   [[[2, 114, 152], [1, 105, 138], [2, 141, 185], [2, 30, 56], [2, 103, 145], [2, 127, 147]],
    [12, 17, 0], 15, 3],
   'invalid'],
  ['regression: connector range (partial repair)',
   [[[2, 129, 171], [1, 169, 226], [1, 85, 130], [1, 33, 84]], [46, 51, 2], 5, 2], 2],
  ['control 1', [[[3, 198, 211], [1, 147, 191], [4, 137, 178]], [148, 208, None], 10, 4], 2],
  ['control 2',
   [[[1, 75, 109], [1, 5, 46], [3, 128, 161], [3, 171, 198], [2, 190, 240], [1, 13, 36]],
    [58, 73, 1], 10, 3],
   None]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: connector range', [[], [214, 229, 2], 10, 2], 2],
  ['regression: connector range (partial repair)', [[[1, 1, 57], [2, 28, 64]], [70, 75, 2], 15, 2],
   None],
  ['control 1', [[[3, 69, 129], [1, 134, 154], [4, 142, 167]], [67, 72, 0], 15, 4], 'invalid'],
  ['control 2', [[[2, 188, 212]], [190, 180, 2], 15, 2], 'invalid']],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: connector range',
   [[[1, 100, 116], [1, 77, 119], [1, 115, 153], [2, 193, 239], [2, 186, 231]], [87, 117, 2], 10,
    2],
   2],
  ['regression: connector range (partial repair)',
   [[[1, 94, 122], [1, 115, 163], [1, 80, 117], [1, 67, 119], [1, 188, 233]], [125, 170, 1], 5, 1],
   None],
  ['control 1', [[[4, 7, 51], [4, 60, 108], [1, 80, 129]], [8, 68, None], 0, 4], 1],
  ['control 2',
   [[[1, 169, 199], [1, 193, 217], [1, 118, 149], [1, 4, 58], [1, 125, 169], [1, 1, 52]],
    [156, 146, None], 10, 1],
   'invalid']]]
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(existing, req, buffer, n_conn):
    s, e, want = req
    if e <= s:
        return 'invalid'
    if want is not None and not 1 <= want < n_conn:
        return 'invalid'
    conns = [want] if want is not None else list(range(1, n_conn + 1))
    for c in conns:
        clash = False
        for c2, s2, e2 in existing:
            if c2 == c and s < e2 + buffer and s2 < e + buffer:
                clash = True
                break
        if not clash:
            return c
    return None
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: connector range', [[[2, 38, 60], [1, 98, 144], [2, 103, 163]], [15, 30, 2], 0, 2],
   2],
  ['regression: connector range (partial repair)', [[], [200, 215, 1], 0, 1], 1],
  ['control 1', [[[2, 157, 203], [1, 191, 236]], [179, 224, None], 5, 2], None],
  ['control 2',
   [[[3, 190, 210], [4, 11, 28], [2, 54, 68], [4, 54, 104], [3, 134, 162]], [79, 139, 4], 5, 4],
   None]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: connector range',
   [[[2, 99, 132], [2, 2, 55], [1, 39, 86], [1, 193, 252], [2, 195, 234], [1, 113, 173]],
    [150, 210, 2], 10, 2],
   None],
  ['regression: connector range (partial repair)',
   [[[1, 25, 81], [1, 152, 165], [1, 43, 90], [1, 62, 73]], [69, 84, 1], 10, 1], None],
  ['control 1',
   [[[2, 130, 150], [3, 5, 36], [3, 174, 193], [2, 18, 78], [1, 61, 115], [4, 68, 95]],
    [171, 201, 2], 10, 4],
   2],
  ['control 2',
   [[[3, 116, 135], [1, 178, 208], [1, 83, 123], [1, 136, 157], [3, 111, 126], [1, 97, 137]],
    [217, 222, 2], 0, 3],
   2]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: connector range',
   [[[2, 114, 152], [1, 105, 138], [2, 141, 185], [2, 30, 56], [2, 103, 145], [2, 127, 147]],
    [12, 17, 0], 15, 3],
   'invalid'],
  ['regression: connector range (partial repair)',
   [[[2, 129, 171], [1, 169, 226], [1, 85, 130], [1, 33, 84]], [46, 51, 2], 5, 2], 2],
  ['control 1', [[[3, 198, 211], [1, 147, 191], [4, 137, 178]], [148, 208, None], 10, 4], 2],
  ['control 2',
   [[[1, 75, 109], [1, 5, 46], [3, 128, 161], [3, 171, 198], [2, 190, 240], [1, 13, 36]],
    [58, 73, 1], 10, 3],
   None]],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: connector range', [[], [214, 229, 2], 10, 2], 2],
  ['regression: connector range (partial repair)', [[[1, 1, 57], [2, 28, 64]], [70, 75, 2], 15, 2],
   None],
  ['control 1', [[[3, 69, 129], [1, 134, 154], [4, 142, 167]], [67, 72, 0], 15, 4], 'invalid'],
  ['control 2', [[[2, 188, 212]], [190, 180, 2], 15, 2], 'invalid']],
 [['boundary: back-to-back with buffer', [[[1, 0, 30]], [40, 60, 1], 10, 1], 1],
  ['boundary: zero-length request', [[], [10, 10, None], 0, 2], 'invalid'],
  ['boundary: last connector requested', [[[1, 0, 30]], [0, 30, 3], 0, 3], 3],
  ['regression: connector range',
   [[[1, 100, 116], [1, 77, 119], [1, 115, 153], [2, 193, 239], [2, 186, 231]], [87, 117, 2], 10,
    2],
   2],
  ['regression: connector range (partial repair)',
   [[[1, 94, 122], [1, 115, 163], [1, 80, 117], [1, 67, 119], [1, 188, 233]], [125, 170, 1], 5, 1],
   None],
  ['control 1', [[[4, 7, 51], [4, 60, 108], [1, 80, 129]], [8, 68, None], 0, 4], 1],
  ['control 2',
   [[[1, 169, 199], [1, 193, 217], [1, 118, 149], [1, 4, 58], [1, 125, 169], [1, 1, 52]],
    [156, 146, None], 10, 1],
   'invalid']]]
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:
Requests for the highest-numbered connector are rejected and connector 0 is accepted.

Connectors are numbered 1..n_conn. existing holds [connector, start, end) reservations. req is [start, end, connector or None]. end <= start or an out-of-range connector returns 'invalid'. Two reservations on the same connector conflict unless separated by at least buffer minutes on both sides. Return the requested connector if free, or the lowest free connector when None was requested, else None.

split: open-access