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

N = 1
observations = []
def solve(preset, start, scans):
    acc = start
    ov = False
    un = False
    pcu = False
    pcd = False
    out = []
    for cu, cd, res in scans:
        cu = bool(cu)
        cd = bool(cd)
        if cu and not pcu:
            acc += 1
            if acc > 32767:
                acc = -32768
                ov = True
        if cd and not pcd:
            acc -= 1
            if acc < -32768:
                acc = 32767
                un = True
        pcu = cu
        pcd = cd
        if res:
            acc = 0
        out.append([acc, acc >= preset, ov, un])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['regression: scenario 49',
   [3,
    -32768,
    [[False, True, False],
     [True, False, False],
     [False, True, False],
     [False, True, False],
     [False, False, False],
     [False, False, False],
     [False, False, True],
     [False, False, False],
     [False, False, False],
     [True, True, True],
     [True, False, False],
     [False, True, True],
     [False, True, False],
     [True, True, False]]],
   [[32767, True, False, True],
    [-32768, False, True, True],
    [32767, True, True, True],
    [32767, True, True, True],
    [32767, True, True, True],
    [32767, True, True, True],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['control: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [0,
    1,
    [[True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, True],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [False, False, False]]],
   [[2, True, False, False],
    [2, True, False, False],
    [2, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False]]],
  ['control: scenario 2',
   [3,
    32766,
    [[True, True, False],
     [False, False, False],
     [True, True, False],
     [False, False, True],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, True, False],
     [False, False, False],
     [True, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, False]]],
   [[32766, True, False, False],
    [32766, True, False, False],
    [32766, True, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [0, False, False, False],
    [0, False, False, False]]]],
 [['regression: scenario 4',
   [3,
    32767,
    [[False, False, False],
     [True, True, True],
     [False, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [False, False, False],
     [False, True, False],
     [False, True, False]]],
   [[32767, True, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [-1, False, False, False],
    [-1, False, False, False],
    [-2, False, False, False],
    [-2, False, False, False]]],
  ['regression: scenario 54',
   [32767,
    32767,
    [[True, False, False],
     [False, False, False],
     [False, True, False],
     [True, True, False],
     [False, True, False],
     [False, False, False],
     [True, False, True],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False],
     [False, True, False],
     [True, True, False],
     [False, True, False]]],
   [[-32768, False, True, False],
    [-32768, False, True, False],
    [32767, True, True, True],
    [-32768, False, True, True],
    [-32768, False, True, True],
    [-32768, False, True, True],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [2, False, False, False],
    [1, False, False, False],
    [2, False, False, False],
    [2, False, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['control: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [2,
    -32767,
    [[True, False, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [False, True, False],
     [False, False, False],
     [True, False, False],
     [True, False, False]]],
   [[-32766, False, False, False],
    [-32766, False, False, False],
    [-32766, False, False, False],
    [-32766, False, False, False],
    [-32767, False, False, False],
    [-32767, False, False, False],
    [-32766, False, False, False],
    [-32766, False, False, False]]]],
 [['regression: scenario 11',
   [-1,
    32767,
    [[False, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [False, True, True],
     [False, True, False],
     [False, True, False],
     [False, True, False],
     [True, True, False],
     [False, False, False]]],
   [[32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [32767, True, True, True],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False]]],
  ['regression: scenario 14',
   [-1,
    -32768,
    [[True, True, False],
     [True, False, False],
     [True, True, False],
     [True, True, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [True, True, True],
     [True, True, False],
     [False, False, False],
     [False, False, False],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [32767, True, False, True],
    [32767, True, False, True],
    [32767, True, False, True],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['control: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [3,
    32765,
    [[True, False, True],
     [True, False, False],
     [True, False, True],
     [False, True, False],
     [True, False, False],
     [True, True, True],
     [False, True, False],
     [False, False, False],
     [True, True, False],
     [False, True, False],
     [True, False, False],
     [False, False, False],
     [True, False, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [-1, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [2, False, False, False],
    [1, False, False, False]]]],
 [['regression: scenario 24',
   [-32768,
    -32767,
    [[True, False, False],
     [False, True, False],
     [True, False, False],
     [True, True, False],
     [True, False, False],
     [False, True, False],
     [False, False, False],
     [True, True, False],
     [True, True, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [False, True, True],
     [False, True, False]]],
   [[-32766, True, False, False],
    [-32767, True, False, False],
    [-32766, True, False, False],
    [-32767, True, False, False],
    [-32767, True, False, False],
    [-32768, True, False, False],
    [-32768, True, False, False],
    [-32768, True, False, False],
    [-32768, True, False, False],
    [-32768, True, False, False],
    [-32768, True, False, False],
    [-32768, True, False, False],
    [0, True, False, False],
    [0, True, False, False]]],
  ['regression: scenario 32',
   [-32768,
    -32768,
    [[False, True, False],
     [False, True, False],
     [True, False, True],
     [True, False, False],
     [True, False, False],
     [False, False, True],
     [False, False, False],
     [False, True, False],
     [False, True, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[32767, True, False, True],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [-1, True, False, False],
    [-1, True, False, False],
    [0, True, False, False],
    [-1, True, False, False],
    [-1, True, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['control: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [32767,
    32766,
    [[False, False, False],
     [True, True, False],
     [False, True, True],
     [True, True, False],
     [False, True, False],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, True],
     [True, False, False],
     [False, False, False],
     [False, False, False]]],
   [[32766, False, False, False],
    [32766, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [2, False, False, False],
    [2, False, False, False],
    [2, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False]]]],
 [['regression: scenario 20',
   [-1,
    32767,
    [[False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False],
     [True, False, True],
     [False, True, False],
     [False, False, False],
     [False, False, False],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [True, True, False]]],
   [[32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False],
    [0, True, False, False],
    [-1, True, False, False],
    [-1, True, False, False],
    [-1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [-1, True, False, False]]],
  ['regression: scenario 43',
   [32767,
    32767,
    [[True, True, False],
     [True, True, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [True, False, True],
     [False, False, False]]],
   [[32767, True, True, True],
    [32767, True, True, True],
    [32767, True, True, True],
    [32767, True, True, True],
    [32766, False, True, True],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['control: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [0,
    -2,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [True, False, False],
     [False, False, True],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [False, False, False],
     [True, False, False]]],
   [[-1, False, False, False],
    [-1, False, False, False],
    [0, True, False, False],
    [-1, False, False, False],
    [-1, False, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(preset, start, scans):
    acc = start
    ov = False
    un = False
    pcu = False
    pcd = False
    out = []
    for cu, cd, res in scans:
        cu = bool(cu)
        cd = bool(cd)
        if cu and not pcu:
            acc += 1
            if acc > 32767:
                acc = -32768
                ov = True
        if cd and not pcd:
            acc -= 1
            if acc < -32768:
                acc = 32767
                un = True
        pcu = cu
        pcd = cd
        if res:
            acc = 0
            ov = False
        out.append([acc, acc >= preset, ov, un])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['regression: scenario 49',
   [3,
    -32768,
    [[False, True, False],
     [True, False, False],
     [False, True, False],
     [False, True, False],
     [False, False, False],
     [False, False, False],
     [False, False, True],
     [False, False, False],
     [False, False, False],
     [True, True, True],
     [True, False, False],
     [False, True, True],
     [False, True, False],
     [True, True, False]]],
   [[32767, True, False, True],
    [-32768, False, True, True],
    [32767, True, True, True],
    [32767, True, True, True],
    [32767, True, True, True],
    [32767, True, True, True],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['control: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [0,
    1,
    [[True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, True],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [False, False, False]]],
   [[2, True, False, False],
    [2, True, False, False],
    [2, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False]]],
  ['control: scenario 2',
   [3,
    32766,
    [[True, True, False],
     [False, False, False],
     [True, True, False],
     [False, False, True],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, True, False],
     [False, False, False],
     [True, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, False]]],
   [[32766, True, False, False],
    [32766, True, False, False],
    [32766, True, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [0, False, False, False],
    [0, False, False, False]]]],
 [['regression: scenario 4',
   [3,
    32767,
    [[False, False, False],
     [True, True, True],
     [False, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [False, False, False],
     [False, True, False],
     [False, True, False]]],
   [[32767, True, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [-1, False, False, False],
    [-1, False, False, False],
    [-2, False, False, False],
    [-2, False, False, False]]],
  ['regression: scenario 54',
   [32767,
    32767,
    [[True, False, False],
     [False, False, False],
     [False, True, False],
     [True, True, False],
     [False, True, False],
     [False, False, False],
     [True, False, True],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False],
     [False, True, False],
     [True, True, False],
     [False, True, False]]],
   [[-32768, False, True, False],
    [-32768, False, True, False],
    [32767, True, True, True],
    [-32768, False, True, True],
    [-32768, False, True, True],
    [-32768, False, True, True],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [2, False, False, False],
    [1, False, False, False],
    [2, False, False, False],
    [2, False, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['control: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [2,
    -32767,
    [[True, False, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [False, True, False],
     [False, False, False],
     [True, False, False],
     [True, False, False]]],
   [[-32766, False, False, False],
    [-32766, False, False, False],
    [-32766, False, False, False],
    [-32766, False, False, False],
    [-32767, False, False, False],
    [-32767, False, False, False],
    [-32766, False, False, False],
    [-32766, False, False, False]]]],
 [['regression: scenario 11',
   [-1,
    32767,
    [[False, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [False, True, True],
     [False, True, False],
     [False, True, False],
     [False, True, False],
     [True, True, False],
     [False, False, False]]],
   [[32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [32767, True, True, True],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False]]],
  ['regression: scenario 14',
   [-1,
    -32768,
    [[True, True, False],
     [True, False, False],
     [True, True, False],
     [True, True, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [True, True, True],
     [True, True, False],
     [False, False, False],
     [False, False, False],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [32767, True, False, True],
    [32767, True, False, True],
    [32767, True, False, True],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['control: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [3,
    32765,
    [[True, False, True],
     [True, False, False],
     [True, False, True],
     [False, True, False],
     [True, False, False],
     [True, True, True],
     [False, True, False],
     [False, False, False],
     [True, True, False],
     [False, True, False],
     [True, False, False],
     [False, False, False],
     [True, False, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [-1, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [2, False, False, False],
    [1, False, False, False]]]],
 [['regression: scenario 24',
   [-32768,
    -32767,
    [[True, False, False],
     [False, True, False],
     [True, False, False],
     [True, True, False],
     [True, False, False],
     [False, True, False],
     [False, False, False],
     [True, True, False],
     [True, True, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [False, True, True],
     [False, True, False]]],
   [[-32766, True, False, False],
    [-32767, True, False, False],
    [-32766, True, False, False],
    [-32767, True, False, False],
    [-32767, True, False, False],
    [-32768, True, False, False],
    [-32768, True, False, False],
    [-32768, True, False, False],
    [-32768, True, False, False],
    [-32768, True, False, False],
    [-32768, True, False, False],
    [-32768, True, False, False],
    [0, True, False, False],
    [0, True, False, False]]],
  ['regression: scenario 32',
   [-32768,
    -32768,
    [[False, True, False],
     [False, True, False],
     [True, False, True],
     [True, False, False],
     [True, False, False],
     [False, False, True],
     [False, False, False],
     [False, True, False],
     [False, True, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[32767, True, False, True],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [-1, True, False, False],
    [-1, True, False, False],
    [0, True, False, False],
    [-1, True, False, False],
    [-1, True, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['control: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [32767,
    32766,
    [[False, False, False],
     [True, True, False],
     [False, True, True],
     [True, True, False],
     [False, True, False],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, True],
     [True, False, False],
     [False, False, False],
     [False, False, False]]],
   [[32766, False, False, False],
    [32766, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [2, False, False, False],
    [2, False, False, False],
    [2, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False]]]],
 [['regression: scenario 20',
   [-1,
    32767,
    [[False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False],
     [True, False, True],
     [False, True, False],
     [False, False, False],
     [False, False, False],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [True, True, False]]],
   [[32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False],
    [0, True, False, False],
    [-1, True, False, False],
    [-1, True, False, False],
    [-1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [-1, True, False, False]]],
  ['regression: scenario 43',
   [32767,
    32767,
    [[True, True, False],
     [True, True, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [True, False, True],
     [False, False, False]]],
   [[32767, True, True, True],
    [32767, True, True, True],
    [32767, True, True, True],
    [32767, True, True, True],
    [32766, False, True, True],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['control: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [0,
    -2,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [True, False, False],
     [False, False, True],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [False, False, False],
     [True, False, False]]],
   [[-1, False, False, False],
    [-1, False, False, False],
    [0, True, False, False],
    [-1, False, False, False],
    [-1, False, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
After RES the accumulator reads zero but OV or UN is still set.

Up/down counter pair sharing one 16-bit signed accumulator, evaluated per scan; scans is a list of [cu, cd, res]. CU counts on its rising edge and CD on its own rising edge (both in one scan cancel). Incrementing 32767 wraps to -32768 and latches OV; decrementing -32768 wraps to 32767 and latches UN. A later RES rung clears ACC, OV and UN but not the edge memories. DN is ACC >= preset. Return [ACC, DN, OV, UN] per scan.

split: open-access


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

N = 1
observations = []
def solve(preset, start, scans):
    acc = start
    ov = False
    un = False
    pcu = False
    pcd = False
    out = []
    for cu, cd, res in scans:
        cu = bool(cu)
        cd = bool(cd)
        if cu and not pcu:
            acc += 1
            if acc > 32767:
                acc = -32768
                ov = True
        if cd and not pcd:
            acc -= 1
            if acc < -32768:
                acc = 32767
                un = True
        pcu = cu
        pcd = cd
        if res:
            acc = 0
            ov = False
            un = False
        out.append([acc, acc == preset, ov, un])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['regression: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['regression: scenario 1',
   [0,
    1,
    [[True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, True],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [False, False, False]]],
   [[2, True, False, False],
    [2, True, False, False],
    [2, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False]]],
  ['regression: scenario 2',
   [3,
    32766,
    [[True, True, False],
     [False, False, False],
     [True, True, False],
     [False, False, True],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, True, False],
     [False, False, False],
     [True, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, False]]],
   [[32766, True, False, False],
    [32766, True, False, False],
    [32766, True, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['regression: scenario 3',
   [0,
    32766,
    [[True, True, False],
     [True, True, False],
     [False, True, False],
     [True, True, False],
     [True, False, False],
     [False, True, True],
     [True, False, False],
     [True, False, False],
     [True, True, False],
     [False, False, False],
     [True, False, False]]],
   [[32766, True, False, False],
    [32766, True, False, False],
    [32766, True, False, False],
    [32767, True, False, False],
    [32767, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False]]]],
 [['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['regression: scenario 5',
   [0,
    0,
    [[False, False, False],
     [False, True, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, True, False, False],
    [-1, False, False, False],
    [-1, False, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['regression: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['regression: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [2,
    -32767,
    [[True, False, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [False, True, False],
     [False, False, False],
     [True, False, False],
     [True, False, False]]],
   [[-32766, False, False, False],
    [-32766, False, False, False],
    [-32766, False, False, False],
    [-32766, False, False, False],
    [-32767, False, False, False],
    [-32767, False, False, False],
    [-32766, False, False, False],
    [-32766, False, False, False]]],
  ['control: scenario 2',
   [2,
    0,
    [[True, False, False],
     [False, False, False],
     [True, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [True, False, False],
     [False, False, False],
     [True, True, False]]],
   [[1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False]]]],
 [['regression: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['regression: scenario 3',
   [0,
    32767,
    [[False, False, False],
     [False, False, False],
     [True, True, False],
     [True, False, True],
     [False, False, False],
     [False, False, False],
     [False, True, False],
     [False, False, False],
     [False, True, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [False, False, False],
     [False, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [32767, True, True, True],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [-1, False, False, False],
    [-1, False, False, False],
    [-2, False, False, False],
    [-1, False, False, False],
    [-1, False, False, False],
    [-2, False, False, False],
    [-2, False, False, False],
    [-2, False, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['regression: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: scenario 1',
   [3,
    32765,
    [[True, False, True],
     [True, False, False],
     [True, False, True],
     [False, True, False],
     [True, False, False],
     [True, True, True],
     [False, True, False],
     [False, False, False],
     [True, True, False],
     [False, True, False],
     [True, False, False],
     [False, False, False],
     [True, False, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [-1, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [2, False, False, False],
    [1, False, False, False]]],
  ['regression: scenario 2',
   [0,
    32765,
    [[False, False, False],
     [False, True, True],
     [True, False, False],
     [True, True, False],
     [False, False, False],
     [False, True, False],
     [True, False, False],
     [True, False, True],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [True, True, False],
     [True, False, False]]],
   [[32765, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [-1, False, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [-1, False, False, False],
    [-1, False, False, False]]]],
 [['regression: scenario 3',
   [5,
    32765,
    [[True, False, False],
     [True, False, False],
     [True, False, False],
     [True, True, False],
     [True, True, False],
     [True, False, False]]],
   [[32766, True, False, False],
    [32766, True, False, False],
    [32766, True, False, False],
    [32765, True, False, False],
    [32765, True, False, False],
    [32765, True, False, False]]],
  ['regression: scenario 13',
   [-1,
    1,
    [[False, True, False],
     [False, False, True],
     [True, False, False],
     [True, True, False],
     [False, True, False],
     [True, False, False],
     [True, False, False],
     [True, False, False]]],
   [[0, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['regression: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['regression: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [32767,
    32766,
    [[False, False, False],
     [True, True, False],
     [False, True, True],
     [True, True, False],
     [False, True, False],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, True],
     [True, False, False],
     [False, False, False],
     [False, False, False]]],
   [[32766, False, False, False],
    [32766, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [2, False, False, False],
    [2, False, False, False],
    [2, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False]]]],
 [['regression: scenario 3',
   [-32768,
    32767,
    [[False, False, False],
     [True, False, False],
     [False, True, False],
     [True, False, False],
     [False, True, False],
     [True, True, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [False, True, False],
     [True, False, True],
     [False, False, False],
     [True, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [-32768, True, True, False],
    [32767, True, True, True],
    [-32768, True, True, True],
    [32767, True, True, True],
    [-32768, True, True, True],
    [-32768, True, True, True],
    [-32768, True, True, True],
    [-32768, True, True, True],
    [32767, True, True, True],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False]]],
  ['regression: scenario 9',
   [0,
    1,
    [[True, False, False],
     [False, True, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [False, False, False],
     [True, True, False],
     [True, False, False],
     [True, True, False],
     [True, False, False],
     [True, True, False]]],
   [[2, True, False, False],
    [1, True, False, False],
    [2, True, False, False],
    [2, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [-1, False, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['regression: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['regression: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['regression: scenario 1',
   [0,
    -2,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [True, False, False],
     [False, False, True],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [False, False, False],
     [True, False, False]]],
   [[-1, False, False, False],
    [-1, False, False, False],
    [0, True, False, False],
    [-1, False, False, False],
    [-1, False, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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(preset, start, scans):
    acc = start
    ov = False
    un = False
    pcu = False
    pcd = False
    out = []
    for cu, cd, res in scans:
        cu = bool(cu)
        cd = bool(cd)
        if cu and not pcu:
            acc += 1
            if acc > 32767:
                acc = -32768
                ov = True
        if cd and not pcd:
            acc -= 1
            if acc < -32768:
                acc = 32767
                un = True
        pcu = cu
        pcd = cd
        if res:
            acc = 0
            ov = False
            un = False
        out.append([acc, acc >= preset and acc > 0, ov, un])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['regression: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['regression: scenario 1',
   [0,
    1,
    [[True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, True],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [False, False, False]]],
   [[2, True, False, False],
    [2, True, False, False],
    [2, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False]]],
  ['regression: scenario 2',
   [3,
    32766,
    [[True, True, False],
     [False, False, False],
     [True, True, False],
     [False, False, True],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, True, False],
     [False, False, False],
     [True, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, False]]],
   [[32766, True, False, False],
    [32766, True, False, False],
    [32766, True, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['regression: scenario 3',
   [0,
    32766,
    [[True, True, False],
     [True, True, False],
     [False, True, False],
     [True, True, False],
     [True, False, False],
     [False, True, True],
     [True, False, False],
     [True, False, False],
     [True, True, False],
     [False, False, False],
     [True, False, False]]],
   [[32766, True, False, False],
    [32766, True, False, False],
    [32766, True, False, False],
    [32767, True, False, False],
    [32767, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False]]]],
 [['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['regression: scenario 5',
   [0,
    0,
    [[False, False, False],
     [False, True, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, True, False, False],
    [-1, False, False, False],
    [-1, False, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['regression: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['regression: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [2,
    -32767,
    [[True, False, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [False, True, False],
     [False, False, False],
     [True, False, False],
     [True, False, False]]],
   [[-32766, False, False, False],
    [-32766, False, False, False],
    [-32766, False, False, False],
    [-32766, False, False, False],
    [-32767, False, False, False],
    [-32767, False, False, False],
    [-32766, False, False, False],
    [-32766, False, False, False]]],
  ['control: scenario 2',
   [2,
    0,
    [[True, False, False],
     [False, False, False],
     [True, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [True, False, False],
     [False, False, False],
     [True, True, False]]],
   [[1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False]]]],
 [['regression: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['regression: scenario 3',
   [0,
    32767,
    [[False, False, False],
     [False, False, False],
     [True, True, False],
     [True, False, True],
     [False, False, False],
     [False, False, False],
     [False, True, False],
     [False, False, False],
     [False, True, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [False, False, False],
     [False, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [32767, True, True, True],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [-1, False, False, False],
    [-1, False, False, False],
    [-2, False, False, False],
    [-1, False, False, False],
    [-1, False, False, False],
    [-2, False, False, False],
    [-2, False, False, False],
    [-2, False, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['regression: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['control: scenario 1',
   [3,
    32765,
    [[True, False, True],
     [True, False, False],
     [True, False, True],
     [False, True, False],
     [True, False, False],
     [True, True, True],
     [False, True, False],
     [False, False, False],
     [True, True, False],
     [False, True, False],
     [True, False, False],
     [False, False, False],
     [True, False, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [-1, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [2, False, False, False],
    [1, False, False, False]]],
  ['regression: scenario 2',
   [0,
    32765,
    [[False, False, False],
     [False, True, True],
     [True, False, False],
     [True, True, False],
     [False, False, False],
     [False, True, False],
     [True, False, False],
     [True, False, True],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [True, True, False],
     [True, False, False]]],
   [[32765, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [-1, False, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [-1, False, False, False],
    [-1, False, False, False]]]],
 [['regression: scenario 3',
   [5,
    32765,
    [[True, False, False],
     [True, False, False],
     [True, False, False],
     [True, True, False],
     [True, True, False],
     [True, False, False]]],
   [[32766, True, False, False],
    [32766, True, False, False],
    [32766, True, False, False],
    [32765, True, False, False],
    [32765, True, False, False],
    [32765, True, False, False]]],
  ['regression: scenario 13',
   [-1,
    1,
    [[False, True, False],
     [False, False, True],
     [True, False, False],
     [True, True, False],
     [False, True, False],
     [True, False, False],
     [True, False, False],
     [True, False, False]]],
   [[0, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['regression: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['regression: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['control: scenario 1',
   [32767,
    32766,
    [[False, False, False],
     [True, True, False],
     [False, True, True],
     [True, True, False],
     [False, True, False],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, True],
     [True, False, False],
     [False, False, False],
     [False, False, False]]],
   [[32766, False, False, False],
    [32766, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [2, False, False, False],
    [2, False, False, False],
    [2, False, False, False],
    [0, False, False, False],
    [1, False, False, False],
    [1, False, False, False],
    [1, False, False, False]]]],
 [['regression: scenario 3',
   [-32768,
    32767,
    [[False, False, False],
     [True, False, False],
     [False, True, False],
     [True, False, False],
     [False, True, False],
     [True, True, False],
     [True, False, False],
     [False, False, False],
     [False, False, False],
     [False, True, False],
     [True, False, True],
     [False, False, False],
     [True, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [-32768, True, True, False],
    [32767, True, True, True],
    [-32768, True, True, True],
    [32767, True, True, True],
    [-32768, True, True, True],
    [-32768, True, True, True],
    [-32768, True, True, True],
    [-32768, True, True, True],
    [32767, True, True, True],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False]]],
  ['regression: scenario 9',
   [0,
    1,
    [[True, False, False],
     [False, True, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [False, False, False],
     [True, True, False],
     [True, False, False],
     [True, True, False],
     [True, False, False],
     [True, True, False]]],
   [[2, True, False, False],
    [1, True, False, False],
    [2, True, False, False],
    [2, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [-1, False, False, False]]],
  ['control: held count input',
   [3, 0, [[True, False, False], [True, False, False], [False, False, False], [True, False, False]]],
   [[1, False, False, False], [1, False, False, False], [1, False, False, False], [2, False, False, False]]],
  ['regression: overflow wrap',
   [0,
    32766,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [False, False, False],
     [True, False, False]]],
   [[32767, True, False, False],
    [32767, True, False, False],
    [-32768, False, True, False],
    [-32768, False, True, False],
    [-32767, False, True, False]]],
  ['regression: underflow then reset',
   [0,
    -32767,
    [[False, True, False],
     [False, False, False],
     [False, True, False],
     [False, False, True],
     [False, False, False]]],
   [[-32768, False, False, False],
    [-32768, False, False, False],
    [32767, True, False, True],
    [0, True, False, False],
    [0, True, False, False]]],
  ['control: simultaneous edges',
   [1,
    0,
    [[True, True, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [False, True, False]]],
   [[0, False, False, False],
    [0, False, False, False],
    [1, True, False, False],
    [0, False, False, False],
    [0, False, False, False]]],
  ['regression: count down while up held',
   [0, 5, [[True, False, False], [True, True, False], [True, False, False], [True, True, False]]],
   [[6, True, False, False], [5, True, False, False], [5, True, False, False], [4, True, False, False]]],
  ['regression: scenario 1',
   [0,
    -2,
    [[True, False, False],
     [False, False, False],
     [True, False, False],
     [True, True, False],
     [True, False, False],
     [False, False, True],
     [True, False, False],
     [True, False, False],
     [True, False, False],
     [False, True, False],
     [False, False, False],
     [True, False, False]]],
   [[-1, False, False, False],
    [-1, False, False, False],
    [0, True, False, False],
    [-1, False, False, False],
    [-1, False, False, False],
    [0, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [1, True, False, False],
    [0, True, False, False],
    [0, True, False, False],
    [1, True, False, False]]]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
DN turns off as soon as the count passes the preset.

Up/down counter pair sharing one 16-bit signed accumulator, evaluated per scan; scans is a list of [cu, cd, res]. CU counts on its rising edge and CD on its own rising edge (both in one scan cancel). Incrementing 32767 wraps to -32768 and latches OV; decrementing -32768 wraps to 32767 and latches UN. A later RES rung clears ACC, OV and UN but not the edge memories. DN is ACC >= preset. Return [ACC, DN, OV, UN] per scan.

split: open-access