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

N = 1
observations = []
def solve(alarms, scans):
    level = dict(alarms)
    prev = set()
    unacked = set()
    silenced = False
    out = []
    for s in scans:
        active = set(s.get('active', []))
        new = active - prev
        if new:
            silenced = False
        unacked = (unacked | new) & active
        if s.get('ack'):
            unacked = set()
        if s.get('silence'):
            silenced = True
        if any(level[a] == 'high' for a in active):
            shown = 'high'
        elif active:
            shown = 'low'
        else:
            shown = None
        color = {'high': 'red', 'low': 'amber', None: 'off'}[shown]
        flashing = any(level[a] == shown for a in unacked)
        horn = bool(active) and not silenced
        prev = active
        out.append([color, flashing, horn])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: escalation sequence',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['B']},
     {'active': ['B'], 'silence': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'ack': True},
     {'active': ['B']},
     {}]],
   [['amber', True, True],
    ['amber', True, False],
    ['red', True, True],
    ['red', False, False],
    ['amber', False, False],
    ['off', False, False]]],
  ['regression: low alarm while high acked',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A'], 'ack': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'silence': True},
     {'active': ['A', 'B']}]],
   [['red', False, False], ['red', False, True], ['red', False, False], ['red', False, False]]],
  ['control: alarm clears unacknowledged',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {}, {'active': ['B']}]],
   [['red', True, True], ['off', False, False], ['amber', True, True]]],
  ['regression: alarm present at first scan',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {'active': ['A']}]],
   [['red', True, True], ['red', True, True]]],
  ['control: silence then new low alarm',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A']}, {'active': ['A'], 'silence': True}, {'active': ['A', 'B']}]],
   [['red', True, True], ['red', True, False], ['red', True, True]]],
  ['regression: scenario 1',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['TT300'], 'ack': True},
     {'active': ['TT300']},
     {'active': ['ZS400'], 'ack': True, 'silence': True},
     {'active': []},
     {'active': ['ZS400'], 'silence': True},
     {'active': ['TT300', 'ZS400']},
     {'active': ['LT200', 'TT300', 'ZS400']},
     {'active': ['LT200', 'TT300', 'ZS400']},
     {'active': ['LT200', 'ZS400'], 'ack': True}]],
   [['amber', False, False],
    ['amber', False, False],
    ['red', False, False],
    ['off', False, False],
    ['red', True, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', False, False]]],
  ['control: scenario 2',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['TT300'], 'silence': True},
     {'active': ['ZS400']},
     {'active': ['LT200', 'TT300']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']},
     {'active': ['LT200', 'PT101', 'TT300'], 'silence': True},
     {'active': ['LT200', 'PT101', 'TT300'], 'silence': True}]],
   [['amber', True, False],
    ['red', True, True],
    ['amber', True, True],
    ['red', True, True],
    ['red', True, False],
    ['red', True, False]]],
  ['regression: scenario 3',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': [], 'ack': True},
     {'active': []},
     {'active': ['ZS400']},
     {'active': ['ZS400']},
     {'active': ['LT200', 'PT101', 'ZS400'], 'silence': True},
     {'active': ['LT200', 'PT101'], 'ack': True}]],
   [['off', False, False],
    ['off', False, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, False],
    ['red', False, False]]]],
 [['regression: low alarm while high acked',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A'], 'ack': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'silence': True},
     {'active': ['A', 'B']}]],
   [['red', False, False], ['red', False, True], ['red', False, False], ['red', False, False]]],
  ['regression: scenario 1',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['ZS400'], 'silence': True},
     {'active': ['LT200', 'ZS400']},
     {'active': ['LT200', 'ZS400']},
     {'active': ['LT200', 'ZS400']},
     {'active': ['LT200', 'ZS400']},
     {'active': ['LT200']}]],
   [['red', True, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['amber', True, True]]],
  ['regression: escalation sequence',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['B']},
     {'active': ['B'], 'silence': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'ack': True},
     {'active': ['B']},
     {}]],
   [['amber', True, True],
    ['amber', True, False],
    ['red', True, True],
    ['red', False, False],
    ['amber', False, False],
    ['off', False, False]]],
  ['control: alarm clears unacknowledged',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {}, {'active': ['B']}]],
   [['red', True, True], ['off', False, False], ['amber', True, True]]],
  ['regression: alarm present at first scan',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {'active': ['A']}]],
   [['red', True, True], ['red', True, True]]],
  ['control: silence then new low alarm',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A']}, {'active': ['A'], 'silence': True}, {'active': ['A', 'B']}]],
   [['red', True, True], ['red', True, False], ['red', True, True]]],
  ['regression: scenario 2',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['PT101']},
     {'active': ['ZS400'], 'ack': True},
     {'active': ['TT300']},
     {'active': ['TT300']},
     {'active': ['PT101', 'TT300']},
     {'active': ['PT101']},
     {'active': ['LT200', 'ZS400']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']},
     {'active': ['LT200', 'TT300', 'ZS400']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']},
     {'active': ['LT200', 'TT300'], 'ack': True},
     {'active': ['LT200', 'TT300']}]],
   [['red', True, True],
    ['red', False, False],
    ['amber', True, True],
    ['amber', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['amber', False, False],
    ['amber', False, False]]],
  ['regression: scenario 3',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['LT200', 'PT101', 'ZS400']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']},
     {'active': ['LT200', 'PT101', 'ZS400'], 'ack': True},
     {'active': ['LT200', 'PT101']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']},
     {'active': ['ZS400']},
     {'active': ['PT101', 'ZS400']},
     {'active': ['PT101', 'ZS400'], 'ack': True},
     {'active': ['PT101', 'ZS400']}]],
   [['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', False, False],
    ['red', False, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', False, False],
    ['red', False, False]]]],
 [['regression: scenario 4',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['PT101'], 'silence': True},
     {'active': ['ZS400']},
     {'active': ['TT300']},
     {'active': ['TT300']},
     {'active': ['PT101']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400'], 'ack': True}]],
   [['red', True, False],
    ['red', True, True],
    ['amber', True, True],
    ['amber', True, True],
    ['red', True, True],
    ['red', False, False]]],
  ['regression: scenario 1',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['PT101']},
     {'active': ['LT200', 'PT101']},
     {'active': ['LT200', 'PT101']},
     {'active': ['LT200', 'PT101'], 'silence': True},
     {'active': ['LT200', 'PT101']},
     {'active': []},
     {'active': []},
     {'active': []},
     {'active': [], 'ack': True},
     {'active': ['LT200']},
     {'active': ['LT200', 'TT300']}]],
   [['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, False],
    ['red', True, False],
    ['off', False, False],
    ['off', False, False],
    ['off', False, False],
    ['off', False, False],
    ['amber', True, True],
    ['amber', True, True]]],
  ['regression: escalation sequence',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['B']},
     {'active': ['B'], 'silence': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'ack': True},
     {'active': ['B']},
     {}]],
   [['amber', True, True],
    ['amber', True, False],
    ['red', True, True],
    ['red', False, False],
    ['amber', False, False],
    ['off', False, False]]],
  ['control: alarm clears unacknowledged',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {}, {'active': ['B']}]],
   [['red', True, True], ['off', False, False], ['amber', True, True]]],
  ['regression: low alarm while high acked',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A'], 'ack': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'silence': True},
     {'active': ['A', 'B']}]],
   [['red', False, False], ['red', False, True], ['red', False, False], ['red', False, False]]],
  ['regression: alarm present at first scan',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {'active': ['A']}]],
   [['red', True, True], ['red', True, True]]],
  ['control: silence then new low alarm',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A']}, {'active': ['A'], 'silence': True}, {'active': ['A', 'B']}]],
   [['red', True, True], ['red', True, False], ['red', True, True]]],
  ['regression: scenario 2',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': []},
     {'active': ['ZS400']},
     {'active': ['ZS400']},
     {'active': ['ZS400'], 'silence': True},
     {'active': ['ZS400']},
     {'active': []},
     {'active': ['LT200', 'ZS400']},
     {'active': ['LT200', 'PT101', 'ZS400']},
     {'active': ['PT101', 'TT300', 'ZS400']}]],
   [['off', False, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, False],
    ['red', True, False],
    ['off', False, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True]]]],
 [['regression: scenario 2',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': []},
     {'active': ['PT101', 'TT300']},
     {'active': ['PT101', 'TT300'], 'ack': True},
     {'active': ['PT101', 'ZS400'], 'silence': True},
     {'active': ['PT101']},
     {'active': ['PT101'], 'silence': True}]],
   [['off', False, False],
    ['red', True, True],
    ['red', False, False],
    ['red', True, False],
    ['red', False, False],
    ['red', False, False]]],
  ['regression: scenario 4',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['LT200'], 'silence': True},
     {'active': ['PT101', 'TT300']},
     {'active': ['PT101', 'TT300', 'ZS400']},
     {'active': ['ZS400']},
     {'active': ['PT101']},
     {'active': ['LT200', 'PT101', 'TT300']}]],
   [['amber', True, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True]]],
  ['regression: escalation sequence',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['B']},
     {'active': ['B'], 'silence': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'ack': True},
     {'active': ['B']},
     {}]],
   [['amber', True, True],
    ['amber', True, False],
    ['red', True, True],
    ['red', False, False],
    ['amber', False, False],
    ['off', False, False]]],
  ['control: alarm clears unacknowledged',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {}, {'active': ['B']}]],
   [['red', True, True], ['off', False, False], ['amber', True, True]]],
  ['regression: low alarm while high acked',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A'], 'ack': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'silence': True},
     {'active': ['A', 'B']}]],
   [['red', False, False], ['red', False, True], ['red', False, False], ['red', False, False]]],
  ['regression: alarm present at first scan',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {'active': ['A']}]],
   [['red', True, True], ['red', True, True]]],
  ['control: silence then new low alarm',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A']}, {'active': ['A'], 'silence': True}, {'active': ['A', 'B']}]],
   [['red', True, True], ['red', True, False], ['red', True, True]]],
  ['regression: scenario 1',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['PT101'], 'ack': True},
     {'active': ['PT101']},
     {'active': ['PT101'], 'ack': True},
     {'active': ['PT101']},
     {'active': ['PT101']},
     {'active': ['LT200', 'PT101', 'TT300']},
     {'active': ['LT200', 'PT101', 'TT300'], 'ack': True},
     {'active': ['LT200', 'TT300']}]],
   [['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, True],
    ['red', False, False],
    ['amber', False, False]]]],
 [['regression: scenario 3',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['ZS400'], 'ack': True},
     {'active': ['TT300', 'ZS400']},
     {'active': ['PT101', 'TT300', 'ZS400'], 'ack': True},
     {'active': ['PT101', 'TT300', 'ZS400']},
     {'active': ['ZS400'], 'ack': True},
     {'active': ['ZS400'], 'silence': True},
     {'active': ['ZS400']},
     {'active': ['ZS400']}]],
   [['red', False, False],
    ['red', False, True],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False]]],
  ['regression: scenario 4',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['PT101', 'TT300']},
     {'active': ['PT101', 'TT300']},
     {'active': ['TT300'], 'ack': True},
     {'active': ['PT101', 'TT300']},
     {'active': ['TT300', 'ZS400']},
     {'active': ['ZS400'], 'silence': True},
     {'active': ['PT101', 'ZS400'], 'ack': True},
     {'active': ['LT200', 'PT101', 'ZS400'], 'ack': True, 'silence': True},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']}]],
   [['red', True, True],
    ['red', True, True],
    ['amber', False, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, True]]],
  ['regression: escalation sequence',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['B']},
     {'active': ['B'], 'silence': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'ack': True},
     {'active': ['B']},
     {}]],
   [['amber', True, True],
    ['amber', True, False],
    ['red', True, True],
    ['red', False, False],
    ['amber', False, False],
    ['off', False, False]]],
  ['control: alarm clears unacknowledged',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {}, {'active': ['B']}]],
   [['red', True, True], ['off', False, False], ['amber', True, True]]],
  ['regression: low alarm while high acked',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A'], 'ack': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'silence': True},
     {'active': ['A', 'B']}]],
   [['red', False, False], ['red', False, True], ['red', False, False], ['red', False, False]]],
  ['regression: alarm present at first scan',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {'active': ['A']}]],
   [['red', True, True], ['red', True, True]]],
  ['control: silence then new low alarm',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A']}, {'active': ['A'], 'silence': True}, {'active': ['A', 'B']}]],
   [['red', True, True], ['red', True, False], ['red', True, True]]],
  ['regression: scenario 1',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': [], 'silence': True},
     {'active': [], 'silence': True},
     {'active': [], 'silence': True},
     {'active': []},
     {'active': ['ZS400']},
     {'active': ['ZS400'], 'ack': True},
     {'active': ['TT300']},
     {'active': ['PT101', 'TT300'], 'ack': True},
     {'active': ['PT101', 'TT300']},
     {'active': ['PT101', 'TT300'], 'silence': True},
     {'active': ['LT200', 'PT101', 'TT300'], 'silence': True},
     {'active': ['LT200', 'PT101', 'TT300']}]],
   [['off', False, False],
    ['off', False, False],
    ['off', False, False],
    ['off', False, False],
    ['red', True, True],
    ['red', False, False],
    ['amber', True, True],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(alarms, scans):
    level = dict(alarms)
    prev = set()
    unacked = set()
    silenced = False
    out = []
    for s in scans:
        active = set(s.get('active', []))
        new = active - prev
        if new:
            silenced = False
        unacked = (unacked | new) & active
        if s.get('ack'):
            unacked = set()
        if s.get('silence'):
            silenced = True
        if any(level[a] == 'high' for a in active):
            shown = 'high'
        elif active:
            shown = 'low'
        else:
            shown = None
        color = {'high': 'red', 'low': 'amber', None: 'off'}[shown]
        flashing = any(level[a] == shown for a in unacked)
        horn = bool(new) and not silenced
        prev = active
        out.append([color, flashing, horn])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: escalation sequence',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['B']},
     {'active': ['B'], 'silence': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'ack': True},
     {'active': ['B']},
     {}]],
   [['amber', True, True],
    ['amber', True, False],
    ['red', True, True],
    ['red', False, False],
    ['amber', False, False],
    ['off', False, False]]],
  ['regression: low alarm while high acked',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A'], 'ack': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'silence': True},
     {'active': ['A', 'B']}]],
   [['red', False, False], ['red', False, True], ['red', False, False], ['red', False, False]]],
  ['control: alarm clears unacknowledged',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {}, {'active': ['B']}]],
   [['red', True, True], ['off', False, False], ['amber', True, True]]],
  ['regression: alarm present at first scan',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {'active': ['A']}]],
   [['red', True, True], ['red', True, True]]],
  ['control: silence then new low alarm',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A']}, {'active': ['A'], 'silence': True}, {'active': ['A', 'B']}]],
   [['red', True, True], ['red', True, False], ['red', True, True]]],
  ['regression: scenario 1',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['TT300'], 'ack': True},
     {'active': ['TT300']},
     {'active': ['ZS400'], 'ack': True, 'silence': True},
     {'active': []},
     {'active': ['ZS400'], 'silence': True},
     {'active': ['TT300', 'ZS400']},
     {'active': ['LT200', 'TT300', 'ZS400']},
     {'active': ['LT200', 'TT300', 'ZS400']},
     {'active': ['LT200', 'ZS400'], 'ack': True}]],
   [['amber', False, False],
    ['amber', False, False],
    ['red', False, False],
    ['off', False, False],
    ['red', True, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', False, False]]],
  ['control: scenario 2',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['TT300'], 'silence': True},
     {'active': ['ZS400']},
     {'active': ['LT200', 'TT300']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']},
     {'active': ['LT200', 'PT101', 'TT300'], 'silence': True},
     {'active': ['LT200', 'PT101', 'TT300'], 'silence': True}]],
   [['amber', True, False],
    ['red', True, True],
    ['amber', True, True],
    ['red', True, True],
    ['red', True, False],
    ['red', True, False]]],
  ['regression: scenario 3',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': [], 'ack': True},
     {'active': []},
     {'active': ['ZS400']},
     {'active': ['ZS400']},
     {'active': ['LT200', 'PT101', 'ZS400'], 'silence': True},
     {'active': ['LT200', 'PT101'], 'ack': True}]],
   [['off', False, False],
    ['off', False, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, False],
    ['red', False, False]]]],
 [['regression: low alarm while high acked',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A'], 'ack': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'silence': True},
     {'active': ['A', 'B']}]],
   [['red', False, False], ['red', False, True], ['red', False, False], ['red', False, False]]],
  ['regression: scenario 1',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['ZS400'], 'silence': True},
     {'active': ['LT200', 'ZS400']},
     {'active': ['LT200', 'ZS400']},
     {'active': ['LT200', 'ZS400']},
     {'active': ['LT200', 'ZS400']},
     {'active': ['LT200']}]],
   [['red', True, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['amber', True, True]]],
  ['regression: escalation sequence',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['B']},
     {'active': ['B'], 'silence': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'ack': True},
     {'active': ['B']},
     {}]],
   [['amber', True, True],
    ['amber', True, False],
    ['red', True, True],
    ['red', False, False],
    ['amber', False, False],
    ['off', False, False]]],
  ['control: alarm clears unacknowledged',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {}, {'active': ['B']}]],
   [['red', True, True], ['off', False, False], ['amber', True, True]]],
  ['regression: alarm present at first scan',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {'active': ['A']}]],
   [['red', True, True], ['red', True, True]]],
  ['control: silence then new low alarm',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A']}, {'active': ['A'], 'silence': True}, {'active': ['A', 'B']}]],
   [['red', True, True], ['red', True, False], ['red', True, True]]],
  ['regression: scenario 2',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['PT101']},
     {'active': ['ZS400'], 'ack': True},
     {'active': ['TT300']},
     {'active': ['TT300']},
     {'active': ['PT101', 'TT300']},
     {'active': ['PT101']},
     {'active': ['LT200', 'ZS400']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']},
     {'active': ['LT200', 'TT300', 'ZS400']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']},
     {'active': ['LT200', 'TT300'], 'ack': True},
     {'active': ['LT200', 'TT300']}]],
   [['red', True, True],
    ['red', False, False],
    ['amber', True, True],
    ['amber', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['amber', False, False],
    ['amber', False, False]]],
  ['regression: scenario 3',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['LT200', 'PT101', 'ZS400']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']},
     {'active': ['LT200', 'PT101', 'ZS400'], 'ack': True},
     {'active': ['LT200', 'PT101']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']},
     {'active': ['ZS400']},
     {'active': ['PT101', 'ZS400']},
     {'active': ['PT101', 'ZS400'], 'ack': True},
     {'active': ['PT101', 'ZS400']}]],
   [['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', False, False],
    ['red', False, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', False, False],
    ['red', False, False]]]],
 [['regression: scenario 4',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['PT101'], 'silence': True},
     {'active': ['ZS400']},
     {'active': ['TT300']},
     {'active': ['TT300']},
     {'active': ['PT101']},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400'], 'ack': True}]],
   [['red', True, False],
    ['red', True, True],
    ['amber', True, True],
    ['amber', True, True],
    ['red', True, True],
    ['red', False, False]]],
  ['regression: scenario 1',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['PT101']},
     {'active': ['LT200', 'PT101']},
     {'active': ['LT200', 'PT101']},
     {'active': ['LT200', 'PT101'], 'silence': True},
     {'active': ['LT200', 'PT101']},
     {'active': []},
     {'active': []},
     {'active': []},
     {'active': [], 'ack': True},
     {'active': ['LT200']},
     {'active': ['LT200', 'TT300']}]],
   [['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, False],
    ['red', True, False],
    ['off', False, False],
    ['off', False, False],
    ['off', False, False],
    ['off', False, False],
    ['amber', True, True],
    ['amber', True, True]]],
  ['regression: escalation sequence',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['B']},
     {'active': ['B'], 'silence': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'ack': True},
     {'active': ['B']},
     {}]],
   [['amber', True, True],
    ['amber', True, False],
    ['red', True, True],
    ['red', False, False],
    ['amber', False, False],
    ['off', False, False]]],
  ['control: alarm clears unacknowledged',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {}, {'active': ['B']}]],
   [['red', True, True], ['off', False, False], ['amber', True, True]]],
  ['regression: low alarm while high acked',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A'], 'ack': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'silence': True},
     {'active': ['A', 'B']}]],
   [['red', False, False], ['red', False, True], ['red', False, False], ['red', False, False]]],
  ['regression: alarm present at first scan',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {'active': ['A']}]],
   [['red', True, True], ['red', True, True]]],
  ['control: silence then new low alarm',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A']}, {'active': ['A'], 'silence': True}, {'active': ['A', 'B']}]],
   [['red', True, True], ['red', True, False], ['red', True, True]]],
  ['regression: scenario 2',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': []},
     {'active': ['ZS400']},
     {'active': ['ZS400']},
     {'active': ['ZS400'], 'silence': True},
     {'active': ['ZS400']},
     {'active': []},
     {'active': ['LT200', 'ZS400']},
     {'active': ['LT200', 'PT101', 'ZS400']},
     {'active': ['PT101', 'TT300', 'ZS400']}]],
   [['off', False, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, False],
    ['red', True, False],
    ['off', False, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True]]]],
 [['regression: scenario 2',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': []},
     {'active': ['PT101', 'TT300']},
     {'active': ['PT101', 'TT300'], 'ack': True},
     {'active': ['PT101', 'ZS400'], 'silence': True},
     {'active': ['PT101']},
     {'active': ['PT101'], 'silence': True}]],
   [['off', False, False],
    ['red', True, True],
    ['red', False, False],
    ['red', True, False],
    ['red', False, False],
    ['red', False, False]]],
  ['regression: scenario 4',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['LT200'], 'silence': True},
     {'active': ['PT101', 'TT300']},
     {'active': ['PT101', 'TT300', 'ZS400']},
     {'active': ['ZS400']},
     {'active': ['PT101']},
     {'active': ['LT200', 'PT101', 'TT300']}]],
   [['amber', True, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True],
    ['red', True, True]]],
  ['regression: escalation sequence',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['B']},
     {'active': ['B'], 'silence': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'ack': True},
     {'active': ['B']},
     {}]],
   [['amber', True, True],
    ['amber', True, False],
    ['red', True, True],
    ['red', False, False],
    ['amber', False, False],
    ['off', False, False]]],
  ['control: alarm clears unacknowledged',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {}, {'active': ['B']}]],
   [['red', True, True], ['off', False, False], ['amber', True, True]]],
  ['regression: low alarm while high acked',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A'], 'ack': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'silence': True},
     {'active': ['A', 'B']}]],
   [['red', False, False], ['red', False, True], ['red', False, False], ['red', False, False]]],
  ['regression: alarm present at first scan',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {'active': ['A']}]],
   [['red', True, True], ['red', True, True]]],
  ['control: silence then new low alarm',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A']}, {'active': ['A'], 'silence': True}, {'active': ['A', 'B']}]],
   [['red', True, True], ['red', True, False], ['red', True, True]]],
  ['regression: scenario 1',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['PT101'], 'ack': True},
     {'active': ['PT101']},
     {'active': ['PT101'], 'ack': True},
     {'active': ['PT101']},
     {'active': ['PT101']},
     {'active': ['LT200', 'PT101', 'TT300']},
     {'active': ['LT200', 'PT101', 'TT300'], 'ack': True},
     {'active': ['LT200', 'TT300']}]],
   [['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, True],
    ['red', False, False],
    ['amber', False, False]]]],
 [['regression: scenario 3',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['ZS400'], 'ack': True},
     {'active': ['TT300', 'ZS400']},
     {'active': ['PT101', 'TT300', 'ZS400'], 'ack': True},
     {'active': ['PT101', 'TT300', 'ZS400']},
     {'active': ['ZS400'], 'ack': True},
     {'active': ['ZS400'], 'silence': True},
     {'active': ['ZS400']},
     {'active': ['ZS400']}]],
   [['red', False, False],
    ['red', False, True],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False]]],
  ['regression: scenario 4',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': ['PT101', 'TT300']},
     {'active': ['PT101', 'TT300']},
     {'active': ['TT300'], 'ack': True},
     {'active': ['PT101', 'TT300']},
     {'active': ['TT300', 'ZS400']},
     {'active': ['ZS400'], 'silence': True},
     {'active': ['PT101', 'ZS400'], 'ack': True},
     {'active': ['LT200', 'PT101', 'ZS400'], 'ack': True, 'silence': True},
     {'active': ['LT200', 'PT101', 'TT300', 'ZS400']}]],
   [['red', True, True],
    ['red', True, True],
    ['amber', False, False],
    ['red', True, True],
    ['red', True, True],
    ['red', True, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, True]]],
  ['regression: escalation sequence',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['B']},
     {'active': ['B'], 'silence': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'ack': True},
     {'active': ['B']},
     {}]],
   [['amber', True, True],
    ['amber', True, False],
    ['red', True, True],
    ['red', False, False],
    ['amber', False, False],
    ['off', False, False]]],
  ['control: alarm clears unacknowledged',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {}, {'active': ['B']}]],
   [['red', True, True], ['off', False, False], ['amber', True, True]]],
  ['regression: low alarm while high acked',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A'], 'ack': True},
     {'active': ['A', 'B']},
     {'active': ['A', 'B'], 'silence': True},
     {'active': ['A', 'B']}]],
   [['red', False, False], ['red', False, True], ['red', False, False], ['red', False, False]]],
  ['regression: alarm present at first scan',
   [[['A', 'high'], ['B', 'low']], [{'active': ['A']}, {'active': ['A']}]],
   [['red', True, True], ['red', True, True]]],
  ['control: silence then new low alarm',
   [[['A', 'high'], ['B', 'low']],
    [{'active': ['A']}, {'active': ['A'], 'silence': True}, {'active': ['A', 'B']}]],
   [['red', True, True], ['red', True, False], ['red', True, True]]],
  ['regression: scenario 1',
   [[['PT101', 'high'], ['LT200', 'low'], ['TT300', 'low'], ['ZS400', 'high']],
    [{'active': [], 'silence': True},
     {'active': [], 'silence': True},
     {'active': [], 'silence': True},
     {'active': []},
     {'active': ['ZS400']},
     {'active': ['ZS400'], 'ack': True},
     {'active': ['TT300']},
     {'active': ['PT101', 'TT300'], 'ack': True},
     {'active': ['PT101', 'TT300']},
     {'active': ['PT101', 'TT300'], 'silence': True},
     {'active': ['LT200', 'PT101', 'TT300'], 'silence': True},
     {'active': ['LT200', 'PT101', 'TT300']}]],
   [['off', False, False],
    ['off', False, False],
    ['off', False, False],
    ['off', False, False],
    ['red', True, True],
    ['red', False, False],
    ['amber', True, True],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False],
    ['red', False, False]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Acknowledging alarms does not stop the horn while they remain active.

Stack light and horn logic. alarms is a list of [name, high|low]; each scan gives the active alarm names plus optional ack and silence buttons. An alarm becomes unacknowledged on its inactive-to-active transition and stops being unacknowledged when it clears or ack is pressed. Any new activation cancels silence; silence (pressed after that) mutes the horn. The lamp is red if any high alarm is active, else amber if any alarm is active, else off; it flashes if an unacknowledged alarm of the displayed level exists. The horn sounds while any unacknowledged alarm exists and not silenced. Return [color, flashing, horn] per scan.

split: open-access


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

N = 1
observations = []
def solve(clamp, ops):
    trap = False
    out = []
    for a, b, op, clear in ops:
        if op == 'ADD':
            r = a + b
        elif op == 'SUB':
            r = a - b
        else:
            r = a * b
        v = not (-32768 <= r <= 32767)
        if v:
            trap = True
            if clamp:
                r = 32767 if r > 0 else -32768
            else:
                r = r % 65536
        if clear:
            trap = False
        out.append([r, v, trap])
        if trap:
            out.append('halted')
            break
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['regression: scenario 1',
   [False,
    [[-32768, -20192, 'SUB', True],
     [12968, -30310, 'ADD', False],
     [-31305, 2, 'SUB', False],
     [11091, 200, 'MUL', True],
     [10310, 200, 'SUB', False]]],
   [[-12576, False, False],
    [-17342, False, False],
    [-31307, False, False],
    [-10024, True, False],
    [10110, False, False]]],
  ['control: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 2',
   [True,
    [[27834, -6307, 'SUB', False],
     [30000, -15473, 'SUB', False],
     [-1, 1, 'MUL', False],
     [-30000, -4507, 'SUB', True],
     [-21177, 21481, 'MUL', False],
     [31159, 1, 'SUB', False]]],
   [[32767, True, True], 'halted']],
  ['regression: scenario 3',
   [False,
    [[18788, 200, 'SUB', True],
     [20369, -23104, 'MUL', False],
     [-32768, 10449, 'ADD', False],
     [-17550, 18799, 'ADD', False],
     [1, 32767, 'MUL', False]]],
   [[18588, False, False], [8640, True, True], 'halted']],
  ['regression: scenario 4',
   [False,
    [[30000, 1, 'ADD', True],
     [-20338, 30000, 'ADD', True],
     [18879, 1, 'SUB', True],
     [27305, -32768, 'MUL', True],
     [16107, -9800, 'MUL', False],
     [3999, -32768, 'SUB', False],
     [1, -21623, 'ADD', False]]],
   [[30001, False, False],
    [9662, False, False],
    [18878, False, False],
    [-32768, True, False],
    [27624, True, True],
    'halted']]],
 [['regression: scenario 4',
   [False,
    [[200, -21485, 'MUL', True],
     [-200, -32768, 'SUB', True],
     [15677, 32607, 'ADD', True],
     [-10557, 26430, 'SUB', False],
     [-200, 30000, 'SUB', True],
     [-30000, 200, 'ADD', True],
     [26687, -20358, 'ADD', False]]],
   [[28376, True, False], [32568, False, False], [-17252, True, False], [28549, True, True], 'halted']],
  ['regression: scenario 1',
   [False,
    [[-15041, -14459, 'SUB', False],
     [13071, -30000, 'ADD', True],
     [-17526, -1, 'MUL', True],
     [-30000, 200, 'MUL', False]]],
   [[-582, False, False], [-16929, False, False], [17526, False, False], [29312, True, True], 'halted']],
  ['regression: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['control: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 2',
   [True,
    [[-1048, 30000, 'ADD', False],
     [2, -13120, 'SUB', False],
     [29306, -32768, 'SUB', True],
     [-9919, -24931, 'MUL', True]]],
   [[28952, False, False], [13122, False, False], [32767, True, False], [32767, True, False]]],
  ['control: scenario 3',
   [False,
    [[-10436, -32034, 'MUL', True],
     [2, 22108, 'SUB', False],
     [7659, 2, 'MUL', False],
     [5339, 1, 'MUL', False]]],
   [[7688, True, False], [-22106, False, False], [15318, False, False], [5339, False, False]]]],
 [['regression: scenario 2',
   [False,
    [[-200, -200, 'ADD', True],
     [32767, 200, 'MUL', False],
     [-9390, 1, 'MUL', False],
     [-11169, -28064, 'ADD', True],
     [22720, -1, 'ADD', False],
     [-200, -1, 'ADD', False],
     [-12859, 1, 'ADD', True]]],
   [[-400, False, False], [-200, True, True], 'halted']],
  ['regression: scenario 10',
   [False,
    [[32767, -28982, 'ADD', False],
     [-30000, 200, 'MUL', False],
     [-1, 2660, 'SUB', True],
     [-200, -13460, 'MUL', False],
     [1, -9677, 'MUL', True],
     [-1, 14598, 'SUB', True],
     [-32768, 1, 'ADD', False]]],
   [[3785, False, False], [29312, True, True], 'halted']],
  ['regression: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['control: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['regression: scenario 1',
   [False,
    [[15094, -24454, 'MUL', False],
     [-30000, 24363, 'MUL', True],
     [2, -14871, 'MUL', False],
     [1, -26550, 'MUL', True],
     [-1, -7090, 'SUB', False],
     [32767, 1, 'MUL', True],
     [-32768, 15967, 'ADD', False],
     [21457, -28729, 'SUB', True]]],
   [[-9924, True, True], 'halted']],
  ['control: scenario 3',
   [True,
    [[200, 30000, 'ADD', True],
     [11009, -200, 'SUB', True],
     [-200, 1, 'MUL', False],
     [-20841, 21280, 'ADD', True]]],
   [[30200, False, False], [11209, False, False], [-200, False, False], [439, False, False]]]],
 [['regression: scenario 8',
   [False,
    [[200, -32768, 'MUL', True],
     [30000, 32767, 'MUL', False],
     [12166, 30000, 'MUL', True],
     [-1, -23798, 'ADD', False],
     [-1, 32767, 'SUB', True]]],
   [[0, True, False], [-30000, True, True], 'halted']],
  ['regression: scenario 3',
   [False,
    [[-32768, 19187, 'MUL', True],
     [-7301, -200, 'SUB', False],
     [-32768, 30000, 'SUB', True],
     [-9747, -24186, 'ADD', True],
     [-1, -22920, 'SUB', True],
     [-259, 22293, 'SUB', False],
     [23494, -1, 'MUL', False],
     [-32529, 24353, 'SUB', True]]],
   [[-32768, True, False],
    [-7101, False, False],
    [2768, True, False],
    [31603, True, False],
    [22919, False, False],
    [-22552, False, False],
    [-23494, False, False],
    [8654, True, False]]],
  ['regression: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['control: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['regression: scenario 1',
   [False,
    [[-26759, 2, 'SUB', True],
     [-30000, -32768, 'SUB', False],
     [-3730, -31438, 'ADD', False],
     [200, 200, 'SUB', True],
     [-24761, 200, 'SUB', True],
     [-20747, 200, 'SUB', False],
     [200, 19789, 'SUB', True]]],
   [[-26761, False, False], [2768, False, False], [30368, True, True], 'halted']],
  ['regression: scenario 2',
   [False,
    [[30000, -32768, 'ADD', True],
     [4451, 31437, 'SUB', False],
     [-1, 22093, 'MUL', False],
     [-30000, -18488, 'SUB', False],
     [30000, -28921, 'MUL', True],
     [-200, 23349, 'ADD', True],
     [-32768, 200, 'SUB', False]]],
   [[-2768, False, False],
    [-26986, False, False],
    [-22093, False, False],
    [-11512, False, False],
    [1104, True, False],
    [23149, False, False],
    [32568, True, True],
    'halted']]],
 [['regression: scenario 19',
   [False,
    [[5524, -20977, 'SUB', True],
     [30000, 30000, 'ADD', False],
     [8864, -30000, 'MUL', False],
     [-32768, -11188, 'MUL', False],
     [200, 32767, 'MUL', True]]],
   [[26501, False, False], [-5536, True, True], 'halted']],
  ['regression: scenario 11',
   [False,
    [[200, -200, 'SUB', False],
     [-200, -200, 'SUB', False],
     [-200, 30000, 'MUL', True],
     [-14430, -32768, 'ADD', False]]],
   [[400, False, False], [0, False, False], [29312, True, False], [18338, True, True], 'halted']],
  ['regression: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['control: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 1',
   [True,
    [[160, 2, 'SUB', True], [200, 200, 'SUB', True], [222, 200, 'SUB', False], [200, -32768, 'ADD', True]]],
   [[158, False, False], [0, False, False], [22, False, False], [-32568, False, False]]],
  ['control: scenario 2',
   [True,
    [[30000, 1574, 'SUB', True],
     [-783, -29319, 'SUB', True],
     [2, 1803, 'SUB', False],
     [200, 2, 'ADD', True],
     [9942, 32767, 'MUL', True],
     [-30000, -19084, 'MUL', True],
     [-10106, -30460, 'ADD', False],
     [2, 32767, 'ADD', True]]],
   [[28426, False, False],
    [28536, False, False],
    [-1801, False, False],
    [202, False, False],
    [32767, True, False],
    [32767, True, False],
    [-32768, True, True],
    'halted']]]]
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(clamp, ops):
    trap = False
    out = []
    for a, b, op, clear in ops:
        if op == 'ADD':
            r = a + b
        elif op == 'SUB':
            r = a - b
        else:
            r = a * b
        v = not (-32768 <= r <= 32767)
        if v:
            trap = True
            if clamp:
                r = 32767 if r > 0 else -32768
            else:
                r = -((-r) % 65536) if r < 0 else r % 65536
        if clear:
            trap = False
        out.append([r, v, trap])
        if trap:
            out.append('halted')
            break
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['regression: scenario 1',
   [False,
    [[-32768, -20192, 'SUB', True],
     [12968, -30310, 'ADD', False],
     [-31305, 2, 'SUB', False],
     [11091, 200, 'MUL', True],
     [10310, 200, 'SUB', False]]],
   [[-12576, False, False],
    [-17342, False, False],
    [-31307, False, False],
    [-10024, True, False],
    [10110, False, False]]],
  ['control: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 2',
   [True,
    [[27834, -6307, 'SUB', False],
     [30000, -15473, 'SUB', False],
     [-1, 1, 'MUL', False],
     [-30000, -4507, 'SUB', True],
     [-21177, 21481, 'MUL', False],
     [31159, 1, 'SUB', False]]],
   [[32767, True, True], 'halted']],
  ['regression: scenario 3',
   [False,
    [[18788, 200, 'SUB', True],
     [20369, -23104, 'MUL', False],
     [-32768, 10449, 'ADD', False],
     [-17550, 18799, 'ADD', False],
     [1, 32767, 'MUL', False]]],
   [[18588, False, False], [8640, True, True], 'halted']],
  ['regression: scenario 4',
   [False,
    [[30000, 1, 'ADD', True],
     [-20338, 30000, 'ADD', True],
     [18879, 1, 'SUB', True],
     [27305, -32768, 'MUL', True],
     [16107, -9800, 'MUL', False],
     [3999, -32768, 'SUB', False],
     [1, -21623, 'ADD', False]]],
   [[30001, False, False],
    [9662, False, False],
    [18878, False, False],
    [-32768, True, False],
    [27624, True, True],
    'halted']]],
 [['regression: scenario 4',
   [False,
    [[200, -21485, 'MUL', True],
     [-200, -32768, 'SUB', True],
     [15677, 32607, 'ADD', True],
     [-10557, 26430, 'SUB', False],
     [-200, 30000, 'SUB', True],
     [-30000, 200, 'ADD', True],
     [26687, -20358, 'ADD', False]]],
   [[28376, True, False], [32568, False, False], [-17252, True, False], [28549, True, True], 'halted']],
  ['regression: scenario 1',
   [False,
    [[-15041, -14459, 'SUB', False],
     [13071, -30000, 'ADD', True],
     [-17526, -1, 'MUL', True],
     [-30000, 200, 'MUL', False]]],
   [[-582, False, False], [-16929, False, False], [17526, False, False], [29312, True, True], 'halted']],
  ['regression: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['control: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 2',
   [True,
    [[-1048, 30000, 'ADD', False],
     [2, -13120, 'SUB', False],
     [29306, -32768, 'SUB', True],
     [-9919, -24931, 'MUL', True]]],
   [[28952, False, False], [13122, False, False], [32767, True, False], [32767, True, False]]],
  ['control: scenario 3',
   [False,
    [[-10436, -32034, 'MUL', True],
     [2, 22108, 'SUB', False],
     [7659, 2, 'MUL', False],
     [5339, 1, 'MUL', False]]],
   [[7688, True, False], [-22106, False, False], [15318, False, False], [5339, False, False]]]],
 [['regression: scenario 2',
   [False,
    [[-200, -200, 'ADD', True],
     [32767, 200, 'MUL', False],
     [-9390, 1, 'MUL', False],
     [-11169, -28064, 'ADD', True],
     [22720, -1, 'ADD', False],
     [-200, -1, 'ADD', False],
     [-12859, 1, 'ADD', True]]],
   [[-400, False, False], [-200, True, True], 'halted']],
  ['regression: scenario 10',
   [False,
    [[32767, -28982, 'ADD', False],
     [-30000, 200, 'MUL', False],
     [-1, 2660, 'SUB', True],
     [-200, -13460, 'MUL', False],
     [1, -9677, 'MUL', True],
     [-1, 14598, 'SUB', True],
     [-32768, 1, 'ADD', False]]],
   [[3785, False, False], [29312, True, True], 'halted']],
  ['regression: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['control: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['regression: scenario 1',
   [False,
    [[15094, -24454, 'MUL', False],
     [-30000, 24363, 'MUL', True],
     [2, -14871, 'MUL', False],
     [1, -26550, 'MUL', True],
     [-1, -7090, 'SUB', False],
     [32767, 1, 'MUL', True],
     [-32768, 15967, 'ADD', False],
     [21457, -28729, 'SUB', True]]],
   [[-9924, True, True], 'halted']],
  ['control: scenario 3',
   [True,
    [[200, 30000, 'ADD', True],
     [11009, -200, 'SUB', True],
     [-200, 1, 'MUL', False],
     [-20841, 21280, 'ADD', True]]],
   [[30200, False, False], [11209, False, False], [-200, False, False], [439, False, False]]]],
 [['regression: scenario 8',
   [False,
    [[200, -32768, 'MUL', True],
     [30000, 32767, 'MUL', False],
     [12166, 30000, 'MUL', True],
     [-1, -23798, 'ADD', False],
     [-1, 32767, 'SUB', True]]],
   [[0, True, False], [-30000, True, True], 'halted']],
  ['regression: scenario 3',
   [False,
    [[-32768, 19187, 'MUL', True],
     [-7301, -200, 'SUB', False],
     [-32768, 30000, 'SUB', True],
     [-9747, -24186, 'ADD', True],
     [-1, -22920, 'SUB', True],
     [-259, 22293, 'SUB', False],
     [23494, -1, 'MUL', False],
     [-32529, 24353, 'SUB', True]]],
   [[-32768, True, False],
    [-7101, False, False],
    [2768, True, False],
    [31603, True, False],
    [22919, False, False],
    [-22552, False, False],
    [-23494, False, False],
    [8654, True, False]]],
  ['regression: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['control: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['regression: scenario 1',
   [False,
    [[-26759, 2, 'SUB', True],
     [-30000, -32768, 'SUB', False],
     [-3730, -31438, 'ADD', False],
     [200, 200, 'SUB', True],
     [-24761, 200, 'SUB', True],
     [-20747, 200, 'SUB', False],
     [200, 19789, 'SUB', True]]],
   [[-26761, False, False], [2768, False, False], [30368, True, True], 'halted']],
  ['regression: scenario 2',
   [False,
    [[30000, -32768, 'ADD', True],
     [4451, 31437, 'SUB', False],
     [-1, 22093, 'MUL', False],
     [-30000, -18488, 'SUB', False],
     [30000, -28921, 'MUL', True],
     [-200, 23349, 'ADD', True],
     [-32768, 200, 'SUB', False]]],
   [[-2768, False, False],
    [-26986, False, False],
    [-22093, False, False],
    [-11512, False, False],
    [1104, True, False],
    [23149, False, False],
    [32568, True, True],
    'halted']]],
 [['regression: scenario 19',
   [False,
    [[5524, -20977, 'SUB', True],
     [30000, 30000, 'ADD', False],
     [8864, -30000, 'MUL', False],
     [-32768, -11188, 'MUL', False],
     [200, 32767, 'MUL', True]]],
   [[26501, False, False], [-5536, True, True], 'halted']],
  ['regression: scenario 11',
   [False,
    [[200, -200, 'SUB', False],
     [-200, -200, 'SUB', False],
     [-200, 30000, 'MUL', True],
     [-14430, -32768, 'ADD', False]]],
   [[400, False, False], [0, False, False], [29312, True, False], [18338, True, True], 'halted']],
  ['regression: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['control: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 1',
   [True,
    [[160, 2, 'SUB', True], [200, 200, 'SUB', True], [222, 200, 'SUB', False], [200, -32768, 'ADD', True]]],
   [[158, False, False], [0, False, False], [22, False, False], [-32568, False, False]]],
  ['control: scenario 2',
   [True,
    [[30000, 1574, 'SUB', True],
     [-783, -29319, 'SUB', True],
     [2, 1803, 'SUB', False],
     [200, 2, 'ADD', True],
     [9942, 32767, 'MUL', True],
     [-30000, -19084, 'MUL', True],
     [-10106, -30460, 'ADD', False],
     [2, 32767, 'ADD', True]]],
   [[28426, False, False],
    [28536, False, False],
    [-1801, False, False],
    [202, False, False],
    [32767, True, False],
    [32767, True, False],
    [-32768, True, True],
    'halted']]]]
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:
Unclamped overflow stores a positive value above 32767 instead of the negative two's-complement result.

16-bit INT math rung followed by a later fault-clearing rung, one op per scan; ops is a list of [a, b, ADD|SUB|MUL, clear]. A result outside -32768..32767 sets the overflow bit V for that instruction and the overflow trap; the stored result is saturated to 32767/-32768 when clamp is set or wrapped as 16-bit two's complement otherwise. V is recomputed per instruction. The later rung clears the trap when clear is true; a trap still set at end of scan halts the processor. Return [result, V, trap] per scan, then "halted" and stop if faulted.

split: open-access


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

N = 1
observations = []
def solve(clamp, ops):
    trap = False
    out = []
    for a, b, op, clear in ops:
        if op == 'ADD':
            r = a + b
        elif op == 'SUB':
            r = a - b
        else:
            r = a * b
        v = not (-32767 <= r <= 32767)
        if v:
            trap = True
            if clamp:
                r = 32767 if r > 0 else -32768
            else:
                r = (r + 32768) % 65536 - 32768
        if clear:
            trap = False
        out.append([r, v, trap])
        if trap:
            out.append('halted')
            break
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['regression: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 1',
   [False,
    [[-32768, -20192, 'SUB', True],
     [12968, -30310, 'ADD', False],
     [-31305, 2, 'SUB', False],
     [11091, 200, 'MUL', True],
     [10310, 200, 'SUB', False]]],
   [[-12576, False, False],
    [-17342, False, False],
    [-31307, False, False],
    [-10024, True, False],
    [10110, False, False]]],
  ['control: scenario 2',
   [True,
    [[27834, -6307, 'SUB', False],
     [30000, -15473, 'SUB', False],
     [-1, 1, 'MUL', False],
     [-30000, -4507, 'SUB', True],
     [-21177, 21481, 'MUL', False],
     [31159, 1, 'SUB', False]]],
   [[32767, True, True], 'halted']],
  ['control: scenario 3',
   [False,
    [[18788, 200, 'SUB', True],
     [20369, -23104, 'MUL', False],
     [-32768, 10449, 'ADD', False],
     [-17550, 18799, 'ADD', False],
     [1, 32767, 'MUL', False]]],
   [[18588, False, False], [8640, True, True], 'halted']],
  ['control: scenario 4',
   [False,
    [[30000, 1, 'ADD', True],
     [-20338, 30000, 'ADD', True],
     [18879, 1, 'SUB', True],
     [27305, -32768, 'MUL', True],
     [16107, -9800, 'MUL', False],
     [3999, -32768, 'SUB', False],
     [1, -21623, 'ADD', False]]],
   [[30001, False, False],
    [9662, False, False],
    [18878, False, False],
    [-32768, True, False],
    [27624, True, True],
    'halted']]],
 [['regression: scenario 46',
   [True,
    [[2, 2, 'ADD', False],
     [-30000, -9133, 'MUL', True],
     [2, -30000, 'SUB', False],
     [-1, 2, 'ADD', True],
     [1, -32768, 'MUL', True],
     [32767, -1242, 'ADD', True],
     [6977, 13360, 'SUB', False]]],
   [[4, False, False],
    [32767, True, False],
    [30002, False, False],
    [1, False, False],
    [-32768, False, False],
    [31525, False, False],
    [-6383, False, False]]],
  ['regression: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['regression: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 1',
   [False,
    [[-15041, -14459, 'SUB', False],
     [13071, -30000, 'ADD', True],
     [-17526, -1, 'MUL', True],
     [-30000, 200, 'MUL', False]]],
   [[-582, False, False], [-16929, False, False], [17526, False, False], [29312, True, True], 'halted']],
  ['control: scenario 2',
   [True,
    [[-1048, 30000, 'ADD', False],
     [2, -13120, 'SUB', False],
     [29306, -32768, 'SUB', True],
     [-9919, -24931, 'MUL', True]]],
   [[28952, False, False], [13122, False, False], [32767, True, False], [32767, True, False]]],
  ['control: scenario 3',
   [False,
    [[-10436, -32034, 'MUL', True],
     [2, 22108, 'SUB', False],
     [7659, 2, 'MUL', False],
     [5339, 1, 'MUL', False]]],
   [[7688, True, False], [-22106, False, False], [15318, False, False], [5339, False, False]]]],
 [['regression: scenario 51',
   [False,
    [[1, -32768, 'MUL', False],
     [-200, 20989, 'MUL', False],
     [-1, 1541, 'MUL', False],
     [-11038, 32767, 'ADD', False]]],
   [[-32768, False, False], [-3496, True, True], 'halted']],
  ['regression: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['regression: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 1',
   [False,
    [[15094, -24454, 'MUL', False],
     [-30000, 24363, 'MUL', True],
     [2, -14871, 'MUL', False],
     [1, -26550, 'MUL', True],
     [-1, -7090, 'SUB', False],
     [32767, 1, 'MUL', True],
     [-32768, 15967, 'ADD', False],
     [21457, -28729, 'SUB', True]]],
   [[-9924, True, True], 'halted']],
  ['control: scenario 2',
   [False,
    [[-200, -200, 'ADD', True],
     [32767, 200, 'MUL', False],
     [-9390, 1, 'MUL', False],
     [-11169, -28064, 'ADD', True],
     [22720, -1, 'ADD', False],
     [-200, -1, 'ADD', False],
     [-12859, 1, 'ADD', True]]],
   [[-400, False, False], [-200, True, True], 'halted']],
  ['control: scenario 3',
   [True,
    [[200, 30000, 'ADD', True],
     [11009, -200, 'SUB', True],
     [-200, 1, 'MUL', False],
     [-20841, 21280, 'ADD', True]]],
   [[30200, False, False], [11209, False, False], [-200, False, False], [439, False, False]]]],
 [['regression: scenario 43',
   [True,
    [[-32768, 1, 'SUB', True],
     [1, -32768, 'MUL', True],
     [-30000, -3339, 'SUB', False],
     [200, -200, 'SUB', True],
     [16985, 2, 'MUL', True],
     [15251, 24317, 'MUL', False],
     [10553, -32768, 'ADD', True]]],
   [[-32768, True, False],
    [-32768, False, False],
    [-26661, False, False],
    [400, False, False],
    [32767, True, False],
    [32767, True, True],
    'halted']],
  ['regression: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['regression: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 1',
   [False,
    [[-26759, 2, 'SUB', True],
     [-30000, -32768, 'SUB', False],
     [-3730, -31438, 'ADD', False],
     [200, 200, 'SUB', True],
     [-24761, 200, 'SUB', True],
     [-20747, 200, 'SUB', False],
     [200, 19789, 'SUB', True]]],
   [[-26761, False, False], [2768, False, False], [30368, True, True], 'halted']],
  ['control: scenario 2',
   [False,
    [[30000, -32768, 'ADD', True],
     [4451, 31437, 'SUB', False],
     [-1, 22093, 'MUL', False],
     [-30000, -18488, 'SUB', False],
     [30000, -28921, 'MUL', True],
     [-200, 23349, 'ADD', True],
     [-32768, 200, 'SUB', False]]],
   [[-2768, False, False],
    [-26986, False, False],
    [-22093, False, False],
    [-11512, False, False],
    [1104, True, False],
    [23149, False, False],
    [32568, True, True],
    'halted']],
  ['control: scenario 3',
   [False,
    [[-32768, 19187, 'MUL', True],
     [-7301, -200, 'SUB', False],
     [-32768, 30000, 'SUB', True],
     [-9747, -24186, 'ADD', True],
     [-1, -22920, 'SUB', True],
     [-259, 22293, 'SUB', False],
     [23494, -1, 'MUL', False],
     [-32529, 24353, 'SUB', True]]],
   [[-32768, True, False],
    [-7101, False, False],
    [2768, True, False],
    [31603, True, False],
    [22919, False, False],
    [-22552, False, False],
    [-23494, False, False],
    [8654, True, False]]]],
 [['regression: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['regression: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 1',
   [True,
    [[160, 2, 'SUB', True], [200, 200, 'SUB', True], [222, 200, 'SUB', False], [200, -32768, 'ADD', True]]],
   [[158, False, False], [0, False, False], [22, False, False], [-32568, False, False]]],
  ['control: scenario 2',
   [True,
    [[30000, 1574, 'SUB', True],
     [-783, -29319, 'SUB', True],
     [2, 1803, 'SUB', False],
     [200, 2, 'ADD', True],
     [9942, 32767, 'MUL', True],
     [-30000, -19084, 'MUL', True],
     [-10106, -30460, 'ADD', False],
     [2, 32767, 'ADD', True]]],
   [[28426, False, False],
    [28536, False, False],
    [-1801, False, False],
    [202, False, False],
    [32767, True, False],
    [32767, True, False],
    [-32768, True, True],
    'halted']],
  ['control: scenario 3',
   [False,
    [[4437, 15506, 'ADD', True],
     [32767, 1, 'SUB', False],
     [-7256, 11958, 'ADD', True],
     [1, 11416, 'MUL', True]]],
   [[19943, False, False], [32766, False, False], [4702, False, False], [11416, False, False]]],
  ['control: scenario 4',
   [False,
    [[19188, -11432, 'MUL', True],
     [-32768, 7005, 'SUB', True],
     [-200, 1, 'ADD', True],
     [32767, -4687, 'MUL', False],
     [7857, 5828, 'ADD', True],
     [-11826, -1129, 'MUL', False],
     [-1, 1, 'MUL', False],
     [-24957, -5100, 'ADD', True]]],
   [[-8224, True, False], [25763, True, False], [-199, False, False], [-28081, True, True], 'halted']]]]
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(clamp, ops):
    trap = False
    out = []
    for a, b, op, clear in ops:
        if op == 'ADD':
            r = a + b
        elif op == 'SUB':
            r = a - b
        else:
            r = a * b
        v = not (-32768 <= r < 32767)
        if v:
            trap = True
            if clamp:
                r = 32767 if r > 0 else -32768
            else:
                r = (r + 32768) % 65536 - 32768
        if clear:
            trap = False
        out.append([r, v, trap])
        if trap:
            out.append('halted')
            break
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['regression: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 1',
   [False,
    [[-32768, -20192, 'SUB', True],
     [12968, -30310, 'ADD', False],
     [-31305, 2, 'SUB', False],
     [11091, 200, 'MUL', True],
     [10310, 200, 'SUB', False]]],
   [[-12576, False, False],
    [-17342, False, False],
    [-31307, False, False],
    [-10024, True, False],
    [10110, False, False]]],
  ['control: scenario 2',
   [True,
    [[27834, -6307, 'SUB', False],
     [30000, -15473, 'SUB', False],
     [-1, 1, 'MUL', False],
     [-30000, -4507, 'SUB', True],
     [-21177, 21481, 'MUL', False],
     [31159, 1, 'SUB', False]]],
   [[32767, True, True], 'halted']],
  ['control: scenario 3',
   [False,
    [[18788, 200, 'SUB', True],
     [20369, -23104, 'MUL', False],
     [-32768, 10449, 'ADD', False],
     [-17550, 18799, 'ADD', False],
     [1, 32767, 'MUL', False]]],
   [[18588, False, False], [8640, True, True], 'halted']],
  ['control: scenario 4',
   [False,
    [[30000, 1, 'ADD', True],
     [-20338, 30000, 'ADD', True],
     [18879, 1, 'SUB', True],
     [27305, -32768, 'MUL', True],
     [16107, -9800, 'MUL', False],
     [3999, -32768, 'SUB', False],
     [1, -21623, 'ADD', False]]],
   [[30001, False, False],
    [9662, False, False],
    [18878, False, False],
    [-32768, True, False],
    [27624, True, True],
    'halted']]],
 [['regression: scenario 46',
   [True,
    [[2, 2, 'ADD', False],
     [-30000, -9133, 'MUL', True],
     [2, -30000, 'SUB', False],
     [-1, 2, 'ADD', True],
     [1, -32768, 'MUL', True],
     [32767, -1242, 'ADD', True],
     [6977, 13360, 'SUB', False]]],
   [[4, False, False],
    [32767, True, False],
    [30002, False, False],
    [1, False, False],
    [-32768, False, False],
    [31525, False, False],
    [-6383, False, False]]],
  ['regression: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['regression: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 1',
   [False,
    [[-15041, -14459, 'SUB', False],
     [13071, -30000, 'ADD', True],
     [-17526, -1, 'MUL', True],
     [-30000, 200, 'MUL', False]]],
   [[-582, False, False], [-16929, False, False], [17526, False, False], [29312, True, True], 'halted']],
  ['control: scenario 2',
   [True,
    [[-1048, 30000, 'ADD', False],
     [2, -13120, 'SUB', False],
     [29306, -32768, 'SUB', True],
     [-9919, -24931, 'MUL', True]]],
   [[28952, False, False], [13122, False, False], [32767, True, False], [32767, True, False]]],
  ['control: scenario 3',
   [False,
    [[-10436, -32034, 'MUL', True],
     [2, 22108, 'SUB', False],
     [7659, 2, 'MUL', False],
     [5339, 1, 'MUL', False]]],
   [[7688, True, False], [-22106, False, False], [15318, False, False], [5339, False, False]]]],
 [['regression: scenario 51',
   [False,
    [[1, -32768, 'MUL', False],
     [-200, 20989, 'MUL', False],
     [-1, 1541, 'MUL', False],
     [-11038, 32767, 'ADD', False]]],
   [[-32768, False, False], [-3496, True, True], 'halted']],
  ['regression: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['regression: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 1',
   [False,
    [[15094, -24454, 'MUL', False],
     [-30000, 24363, 'MUL', True],
     [2, -14871, 'MUL', False],
     [1, -26550, 'MUL', True],
     [-1, -7090, 'SUB', False],
     [32767, 1, 'MUL', True],
     [-32768, 15967, 'ADD', False],
     [21457, -28729, 'SUB', True]]],
   [[-9924, True, True], 'halted']],
  ['control: scenario 2',
   [False,
    [[-200, -200, 'ADD', True],
     [32767, 200, 'MUL', False],
     [-9390, 1, 'MUL', False],
     [-11169, -28064, 'ADD', True],
     [22720, -1, 'ADD', False],
     [-200, -1, 'ADD', False],
     [-12859, 1, 'ADD', True]]],
   [[-400, False, False], [-200, True, True], 'halted']],
  ['control: scenario 3',
   [True,
    [[200, 30000, 'ADD', True],
     [11009, -200, 'SUB', True],
     [-200, 1, 'MUL', False],
     [-20841, 21280, 'ADD', True]]],
   [[30200, False, False], [11209, False, False], [-200, False, False], [439, False, False]]]],
 [['regression: scenario 43',
   [True,
    [[-32768, 1, 'SUB', True],
     [1, -32768, 'MUL', True],
     [-30000, -3339, 'SUB', False],
     [200, -200, 'SUB', True],
     [16985, 2, 'MUL', True],
     [15251, 24317, 'MUL', False],
     [10553, -32768, 'ADD', True]]],
   [[-32768, True, False],
    [-32768, False, False],
    [-26661, False, False],
    [400, False, False],
    [32767, True, False],
    [32767, True, True],
    'halted']],
  ['regression: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['control: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['regression: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 1',
   [False,
    [[-26759, 2, 'SUB', True],
     [-30000, -32768, 'SUB', False],
     [-3730, -31438, 'ADD', False],
     [200, 200, 'SUB', True],
     [-24761, 200, 'SUB', True],
     [-20747, 200, 'SUB', False],
     [200, 19789, 'SUB', True]]],
   [[-26761, False, False], [2768, False, False], [30368, True, True], 'halted']],
  ['control: scenario 2',
   [False,
    [[30000, -32768, 'ADD', True],
     [4451, 31437, 'SUB', False],
     [-1, 22093, 'MUL', False],
     [-30000, -18488, 'SUB', False],
     [30000, -28921, 'MUL', True],
     [-200, 23349, 'ADD', True],
     [-32768, 200, 'SUB', False]]],
   [[-2768, False, False],
    [-26986, False, False],
    [-22093, False, False],
    [-11512, False, False],
    [1104, True, False],
    [23149, False, False],
    [32568, True, True],
    'halted']],
  ['control: scenario 3',
   [False,
    [[-32768, 19187, 'MUL', True],
     [-7301, -200, 'SUB', False],
     [-32768, 30000, 'SUB', True],
     [-9747, -24186, 'ADD', True],
     [-1, -22920, 'SUB', True],
     [-259, 22293, 'SUB', False],
     [23494, -1, 'MUL', False],
     [-32529, 24353, 'SUB', True]]],
   [[-32768, True, False],
    [-7101, False, False],
    [2768, True, False],
    [31603, True, False],
    [22919, False, False],
    [-22552, False, False],
    [-23494, False, False],
    [8654, True, False]]]],
 [['regression: exact limits',
   [False, [[-32767, 1, 'SUB', False], [32766, 1, 'ADD', False], [3, 4, 'MUL', False]]],
   [[-32768, False, False], [32767, False, False], [12, False, False]]],
  ['regression: saturation both signs',
   [True,
    [[-2, -20000, 'MUL', True],
     [-20000, 20000, 'SUB', True],
     [32767, 0, 'ADD', False],
     [1, 1, 'ADD', False]]],
   [[32767, True, False], [-32768, True, False], [32767, False, False], [2, False, False]]],
  ['control: wrap with cleared traps',
   [False,
    [[30000, 5000, 'ADD', True],
     [-30000, 5000, 'SUB', True],
     [200, 200, 'MUL', False],
     [1, 1, 'ADD', False]]],
   [[-30536, True, False], [30536, True, False], [-25536, True, True], 'halted']],
  ['control: overflow then clean ops',
   [True, [[32767, 1, 'ADD', True], [5, 5, 'ADD', False], [7, 7, 'SUB', False]]],
   [[32767, True, False], [10, False, False], [0, False, False]]],
  ['control: scenario 1',
   [True,
    [[160, 2, 'SUB', True], [200, 200, 'SUB', True], [222, 200, 'SUB', False], [200, -32768, 'ADD', True]]],
   [[158, False, False], [0, False, False], [22, False, False], [-32568, False, False]]],
  ['control: scenario 2',
   [True,
    [[30000, 1574, 'SUB', True],
     [-783, -29319, 'SUB', True],
     [2, 1803, 'SUB', False],
     [200, 2, 'ADD', True],
     [9942, 32767, 'MUL', True],
     [-30000, -19084, 'MUL', True],
     [-10106, -30460, 'ADD', False],
     [2, 32767, 'ADD', True]]],
   [[28426, False, False],
    [28536, False, False],
    [-1801, False, False],
    [202, False, False],
    [32767, True, False],
    [32767, True, False],
    [-32768, True, True],
    'halted']],
  ['control: scenario 3',
   [False,
    [[4437, 15506, 'ADD', True],
     [32767, 1, 'SUB', False],
     [-7256, 11958, 'ADD', True],
     [1, 11416, 'MUL', True]]],
   [[19943, False, False], [32766, False, False], [4702, False, False], [11416, False, False]]],
  ['control: scenario 4',
   [False,
    [[19188, -11432, 'MUL', True],
     [-32768, 7005, 'SUB', True],
     [-200, 1, 'ADD', True],
     [32767, -4687, 'MUL', False],
     [7857, 5828, 'ADD', True],
     [-11826, -1129, 'MUL', False],
     [-1, 1, 'MUL', False],
     [-24957, -5100, 'ADD', True]]],
   [[-8224, True, False], [25763, True, False], [-199, False, False], [-28081, True, True], 'halted']]]]
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 legitimate most-negative result sets the overflow bit and faults the processor.

16-bit INT math rung followed by a later fault-clearing rung, one op per scan; ops is a list of [a, b, ADD|SUB|MUL, clear]. A result outside -32768..32767 sets the overflow bit V for that instruction and the overflow trap; the stored result is saturated to 32767/-32768 when clamp is set or wrapped as 16-bit two's complement otherwise. V is recomputed per instruction. The later rung clears the trap when clear is true; a trap still set at end of scan halts the processor. Return [result, V, trap] per scan, then "halted" and stop if faulted.

split: open-access