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 a > 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: 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]]],
  ['regression: scenario 8',
   [True,
    [[-30000, -256, 'MUL', True],
     [-1, 17551, 'ADD', True],
     [-200, 22315, 'MUL', False],
     [-30000, 30000, 'ADD', True]]],
   [[32767, True, False], [17550, False, False], [-32768, True, True], 'halted']],
  ['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: 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,
    [[-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']]],
 [['regression: 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]]],
  ['regression: scenario 5',
   [True,
    [[32767, -13835, 'MUL', True],
     [2, -30000, 'SUB', False],
     [9813, -200, 'SUB', False],
     [-9454, -19431, 'ADD', True],
     [26200, 2, 'MUL', False],
     [2, -30000, 'SUB', True],
     [-13629, -30000, 'MUL', True]]],
   [[-32768, True, False],
    [30002, False, False],
    [10013, False, False],
    [-28885, False, False],
    [32767, True, True],
    'halted']],
  ['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: 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,
    [[-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 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 9',
   [True,
    [[-10100, 19610, 'ADD', True],
     [-32768, -106, 'MUL', False],
     [-15096, 200, 'SUB', False],
     [3930, 7872, 'ADD', False],
     [1, -15393, 'MUL', True],
     [-27515, -200, 'MUL', True],
     [32767, 9291, 'SUB', True]]],
   [[9510, False, False], [32767, True, True], 'halted']],
  ['regression: scenario 13',
   [True,
    [[200, -10922, 'SUB', True],
     [-1, -20952, 'SUB', True],
     [-200, -3607, 'ADD', True],
     [-32768, -19464, 'ADD', False],
     [1, -200, 'ADD', False],
     [30000, 1, 'SUB', True],
     [32767, 32767, 'MUL', True],
     [19302, 22544, 'SUB', True]]],
   [[11122, False, False], [20951, False, False], [-3807, False, False], [-32768, True, True], 'halted']],
  ['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: 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']]],
 [['regression: scenario 31',
   [True,
    [[-2900, -200, 'MUL', True],
     [23750, 200, 'SUB', False],
     [200, -31152, 'MUL', True],
     [30000, -32768, 'SUB', True]]],
   [[32767, True, False], [23550, False, False], [-32768, True, False], [32767, True, False]]],
  ['regression: scenario 9',
   [True,
    [[10861, -200, 'SUB', False],
     [2, 6241, 'ADD', True],
     [30000, 1, 'ADD', True],
     [-20637, -27040, 'ADD', False],
     [14992, -200, 'ADD', True],
     [30305, -32768, 'MUL', False]]],
   [[11061, False, False], [6243, False, False], [30001, False, False], [-32768, True, True], 'halted']],
  ['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: 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,
    [[-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']]],
 [['regression: scenario 14',
   [True,
    [[200, 2, 'SUB', True],
     [30382, 5446, 'ADD', True],
     [-200, 30000, 'SUB', False],
     [-19301, -200, 'MUL', True],
     [1112, 32767, 'ADD', True],
     [1, 21509, 'SUB', False],
     [-32768, 28969, 'ADD', True]]],
   [[198, False, False],
    [32767, True, False],
    [-30200, False, False],
    [32767, True, False],
    [32767, True, False],
    [-21508, False, False],
    [-3799, False, False]]],
  ['regression: scenario 15',
   [True,
    [[30398, 26515, 'SUB', True],
     [-32768, -3816, 'ADD', False],
     [23577, -13673, 'SUB', False],
     [-22301, -32768, 'SUB', True],
     [30000, -200, 'MUL', True],
     [-28303, 30000, 'SUB', True],
     [-29315, -30000, 'SUB', True]]],
   [[3883, False, False], [-32768, True, True], 'halted']],
  ['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: 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]]],
  ['regression: 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)

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 -32767
            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: 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]]],
  ['regression: scenario 8',
   [True,
    [[-30000, -256, 'MUL', True],
     [-1, 17551, 'ADD', True],
     [-200, 22315, 'MUL', False],
     [-30000, 30000, 'ADD', True]]],
   [[32767, True, False], [17550, False, False], [-32768, True, True], 'halted']],
  ['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: 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,
    [[-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']]],
 [['regression: 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]]],
  ['regression: scenario 5',
   [True,
    [[32767, -13835, 'MUL', True],
     [2, -30000, 'SUB', False],
     [9813, -200, 'SUB', False],
     [-9454, -19431, 'ADD', True],
     [26200, 2, 'MUL', False],
     [2, -30000, 'SUB', True],
     [-13629, -30000, 'MUL', True]]],
   [[-32768, True, False],
    [30002, False, False],
    [10013, False, False],
    [-28885, False, False],
    [32767, True, True],
    'halted']],
  ['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: 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,
    [[-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 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 9',
   [True,
    [[-10100, 19610, 'ADD', True],
     [-32768, -106, 'MUL', False],
     [-15096, 200, 'SUB', False],
     [3930, 7872, 'ADD', False],
     [1, -15393, 'MUL', True],
     [-27515, -200, 'MUL', True],
     [32767, 9291, 'SUB', True]]],
   [[9510, False, False], [32767, True, True], 'halted']],
  ['regression: scenario 13',
   [True,
    [[200, -10922, 'SUB', True],
     [-1, -20952, 'SUB', True],
     [-200, -3607, 'ADD', True],
     [-32768, -19464, 'ADD', False],
     [1, -200, 'ADD', False],
     [30000, 1, 'SUB', True],
     [32767, 32767, 'MUL', True],
     [19302, 22544, 'SUB', True]]],
   [[11122, False, False], [20951, False, False], [-3807, False, False], [-32768, True, True], 'halted']],
  ['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: 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']]],
 [['regression: scenario 31',
   [True,
    [[-2900, -200, 'MUL', True],
     [23750, 200, 'SUB', False],
     [200, -31152, 'MUL', True],
     [30000, -32768, 'SUB', True]]],
   [[32767, True, False], [23550, False, False], [-32768, True, False], [32767, True, False]]],
  ['regression: scenario 9',
   [True,
    [[10861, -200, 'SUB', False],
     [2, 6241, 'ADD', True],
     [30000, 1, 'ADD', True],
     [-20637, -27040, 'ADD', False],
     [14992, -200, 'ADD', True],
     [30305, -32768, 'MUL', False]]],
   [[11061, False, False], [6243, False, False], [30001, False, False], [-32768, True, True], 'halted']],
  ['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: 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,
    [[-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']]],
 [['regression: scenario 14',
   [True,
    [[200, 2, 'SUB', True],
     [30382, 5446, 'ADD', True],
     [-200, 30000, 'SUB', False],
     [-19301, -200, 'MUL', True],
     [1112, 32767, 'ADD', True],
     [1, 21509, 'SUB', False],
     [-32768, 28969, 'ADD', True]]],
   [[198, False, False],
    [32767, True, False],
    [-30200, False, False],
    [32767, True, False],
    [32767, True, False],
    [-21508, False, False],
    [-3799, False, False]]],
  ['regression: scenario 15',
   [True,
    [[30398, 26515, 'SUB', True],
     [-32768, -3816, 'ADD', False],
     [23577, -13673, 'SUB', False],
     [-22301, -32768, 'SUB', True],
     [30000, -200, 'MUL', True],
     [-28303, 30000, 'SUB', True],
     [-29315, -30000, 'SUB', True]]],
   [[3883, False, False], [-32768, True, True], 'halted']],
  ['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: 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]]],
  ['regression: 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:
Multiplying two negative numbers saturates to -32768 instead of 32767.

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 clear:
            trap = False
        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
        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: 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]]],
  ['regression: 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,
    [[-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']],
  ['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: 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]]],
  ['regression: 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: 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: 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: 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: 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]]],
  ['regression: 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 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: 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 6',
   [True,
    [[-22967, 31354, 'ADD', True],
     [30000, 2, 'MUL', True],
     [-32768, -1, 'MUL', False],
     [26774, -19891, 'SUB', False],
     [1, -15244, 'ADD', False]]],
   [[8387, False, False], [32767, True, False], [32767, 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']],
  ['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: 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: 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 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 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']],
  ['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: 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: 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 4',
   [False,
    [[18038, 28061, 'MUL', False],
     [-200, -3461, 'SUB', True],
     [-32768, -11546, 'SUB', False],
     [-28255, 22972, 'ADD', False]]],
   [[29790, True, True], 'halted']]],
 [['regression: 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']],
  ['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']],
  ['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: 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: 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]]],
  ['regression: 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 + 32768) % 65536 - 32768
        if clear and not v:
            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: 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]]],
  ['regression: 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,
    [[-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']],
  ['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: 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]]],
  ['regression: 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: 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: 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: 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: 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]]],
  ['regression: 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 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: 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 6',
   [True,
    [[-22967, 31354, 'ADD', True],
     [30000, 2, 'MUL', True],
     [-32768, -1, 'MUL', False],
     [26774, -19891, 'SUB', False],
     [1, -15244, 'ADD', False]]],
   [[8387, False, False], [32767, True, False], [32767, 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']],
  ['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: 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: 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 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 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']],
  ['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: 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: 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 4',
   [False,
    [[18038, 28061, 'MUL', False],
     [-200, -3461, 'SUB', True],
     [-32768, -11546, 'SUB', False],
     [-28255, 22972, 'ADD', False]]],
   [[29790, True, True], 'halted']]],
 [['regression: 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']],
  ['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']],
  ['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: 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: 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]]],
  ['regression: 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:
An overflow in the same scan as the clearing rung still halts 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


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

N = 1
observations = []
def solve(clamp, ops):
    trap = False
    last_v = 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 = last_v or not (-32768 <= r <= 32767)
        last_v = v
        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: 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]]],
  ['regression: 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: 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: 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: 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: 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 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]]],
  ['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: 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: 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]]],
  ['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 8',
   [False,
    [[-4887, -27663, 'SUB', True],
     [30000, 6920, 'MUL', True],
     [28114, 1, 'SUB', False],
     [1, -5974, 'SUB', False]]],
   [[22776, False, False], [-18048, True, False], [28113, False, False], [5975, False, False]]],
  ['regression: scenario 12',
   [True,
    [[-200, -14943, 'ADD', True],
     [2, 30000, 'SUB', True],
     [2, -32768, 'SUB', True],
     [24257, -25508, 'SUB', True],
     [932, -29592, 'SUB', False],
     [-32768, -15582, 'ADD', False],
     [25886, -200, 'SUB', False],
     [15203, -12133, 'SUB', False]]],
   [[-15143, False, False],
    [-29998, False, False],
    [32767, True, False],
    [32767, True, False],
    [30524, False, False],
    [-32768, True, True],
    'halted']],
  ['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: 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: 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']]],
 [['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: scenario 31',
   [True,
    [[-2900, -200, 'MUL', True],
     [23750, 200, 'SUB', False],
     [200, -31152, 'MUL', True],
     [30000, -32768, 'SUB', True]]],
   [[32767, True, False], [23550, False, False], [-32768, True, False], [32767, True, 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: 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: 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']],
  ['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 13',
   [False,
    [[-30088, 16833, 'SUB', True],
     [1555, 200, 'ADD', False],
     [200, -31213, 'SUB', True],
     [-10659, -15907, 'MUL', False],
     [2, -10563, 'ADD', True],
     [-9064, 16082, 'ADD', True],
     [30646, -1, 'ADD', True],
     [10428, 200, 'ADD', True]]],
   [[18615, True, False], [1755, False, False], [31413, False, False], [11081, True, True], 'halted']],
  ['regression: scenario 18',
   [True,
    [[-24623, 2, 'MUL', True],
     [-200, -32768, 'MUL', True],
     [1, -1, 'SUB', False],
     [-22404, -5569, 'SUB', True],
     [30526, 30000, 'MUL', True]]],
   [[-32768, True, False],
    [32767, True, False],
    [2, False, False],
    [-16835, False, False],
    [32767, True, 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: 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: 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)

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

N = 1
observations = []
def solve(clamp, ops):
    trap = False
    last_v = 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 = (last_v and not clear) or not (-32768 <= r <= 32767)
        last_v = v
        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: 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]]],
  ['regression: 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: 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: 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: 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: 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 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]]],
  ['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: 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: 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]]],
  ['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 8',
   [False,
    [[-4887, -27663, 'SUB', True],
     [30000, 6920, 'MUL', True],
     [28114, 1, 'SUB', False],
     [1, -5974, 'SUB', False]]],
   [[22776, False, False], [-18048, True, False], [28113, False, False], [5975, False, False]]],
  ['regression: scenario 12',
   [True,
    [[-200, -14943, 'ADD', True],
     [2, 30000, 'SUB', True],
     [2, -32768, 'SUB', True],
     [24257, -25508, 'SUB', True],
     [932, -29592, 'SUB', False],
     [-32768, -15582, 'ADD', False],
     [25886, -200, 'SUB', False],
     [15203, -12133, 'SUB', False]]],
   [[-15143, False, False],
    [-29998, False, False],
    [32767, True, False],
    [32767, True, False],
    [30524, False, False],
    [-32768, True, True],
    'halted']],
  ['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: 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: 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']]],
 [['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: scenario 31',
   [True,
    [[-2900, -200, 'MUL', True],
     [23750, 200, 'SUB', False],
     [200, -31152, 'MUL', True],
     [30000, -32768, 'SUB', True]]],
   [[32767, True, False], [23550, False, False], [-32768, True, False], [32767, True, 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: 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: 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']],
  ['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 13',
   [False,
    [[-30088, 16833, 'SUB', True],
     [1555, 200, 'ADD', False],
     [200, -31213, 'SUB', True],
     [-10659, -15907, 'MUL', False],
     [2, -10563, 'ADD', True],
     [-9064, 16082, 'ADD', True],
     [30646, -1, 'ADD', True],
     [10428, 200, 'ADD', True]]],
   [[18615, True, False], [1755, False, False], [31413, False, False], [11081, True, True], 'halted']],
  ['regression: scenario 18',
   [True,
    [[-24623, 2, 'MUL', True],
     [-200, -32768, 'MUL', True],
     [1, -1, 'SUB', False],
     [-22404, -5569, 'SUB', True],
     [30526, 30000, 'MUL', True]]],
   [[-32768, True, False],
    [32767, True, False],
    [2, False, False],
    [-16835, False, False],
    [32767, True, 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: 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: 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:
After one handled overflow every later math result is flagged, saturated and trapped.

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(heat_sp, cool_sp, temps):
    diff = 10
    if cool_sp - heat_sp < 30:
        heat_sp = cool_sp - 30
    state = 'idle'
    last_heat_end = None
    last_cool_end = None
    out = []
    for minute, temp in enumerate(temps):
        if state == 'heat' and temp >= heat_sp:
            state = 'idle'
            last_heat_end = minute
        elif state == 'cool' and temp <= cool_sp:
            state = 'idle'
            last_cool_end = minute
        if state == 'idle':
            if temp < heat_sp - diff and (last_cool_end is None or minute - last_cool_end >= 5):
                state = 'heat'
            elif temp > cool_sp + diff and (last_heat_end is None or minute - last_heat_end >= 5):
                state = 'cool'
        out.append(state)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: heat then locked cool',
   [680, 700, [650, 660, 690, 700, 712, 725, 725, 725, 715, 710]],
   ['heat', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'idle']],
  ['regression: deadband squeeze',
   [700, 705, [740, 741, 735, 730, 729]],
   ['idle', 'cool', 'cool', 'idle', 'idle']],
  ['control: exact thresholds',
   [680, 720, [670, 669, 680, 731, 730, 720, 721]],
   ['idle', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle']],
  ['control: short heat cycle then hot',
   [680, 760, [660, 685, 790, 790, 790, 790, 790, 790]],
   ['heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool']],
  ['regression: scenario 1',
   [680, 690, [693, 687, 709, 709, 685, 704, 684, 680, 686, 687, 679]],
   ['idle', 'idle', 'idle', 'idle', 'idle', 'idle', 'idle', 'idle', 'idle', 'idle', 'idle']],
  ['regression: scenario 2',
   [680, 700, [681, 686, 698, 703, 724, 744, 752, 732, 755, 742, 762, 772]],
   ['idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool']],
  ['control: scenario 3',
   [680,
    740,
    [649,
     667,
     643,
     622,
     637,
     657,
     634,
     649,
     674,
     654,
     672,
     675,
     698,
     687,
     666,
     654,
     670,
     645,
     654,
     637,
     651,
     672]],
   ['heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'idle',
    'idle',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat']],
  ['control: scenario 4',
   [680, 720, [678, 693, 708, 706, 691, 698, 702, 725, 720, 725, 717, 703, 717, 731, 714, 728, 740, 735]],
   ['idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'cool',
    'idle',
    'idle',
    'cool',
    'cool']]],
 [['regression: deadband squeeze',
   [700, 705, [740, 741, 735, 730, 729]],
   ['idle', 'cool', 'cool', 'idle', 'idle']],
  ['regression: scenario 1',
   [680, 700, [687, 695, 710, 721, 712, 724, 736, 738, 737, 740, 748]],
   ['idle', 'idle', 'idle', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool']],
  ['regression: heat then locked cool',
   [680, 700, [650, 660, 690, 700, 712, 725, 725, 725, 715, 710]],
   ['heat', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'idle']],
  ['control: exact thresholds',
   [680, 720, [670, 669, 680, 731, 730, 720, 721]],
   ['idle', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle']],
  ['control: short heat cycle then hot',
   [680, 760, [660, 685, 790, 790, 790, 790, 790, 790]],
   ['heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool']],
  ['control: scenario 2',
   [680, 710, [674, 681, 688, 699, 718, 698, 716, 709, 691, 672, 678, 701, 724, 699, 683, 698, 691, 701]],
   ['idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'cool',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle']],
  ['regression: scenario 3',
   [680, 690, [686, 679, 704, 696, 678, 658, 635, 648, 643, 657, 650, 661, 672, 691, 687, 685, 708, 687]],
   ['idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle']],
  ['control: scenario 4',
   [680, 710, [747, 771, 775, 769, 770, 766, 790, 790, 790, 789, 781]],
   ['cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool']]],
 [['regression: scenario 6',
   [680, 700, [757, 740, 724, 721, 702, 699, 724, 717, 742, 721, 723, 700]],
   ['cool', 'cool', 'cool', 'cool', 'idle', 'idle', 'cool', 'cool', 'cool', 'cool', 'cool', 'idle']],
  ['regression: scenario 8',
   [680, 690, [726, 722, 736, 726, 744, 763, 778, 789, 774, 760, 781, 786, 790, 783, 770, 770, 750]],
   ['cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool']],
  ['regression: heat then locked cool',
   [680, 700, [650, 660, 690, 700, 712, 725, 725, 725, 715, 710]],
   ['heat', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'idle']],
  ['control: exact thresholds',
   [680, 720, [670, 669, 680, 731, 730, 720, 721]],
   ['idle', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle']],
  ['control: short heat cycle then hot',
   [680, 760, [660, 685, 790, 790, 790, 790, 790, 790]],
   ['heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool']],
  ['regression: deadband squeeze',
   [700, 705, [740, 741, 735, 730, 729]],
   ['idle', 'cool', 'cool', 'idle', 'idle']],
  ['control: scenario 1',
   [680, 710, [758, 778, 769, 788, 780, 787, 790, 773, 789, 766, 785, 779, 790, 790, 790, 777, 790]],
   ['cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool']],
  ['control: scenario 2',
   [680, 740, [646, 654, 672, 672, 692, 717, 736, 759, 769, 790, 790]],
   ['heat', 'heat', 'heat', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool']]],
 [['regression: scenario 3',
   [680, 690, [720, 710, 717, 702, 708, 715, 732, 753, 772, 753, 757]],
   ['idle', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'cool', 'cool', 'cool']],
  ['regression: scenario 13',
   [680, 700, [732, 729, 742, 749, 733, 737, 714, 714, 716, 713, 725, 733, 737, 726, 729, 723, 745]],
   ['cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool']],
  ['regression: heat then locked cool',
   [680, 700, [650, 660, 690, 700, 712, 725, 725, 725, 715, 710]],
   ['heat', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'idle']],
  ['control: exact thresholds',
   [680, 720, [670, 669, 680, 731, 730, 720, 721]],
   ['idle', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle']],
  ['control: short heat cycle then hot',
   [680, 760, [660, 685, 790, 790, 790, 790, 790, 790]],
   ['heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool']],
  ['regression: deadband squeeze',
   [700, 705, [740, 741, 735, 730, 729]],
   ['idle', 'cool', 'cool', 'idle', 'idle']],
  ['regression: scenario 1',
   [680,
    690,
    [671, 646, 636, 635, 620, 625, 635, 653, 659, 636, 645, 633, 620, 628, 620, 638, 653, 655, 666, 678]],
   ['idle',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat']],
  ['control: scenario 2',
   [680, 740, [655, 638, 663, 641, 622, 636, 628, 621, 621, 620, 635, 632, 644, 651, 656, 675, 687]],
   ['heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'idle']]],
 [['regression: scenario 8',
   [680, 690, [656, 635, 650, 663, 673, 651, 644, 644, 623, 630, 643, 655]],
   ['heat', 'heat', 'heat', 'heat', 'heat', 'heat', 'heat', 'heat', 'heat', 'heat', 'heat', 'heat']],
  ['regression: scenario 22',
   [680, 690, [707, 687, 693, 697, 709, 698, 723, 702, 723, 731, 749, 743, 742, 754, 733, 715, 708]],
   ['idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'cool',
    'idle',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'idle']],
  ['regression: heat then locked cool',
   [680, 700, [650, 660, 690, 700, 712, 725, 725, 725, 715, 710]],
   ['heat', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'idle']],
  ['control: exact thresholds',
   [680, 720, [670, 669, 680, 731, 730, 720, 721]],
   ['idle', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle']],
  ['control: short heat cycle then hot',
   [680, 760, [660, 685, 790, 790, 790, 790, 790, 790]],
   ['heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool']],
  ['regression: deadband squeeze',
   [700, 705, [740, 741, 735, 730, 729]],
   ['idle', 'cool', 'cool', 'idle', 'idle']],
  ['control: scenario 1',
   [680, 720, [774, 779, 785, 790, 790, 781, 778, 777, 770, 767, 743, 730, 708, 690]],
   ['cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'idle',
    'idle']],
  ['control: scenario 2',
   [680,
    710,
    [701,
     679,
     702,
     721,
     727,
     748,
     743,
     720,
     741,
     738,
     762,
     769,
     758,
     740,
     744,
     725,
     748,
     728,
     742,
     755,
     769]],
   ['idle',
    'idle',
    'idle',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool']]]]
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(heat_sp, cool_sp, temps):
    diff = 10
    if cool_sp - heat_sp < 30:
        cool_sp = cool_sp + 30
    state = 'idle'
    last_heat_end = None
    last_cool_end = None
    out = []
    for minute, temp in enumerate(temps):
        if state == 'heat' and temp >= heat_sp:
            state = 'idle'
            last_heat_end = minute
        elif state == 'cool' and temp <= cool_sp:
            state = 'idle'
            last_cool_end = minute
        if state == 'idle':
            if temp < heat_sp - diff and (last_cool_end is None or minute - last_cool_end >= 5):
                state = 'heat'
            elif temp > cool_sp + diff and (last_heat_end is None or minute - last_heat_end >= 5):
                state = 'cool'
        out.append(state)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: heat then locked cool',
   [680, 700, [650, 660, 690, 700, 712, 725, 725, 725, 715, 710]],
   ['heat', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'idle']],
  ['regression: deadband squeeze',
   [700, 705, [740, 741, 735, 730, 729]],
   ['idle', 'cool', 'cool', 'idle', 'idle']],
  ['control: exact thresholds',
   [680, 720, [670, 669, 680, 731, 730, 720, 721]],
   ['idle', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle']],
  ['control: short heat cycle then hot',
   [680, 760, [660, 685, 790, 790, 790, 790, 790, 790]],
   ['heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool']],
  ['regression: scenario 1',
   [680, 690, [693, 687, 709, 709, 685, 704, 684, 680, 686, 687, 679]],
   ['idle', 'idle', 'idle', 'idle', 'idle', 'idle', 'idle', 'idle', 'idle', 'idle', 'idle']],
  ['regression: scenario 2',
   [680, 700, [681, 686, 698, 703, 724, 744, 752, 732, 755, 742, 762, 772]],
   ['idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool']],
  ['control: scenario 3',
   [680,
    740,
    [649,
     667,
     643,
     622,
     637,
     657,
     634,
     649,
     674,
     654,
     672,
     675,
     698,
     687,
     666,
     654,
     670,
     645,
     654,
     637,
     651,
     672]],
   ['heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'idle',
    'idle',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat']],
  ['control: scenario 4',
   [680, 720, [678, 693, 708, 706, 691, 698, 702, 725, 720, 725, 717, 703, 717, 731, 714, 728, 740, 735]],
   ['idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'cool',
    'idle',
    'idle',
    'cool',
    'cool']]],
 [['regression: deadband squeeze',
   [700, 705, [740, 741, 735, 730, 729]],
   ['idle', 'cool', 'cool', 'idle', 'idle']],
  ['regression: scenario 1',
   [680, 700, [687, 695, 710, 721, 712, 724, 736, 738, 737, 740, 748]],
   ['idle', 'idle', 'idle', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool']],
  ['regression: heat then locked cool',
   [680, 700, [650, 660, 690, 700, 712, 725, 725, 725, 715, 710]],
   ['heat', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'idle']],
  ['control: exact thresholds',
   [680, 720, [670, 669, 680, 731, 730, 720, 721]],
   ['idle', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle']],
  ['control: short heat cycle then hot',
   [680, 760, [660, 685, 790, 790, 790, 790, 790, 790]],
   ['heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool']],
  ['control: scenario 2',
   [680, 710, [674, 681, 688, 699, 718, 698, 716, 709, 691, 672, 678, 701, 724, 699, 683, 698, 691, 701]],
   ['idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'cool',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle']],
  ['regression: scenario 3',
   [680, 690, [686, 679, 704, 696, 678, 658, 635, 648, 643, 657, 650, 661, 672, 691, 687, 685, 708, 687]],
   ['idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle']],
  ['control: scenario 4',
   [680, 710, [747, 771, 775, 769, 770, 766, 790, 790, 790, 789, 781]],
   ['cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool', 'cool']]],
 [['regression: scenario 6',
   [680, 700, [757, 740, 724, 721, 702, 699, 724, 717, 742, 721, 723, 700]],
   ['cool', 'cool', 'cool', 'cool', 'idle', 'idle', 'cool', 'cool', 'cool', 'cool', 'cool', 'idle']],
  ['regression: scenario 8',
   [680, 690, [726, 722, 736, 726, 744, 763, 778, 789, 774, 760, 781, 786, 790, 783, 770, 770, 750]],
   ['cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool']],
  ['regression: heat then locked cool',
   [680, 700, [650, 660, 690, 700, 712, 725, 725, 725, 715, 710]],
   ['heat', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'idle']],
  ['control: exact thresholds',
   [680, 720, [670, 669, 680, 731, 730, 720, 721]],
   ['idle', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle']],
  ['control: short heat cycle then hot',
   [680, 760, [660, 685, 790, 790, 790, 790, 790, 790]],
   ['heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool']],
  ['regression: deadband squeeze',
   [700, 705, [740, 741, 735, 730, 729]],
   ['idle', 'cool', 'cool', 'idle', 'idle']],
  ['control: scenario 1',
   [680, 710, [758, 778, 769, 788, 780, 787, 790, 773, 789, 766, 785, 779, 790, 790, 790, 777, 790]],
   ['cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool']],
  ['control: scenario 2',
   [680, 740, [646, 654, 672, 672, 692, 717, 736, 759, 769, 790, 790]],
   ['heat', 'heat', 'heat', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool']]],
 [['regression: scenario 3',
   [680, 690, [720, 710, 717, 702, 708, 715, 732, 753, 772, 753, 757]],
   ['idle', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'cool', 'cool', 'cool']],
  ['regression: scenario 13',
   [680, 700, [732, 729, 742, 749, 733, 737, 714, 714, 716, 713, 725, 733, 737, 726, 729, 723, 745]],
   ['cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool']],
  ['regression: heat then locked cool',
   [680, 700, [650, 660, 690, 700, 712, 725, 725, 725, 715, 710]],
   ['heat', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'idle']],
  ['control: exact thresholds',
   [680, 720, [670, 669, 680, 731, 730, 720, 721]],
   ['idle', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle']],
  ['control: short heat cycle then hot',
   [680, 760, [660, 685, 790, 790, 790, 790, 790, 790]],
   ['heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool']],
  ['regression: deadband squeeze',
   [700, 705, [740, 741, 735, 730, 729]],
   ['idle', 'cool', 'cool', 'idle', 'idle']],
  ['regression: scenario 1',
   [680,
    690,
    [671, 646, 636, 635, 620, 625, 635, 653, 659, 636, 645, 633, 620, 628, 620, 638, 653, 655, 666, 678]],
   ['idle',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat']],
  ['control: scenario 2',
   [680, 740, [655, 638, 663, 641, 622, 636, 628, 621, 621, 620, 635, 632, 644, 651, 656, 675, 687]],
   ['heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'heat',
    'idle']]],
 [['regression: scenario 8',
   [680, 690, [656, 635, 650, 663, 673, 651, 644, 644, 623, 630, 643, 655]],
   ['heat', 'heat', 'heat', 'heat', 'heat', 'heat', 'heat', 'heat', 'heat', 'heat', 'heat', 'heat']],
  ['regression: scenario 22',
   [680, 690, [707, 687, 693, 697, 709, 698, 723, 702, 723, 731, 749, 743, 742, 754, 733, 715, 708]],
   ['idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'idle',
    'cool',
    'idle',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'idle']],
  ['regression: heat then locked cool',
   [680, 700, [650, 660, 690, 700, 712, 725, 725, 725, 715, 710]],
   ['heat', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool', 'idle']],
  ['control: exact thresholds',
   [680, 720, [670, 669, 680, 731, 730, 720, 721]],
   ['idle', 'heat', 'idle', 'idle', 'idle', 'idle', 'idle']],
  ['control: short heat cycle then hot',
   [680, 760, [660, 685, 790, 790, 790, 790, 790, 790]],
   ['heat', 'idle', 'idle', 'idle', 'idle', 'idle', 'cool', 'cool']],
  ['regression: deadband squeeze',
   [700, 705, [740, 741, 735, 730, 729]],
   ['idle', 'cool', 'cool', 'idle', 'idle']],
  ['control: scenario 1',
   [680, 720, [774, 779, 785, 790, 790, 781, 778, 777, 770, 767, 743, 730, 708, 690]],
   ['cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'idle',
    'idle']],
  ['control: scenario 2',
   [680,
    710,
    [701,
     679,
     702,
     721,
     727,
     748,
     743,
     720,
     741,
     738,
     762,
     769,
     758,
     740,
     744,
     725,
     748,
     728,
     742,
     755,
     769]],
   ['idle',
    'idle',
    'idle',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool',
    'cool']]]]
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:
Raising cooling close to heating silently drops the heating setpoint the user just chose.

Auto heat/cool thermostat sampled once per minute (temps in tenths F). If the cooling setpoint is less than 3.0 F above the heating setpoint it is pushed up to heat_sp + 30 (the heating setpoint wins). Heating starts below heat_sp - 1.0 F and ends at temp >= heat_sp; cooling starts above cool_sp + 1.0 F and ends at temp <= cool_sp. After a heating call ends, cooling may not start until 5 minutes later, and vice versa. Return heat, cool or idle per minute.

split: open-access