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: validity end exclusivity',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 39,
      'valid_to': 45,
      'kind': 'absolute',
      'periods': [[0, 6], [5, 32], [0, 10]]}],
    45, None, 32],
   [32, 'hw']],
  ['regression: validity end exclusivity (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 17,
      'valid_to': 91,
      'kind': 'absolute',
      'periods': [[30, 10], [5, 10]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 28,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 20], [30, 6], [10, 16]]}],
    90, 29, 16],
   [10, 'tx']],
  ['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: validity end exclusivity',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 39,
      'valid_to': 45,
      'kind': 'absolute',
      'periods': [[0, 6], [5, 32], [0, 10]]}],
    45, None, 32],
   [32, 'hw']],
  ['regression: validity end exclusivity (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 17,
      'valid_to': 91,
      'kind': 'absolute',
      'periods': [[30, 10], [5, 10]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 28,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 20], [30, 6], [10, 16]]}],
    90, 29, 16],
   [10, 'tx']],
  ['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: validity end exclusivity',
   [[{'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']],
  ['regression: validity end exclusivity (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 17,
      'valid_to': 91,
      'kind': 'absolute',
      'periods': [[30, 10], [5, 10]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 28,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 20], [30, 6], [10, 16]]}],
    90, 29, 16],
   [10, '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: validity end exclusivity',
   [[{'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']],
  ['regression: validity end exclusivity (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 17,
      'valid_to': 91,
      'kind': 'absolute',
      'periods': [[30, 10], [5, 10]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 28,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 20], [30, 6], [10, 16]]}],
    90, 29, 16],
   [10, 'tx']],
  ['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: validity end exclusivity',
   [[{'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']],
  ['regression: validity end exclusivity (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 17,
      'valid_to': 91,
      'kind': 'absolute',
      'periods': [[30, 10], [5, 10]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 28,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 20], [30, 6], [10, 16]]}],
    90, 29, 16],
   [10, 'tx']],
  ['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'] - 1)):
            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: validity end exclusivity',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 39,
      'valid_to': 45,
      'kind': 'absolute',
      'periods': [[0, 6], [5, 32], [0, 10]]}],
    45, None, 32],
   [32, 'hw']],
  ['regression: validity end exclusivity (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 17,
      'valid_to': 91,
      'kind': 'absolute',
      'periods': [[30, 10], [5, 10]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 28,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 20], [30, 6], [10, 16]]}],
    90, 29, 16],
   [10, 'tx']],
  ['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: validity end exclusivity',
   [[{'purpose': 'default',
      'stack': 1,
      'valid_from': 39,
      'valid_to': 45,
      'kind': 'absolute',
      'periods': [[0, 6], [5, 32], [0, 10]]}],
    45, None, 32],
   [32, 'hw']],
  ['regression: validity end exclusivity (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 17,
      'valid_to': 91,
      'kind': 'absolute',
      'periods': [[30, 10], [5, 10]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 28,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 20], [30, 6], [10, 16]]}],
    90, 29, 16],
   [10, 'tx']],
  ['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: validity end exclusivity',
   [[{'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']],
  ['regression: validity end exclusivity (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 17,
      'valid_to': 91,
      'kind': 'absolute',
      'periods': [[30, 10], [5, 10]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 28,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 20], [30, 6], [10, 16]]}],
    90, 29, 16],
   [10, '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: validity end exclusivity',
   [[{'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']],
  ['regression: validity end exclusivity (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 17,
      'valid_to': 91,
      'kind': 'absolute',
      'periods': [[30, 10], [5, 10]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 28,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 20], [30, 6], [10, 16]]}],
    90, 29, 16],
   [10, 'tx']],
  ['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: validity end exclusivity',
   [[{'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']],
  ['regression: validity end exclusivity (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 17,
      'valid_to': 91,
      'kind': 'absolute',
      'periods': [[30, 10], [5, 10]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 28,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 20], [30, 6], [10, 16]]}],
    90, 29, 16],
   [10, 'tx']],
  ['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 profile keeps limiting current for one tick after it expired.

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:
                continue
            origin = p['valid_from']
        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 schedule origin',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 6,
      'valid_to': 42,
      'kind': 'relative',
      'periods': [[0, 20], [10, 16]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 59,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 32]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 8,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 32], [0, 6]]}],
    12, 0, 24],
   [16, 'tx']],
  ['regression: relative schedule origin (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 49,
      'valid_to': 55,
      'kind': 'relative',
      'periods': [[30, 20], [10, 10], [0, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 51,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[30, 32], [0, 6], [0, 10]]}],
    59, 20, 16],
   [16, '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: relative schedule origin',
   [[{'purpose': 'max',
      'stack': 2,
      'valid_from': 60,
      'valid_to': 79,
      'kind': 'relative',
      'periods': [[0, 6], [30, 32], [5, 16]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 21,
      'valid_to': 94,
      'kind': 'relative',
      'periods': [[5, 6], [30, 16], [0, 20]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 56,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 32], [10, 6]]}],
    30, 58, 32],
   [32, 'hw']],
  ['regression: relative schedule origin (partial repair)',
   [[{'purpose': 'max',
      'stack': 1,
      'valid_from': 39,
      'valid_to': 113,
      'kind': 'relative',
      'periods': [[30, 10], [20, 32]]}],
    54, 7, 16],
   [10, 'max']],
  ['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 schedule origin',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 30,
      'valid_to': 93,
      'kind': 'absolute',
      'periods': [[20, 6], [45, 10]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 14,
      'valid_to': 89,
      'kind': 'relative',
      'periods': [[10, 16], [45, 20]]}],
    43, 36, 32],
   [32, 'hw']],
  ['regression: relative schedule origin (partial repair)',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 22,
      'valid_to': 86,
      'kind': 'relative',
      'periods': [[30, 10], [10, 20], [45, 32]]}],
    51, 2, 24],
   [24, '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: relative schedule origin',
   [[{'purpose': 'max',
      'stack': 1,
      'valid_from': 34,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 16], [0, 32]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 25,
      'valid_to': 27,
      'kind': 'relative',
      'periods': [[20, 32], [0, 20], [45, 16]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 19,
      'valid_to': 59,
      'kind': 'relative',
      'periods': [[45, 16], [45, 32], [0, 6]]}],
    23, 45, 24],
   [24, 'hw']],
  ['regression: relative schedule origin (partial repair)',
   [[{'purpose': 'max',
      'stack': 0,
      'valid_from': 9,
      'valid_to': 26,
      'kind': 'absolute',
      'periods': [[30, 6]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 36,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 6], [10, 32], [10, 10]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 54,
      'valid_to': 130,
      'kind': 'relative',
      'periods': [[30, 32], [10, 10], [30, 6]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 29,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [20, 20], [5, 32]]}],
    81, 13, 32],
   [32, 'tx']],
  ['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 schedule origin',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[5, 16], [10, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 12,
      'valid_to': 24,
      'kind': 'relative',
      'periods': [[0, 10]]}],
    30, 54, 32],
   [32, 'hw']],
  ['regression: relative schedule origin (partial repair)',
   [[{'purpose': 'max',
      'stack': 1,
      'valid_from': 24,
      'valid_to': 74,
      'kind': 'relative',
      'periods': [[45, 10]]},
     {'purpose': 'max',
      'stack': 2,
      'valid_from': 43,
      'valid_to': 81,
      'kind': 'relative',
      'periods': [[20, 10]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 3,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[5, 16], [20, 20]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 20,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 20], [5, 32]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 5,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 32], [30, 16], [0, 20]]}],
    56, 8, 16],
   [10, 'max']],
  ['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:
                continue
            origin = max(session_start, p['valid_from'])
        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 schedule origin',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 6,
      'valid_to': 42,
      'kind': 'relative',
      'periods': [[0, 20], [10, 16]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 59,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 32]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 8,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[10, 32], [0, 6]]}],
    12, 0, 24],
   [16, 'tx']],
  ['regression: relative schedule origin (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 49,
      'valid_to': 55,
      'kind': 'relative',
      'periods': [[30, 20], [10, 10], [0, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 51,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[30, 32], [0, 6], [0, 10]]}],
    59, 20, 16],
   [16, '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: relative schedule origin',
   [[{'purpose': 'max',
      'stack': 2,
      'valid_from': 60,
      'valid_to': 79,
      'kind': 'relative',
      'periods': [[0, 6], [30, 32], [5, 16]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 21,
      'valid_to': 94,
      'kind': 'relative',
      'periods': [[5, 6], [30, 16], [0, 20]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 56,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 32], [10, 6]]}],
    30, 58, 32],
   [32, 'hw']],
  ['regression: relative schedule origin (partial repair)',
   [[{'purpose': 'max',
      'stack': 1,
      'valid_from': 39,
      'valid_to': 113,
      'kind': 'relative',
      'periods': [[30, 10], [20, 32]]}],
    54, 7, 16],
   [10, 'max']],
  ['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 schedule origin',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 30,
      'valid_to': 93,
      'kind': 'absolute',
      'periods': [[20, 6], [45, 10]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 14,
      'valid_to': 89,
      'kind': 'relative',
      'periods': [[10, 16], [45, 20]]}],
    43, 36, 32],
   [32, 'hw']],
  ['regression: relative schedule origin (partial repair)',
   [[{'purpose': 'tx',
      'stack': 0,
      'valid_from': 22,
      'valid_to': 86,
      'kind': 'relative',
      'periods': [[30, 10], [10, 20], [45, 32]]}],
    51, 2, 24],
   [24, '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: relative schedule origin',
   [[{'purpose': 'max',
      'stack': 1,
      'valid_from': 34,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 16], [0, 32]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 25,
      'valid_to': 27,
      'kind': 'relative',
      'periods': [[20, 32], [0, 20], [45, 16]]},
     {'purpose': 'max',
      'stack': 1,
      'valid_from': 19,
      'valid_to': 59,
      'kind': 'relative',
      'periods': [[45, 16], [45, 32], [0, 6]]}],
    23, 45, 24],
   [24, 'hw']],
  ['regression: relative schedule origin (partial repair)',
   [[{'purpose': 'max',
      'stack': 0,
      'valid_from': 9,
      'valid_to': 26,
      'kind': 'absolute',
      'periods': [[30, 6]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 36,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 6], [10, 32], [10, 10]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 54,
      'valid_to': 130,
      'kind': 'relative',
      'periods': [[30, 32], [10, 10], [30, 6]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 29,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [20, 20], [5, 32]]}],
    81, 13, 32],
   [32, 'tx']],
  ['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 schedule origin',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 0,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[5, 16], [10, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 12,
      'valid_to': 24,
      'kind': 'relative',
      'periods': [[0, 10]]}],
    30, 54, 32],
   [32, 'hw']],
  ['regression: relative schedule origin (partial repair)',
   [[{'purpose': 'max',
      'stack': 1,
      'valid_from': 24,
      'valid_to': 74,
      'kind': 'relative',
      'periods': [[45, 10]]},
     {'purpose': 'max',
      'stack': 2,
      'valid_from': 43,
      'valid_to': 81,
      'kind': 'relative',
      'periods': [[20, 10]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 3,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[5, 16], [20, 20]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 20,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 20], [5, 32]]},
     {'purpose': 'default',
      'stack': 1,
      'valid_from': 5,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 32], [30, 16], [0, 20]]}],
    56, 8, 16],
   [10, 'max']],
  ['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:
Relative profiles step through their periods on the wall clock instead of session elapsed time.

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:
                continue
            origin = session_start
        else:
            origin = p['valid_from']
        el = t - origin
        lim = None
        for off, val in 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: period ordering',
   [[{'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: period ordering (partial repair)',
   [[{'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']],
  ['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: period ordering',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 7,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[45, 16], [20, 6]]}],
    100, None, 24],
   [16, 'tx']],
  ['regression: period ordering (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 10,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 16], [20, 32], [45, 10]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 24,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 16], [5, 32], [5, 10]]}],
    90, 50, 24],
   [10, 'tx']],
  ['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: period ordering',
   [[{'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']],
  ['regression: period ordering (partial repair)',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 18,
      'valid_to': 34,
      'kind': 'relative',
      'periods': [[30, 16], [20, 10], [5, 10]]},
     {'purpose': 'max',
      'stack': 2,
      'valid_from': 21,
      'valid_to': 43,
      'kind': 'relative',
      'periods': [[45, 20], [10, 16], [5, 10]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 59,
      'valid_to': 88,
      'kind': 'absolute',
      'periods': [[0, 10], [10, 20]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 41,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 20], [10, 10], [20, 10]]}],
    69, None, 24],
   [20, '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: period ordering',
   [[{'purpose': 'max',
      'stack': 0,
      'valid_from': 9,
      'valid_to': 26,
      'kind': 'absolute',
      'periods': [[30, 6]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 36,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 6], [10, 32], [10, 10]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 54,
      'valid_to': 130,
      'kind': 'relative',
      'periods': [[30, 32], [10, 10], [30, 6]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 29,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [20, 20], [5, 32]]}],
    81, 13, 32],
   [32, 'tx']],
  ['regression: period ordering (partial repair)',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 6,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 10], [45, 16]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 2,
      'valid_to': 37,
      'kind': 'relative',
      'periods': [[45, 10], [10, 32]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 38,
      'valid_to': 48,
      'kind': 'absolute',
      'periods': [[5, 10]]}],
    53, None, 24],
   [16, 'default']],
  ['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: period ordering',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 56,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 10], [0, 6], [0, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 23,
      'valid_to': 53,
      'kind': 'relative',
      'periods': [[10, 10], [5, 20], [45, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 59,
      'valid_to': 135,
      'kind': 'absolute',
      'periods': [[0, 32]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 59,
      'valid_to': 130,
      'kind': 'relative',
      'periods': [[0, 16], [20, 32], [30, 20]]}],
    51, 36, 16],
   [10, 'tx']],
  ['regression: period ordering (partial repair)',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 24,
      'valid_to': 78,
      'kind': 'absolute',
      'periods': [[0, 6], [20, 20]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 1,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 20], [0, 10], [0, 16]]}],
    99, None, 32],
   [20, 'default']],
  ['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'], reverse=True):
            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: period ordering',
   [[{'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: period ordering (partial repair)',
   [[{'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']],
  ['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: period ordering',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 7,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[45, 16], [20, 6]]}],
    100, None, 24],
   [16, 'tx']],
  ['regression: period ordering (partial repair)',
   [[{'purpose': 'tx',
      'stack': 1,
      'valid_from': 10,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 16], [20, 32], [45, 10]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 24,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[0, 16], [5, 32], [5, 10]]}],
    90, 50, 24],
   [10, 'tx']],
  ['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: period ordering',
   [[{'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']],
  ['regression: period ordering (partial repair)',
   [[{'purpose': 'tx',
      'stack': 2,
      'valid_from': 18,
      'valid_to': 34,
      'kind': 'relative',
      'periods': [[30, 16], [20, 10], [5, 10]]},
     {'purpose': 'max',
      'stack': 2,
      'valid_from': 21,
      'valid_to': 43,
      'kind': 'relative',
      'periods': [[45, 20], [10, 16], [5, 10]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 59,
      'valid_to': 88,
      'kind': 'absolute',
      'periods': [[0, 10], [10, 20]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 41,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[10, 20], [10, 10], [20, 10]]}],
    69, None, 24],
   [20, '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: period ordering',
   [[{'purpose': 'max',
      'stack': 0,
      'valid_from': 9,
      'valid_to': 26,
      'kind': 'absolute',
      'periods': [[30, 6]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 36,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 6], [10, 32], [10, 10]]},
     {'purpose': 'tx',
      'stack': 2,
      'valid_from': 54,
      'valid_to': 130,
      'kind': 'relative',
      'periods': [[30, 32], [10, 10], [30, 6]]},
     {'purpose': 'tx',
      'stack': 1,
      'valid_from': 29,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[0, 6], [20, 20], [5, 32]]}],
    81, 13, 32],
   [32, 'tx']],
  ['regression: period ordering (partial repair)',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 6,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[30, 10], [45, 16]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 2,
      'valid_to': 37,
      'kind': 'relative',
      'periods': [[45, 10], [10, 32]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 38,
      'valid_to': 48,
      'kind': 'absolute',
      'periods': [[5, 10]]}],
    53, None, 24],
   [16, 'default']],
  ['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: period ordering',
   [[{'purpose': 'default',
      'stack': 2,
      'valid_from': 56,
      'valid_to': None,
      'kind': 'relative',
      'periods': [[45, 10], [0, 6], [0, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 23,
      'valid_to': 53,
      'kind': 'relative',
      'periods': [[10, 10], [5, 20], [45, 20]]},
     {'purpose': 'tx',
      'stack': 0,
      'valid_from': 59,
      'valid_to': 135,
      'kind': 'absolute',
      'periods': [[0, 32]]},
     {'purpose': 'default',
      'stack': 2,
      'valid_from': 59,
      'valid_to': 130,
      'kind': 'relative',
      'periods': [[0, 16], [20, 32], [30, 20]]}],
    51, 36, 16],
   [10, 'tx']],
  ['regression: period ordering (partial repair)',
   [[{'purpose': 'default',
      'stack': 0,
      'valid_from': 24,
      'valid_to': 78,
      'kind': 'absolute',
      'periods': [[0, 6], [20, 20]]},
     {'purpose': 'default',
      'stack': 0,
      'valid_from': 1,
      'valid_to': None,
      'kind': 'absolute',
      'periods': [[20, 20], [0, 10], [0, 16]]}],
    99, None, 32],
   [20, 'default']],
  ['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 applied limit depends on the order periods were listed in the message.

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