broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import difflib
N = 1
observations = []
def solve(base, ours, theirs):
    def matches(x):
        sm = difflib.SequenceMatcher(None, base, x, autojunk=False)
        mp = {}
        for i, j, size in sm.get_matching_blocks():
            for t in range(size):
                mp[i + t] = j + t
        return mp
    mo, mt = matches(ours), matches(theirs)
    out = []
    conflicts = 0
    i = j = k = 0
    anchors = [bi for bi in range(len(base)) if bi in mo and bi in mt] + [None]
    for bi in anchors:
        if bi is None:
            i1, j1, k1 = len(base), len(ours), len(theirs)
        else:
            i1, j1, k1 = bi, mo[bi], mt[bi]
        B, O, T = base[i:i1], ours[j:j1], theirs[k:k1]
        if O == B:
            out.extend(T)
        elif T == B:
            out.extend(O)
        elif O == T:
            out.extend(O)
        else:
            conflicts += 1
            out.append('<<<<<<< ours')
            out.extend(O)
            out.append('||||||| base')
            out.extend(B)
            out.append('=======')
            out.extend(T)
            out.append('>>>>>>> theirs')
        if bi is not None:
            out.append(base[bi])
            i, j, k = i1 + 1, j1 + 1, k1
    return {'merged': out, 'conflicts': conflicts}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C1', 'd', 'e'], ['a', 'b', 'C1', 'd', 'e']], {'merged': ['a', 'b', 'C1', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    2: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C2', 'd', 'e'], ['a', 'b', 'C2', 'd', 'e']], {'merged': ['a', 'b', 'C2', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    3: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C3', 'd', 'e'], ['a', 'b', 'C3', 'd', 'e']], {'merged': ['a', 'b', 'C3', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    4: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C4', 'd', 'e'], ['a', 'b', 'C4', 'd', 'e']], {'merged': ['a', 'b', 'C4', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2', 'o3'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', 'o3', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    5: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C5', 'd', 'e'], ['a', 'b', 'C5', 'd', 'e']], {'merged': ['a', 'b', 'C5', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2', 'o3', 'o4'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', 'o3', 'o4', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3', 't4']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3', 't4'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
}[N]
for label, args, expected in cases:
    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
import difflib
N = 1
observations = []
def solve(base, ours, theirs):
    def matches(x):
        sm = difflib.SequenceMatcher(None, base, x, autojunk=False)
        mp = {}
        for i, j, size in sm.get_matching_blocks():
            for t in range(size):
                mp[i + t] = j + t
        return mp
    mo, mt = matches(ours), matches(theirs)
    out = []
    conflicts = 0
    i = j = k = 0
    anchors = [bi for bi in range(len(base)) if bi in mo and bi in mt] + [None]
    for bi in anchors:
        if bi is None:
            i1, j1, k1 = len(base), len(ours), len(theirs)
        else:
            i1, j1, k1 = bi, mo[bi], mt[bi]
        B, O, T = base[i:i1], ours[j:j1], theirs[k:k1]
        if O == B:
            out.extend(T)
        elif T == B:
            out.extend(O)
        elif O == T:
            out.extend(O)
        else:
            conflicts += 1
            out.append('<<<<<<< ours')
            out.extend(O)
            out.append('||||||| base')
            out.extend(B)
            out.append('=======')
            out.extend(T)
            out.append('>>>>>>> theirs')
        if bi is not None:
            out.append(base[bi])
            i, j, k = i1, j1 + 1, k1 + 1
    return {'merged': out, 'conflicts': conflicts}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C1', 'd', 'e'], ['a', 'b', 'C1', 'd', 'e']], {'merged': ['a', 'b', 'C1', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    2: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C2', 'd', 'e'], ['a', 'b', 'C2', 'd', 'e']], {'merged': ['a', 'b', 'C2', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    3: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C3', 'd', 'e'], ['a', 'b', 'C3', 'd', 'e']], {'merged': ['a', 'b', 'C3', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    4: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C4', 'd', 'e'], ['a', 'b', 'C4', 'd', 'e']], {'merged': ['a', 'b', 'C4', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2', 'o3'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', 'o3', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    5: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C5', 'd', 'e'], ['a', 'b', 'C5', 'd', 'e']], {'merged': ['a', 'b', 'C5', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2', 'o3', 'o4'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', 'o3', 'o4', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3', 't4']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3', 't4'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
}[N]
for label, args, expected in cases:
    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 every stable line, their region looks changed and clean merges become conflicts or duplicate the anchor.

Match base against ours and against theirs with difflib.SequenceMatcher (autojunk off). Base lines matched on both sides are stable anchors. Between consecutive anchors (and after the last) compare the three regions: unchanged ours takes theirs, unchanged theirs takes ours, identical changes take ours, anything else is a conflict rendered as "<<<<<<< ours", ours, "||||||| base", base, "=======", theirs, ">>>>>>> theirs". Return merged lines and the conflict count.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import difflib
N = 1
observations = []
def solve(base, ours, theirs):
    def matches(x):
        sm = difflib.SequenceMatcher(None, base, x, autojunk=False)
        mp = {}
        for i, j, size in sm.get_matching_blocks():
            for t in range(size):
                mp[i + t] = j + t
        return mp
    mo, mt = matches(ours), matches(theirs)
    out = []
    conflicts = 0
    i = j = k = 0
    anchors = [bi for bi in range(len(base)) if bi in mo and bi in mt] + [None]
    for bi in anchors:
        if bi is None:
            i1, j1, k1 = len(base), len(ours), len(theirs)
        else:
            i1, j1, k1 = bi, mo[bi], mt[bi]
        B, O, T = base[i:i1], ours[j:j1], theirs[k:k1]
        if O == B:
            out.extend(T)
        elif T == B:
            out.extend(O)
        elif O == T:
            out.extend(O)
        else:
            conflicts += 1
            out.append('<<<<<<< ours')
            out.extend(O)
            out.append('=======')
            out.extend(T)
            out.append('>>>>>>> theirs')
        if bi is not None:
            out.append(base[bi])
            i, j, k = i1 + 1, j1 + 1, k1 + 1
    return {'merged': out, 'conflicts': conflicts}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C1', 'd', 'e'], ['a', 'b', 'C1', 'd', 'e']], {'merged': ['a', 'b', 'C1', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    2: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C2', 'd', 'e'], ['a', 'b', 'C2', 'd', 'e']], {'merged': ['a', 'b', 'C2', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    3: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C3', 'd', 'e'], ['a', 'b', 'C3', 'd', 'e']], {'merged': ['a', 'b', 'C3', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    4: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C4', 'd', 'e'], ['a', 'b', 'C4', 'd', 'e']], {'merged': ['a', 'b', 'C4', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2', 'o3'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', 'o3', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    5: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C5', 'd', 'e'], ['a', 'b', 'C5', 'd', 'e']], {'merged': ['a', 'b', 'C5', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2', 'o3', 'o4'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', 'o3', 'o4', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3', 't4']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3', 't4'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
}[N]
for label, args, expected in cases:
    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
import difflib
N = 1
observations = []
def solve(base, ours, theirs):
    def matches(x):
        sm = difflib.SequenceMatcher(None, base, x, autojunk=False)
        mp = {}
        for i, j, size in sm.get_matching_blocks():
            for t in range(size):
                mp[i + t] = j + t
        return mp
    mo, mt = matches(ours), matches(theirs)
    out = []
    conflicts = 0
    i = j = k = 0
    anchors = [bi for bi in range(len(base)) if bi in mo and bi in mt] + [None]
    for bi in anchors:
        if bi is None:
            i1, j1, k1 = len(base), len(ours), len(theirs)
        else:
            i1, j1, k1 = bi, mo[bi], mt[bi]
        B, O, T = base[i:i1], ours[j:j1], theirs[k:k1]
        if O == B:
            out.extend(T)
        elif T == B:
            out.extend(O)
        elif O == T:
            out.extend(O)
        else:
            conflicts += 1
            out.append('<<<<<<< ours')
            out.extend(O)
            out.append('||||||| base')
            out.append('=======')
            out.extend(T)
            out.append('>>>>>>> theirs')
        if bi is not None:
            out.append(base[bi])
            i, j, k = i1 + 1, j1 + 1, k1 + 1
    return {'merged': out, 'conflicts': conflicts}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C1', 'd', 'e'], ['a', 'b', 'C1', 'd', 'e']], {'merged': ['a', 'b', 'C1', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    2: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C2', 'd', 'e'], ['a', 'b', 'C2', 'd', 'e']], {'merged': ['a', 'b', 'C2', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    3: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C3', 'd', 'e'], ['a', 'b', 'C3', 'd', 'e']], {'merged': ['a', 'b', 'C3', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    4: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C4', 'd', 'e'], ['a', 'b', 'C4', 'd', 'e']], {'merged': ['a', 'b', 'C4', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2', 'o3'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', 'o3', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    5: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C5', 'd', 'e'], ['a', 'b', 'C5', 'd', 'e']], {'merged': ['a', 'b', 'C5', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2', 'o3', 'o4'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', 'o3', 'o4', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3', 't4']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3', 't4'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
}[N]
for label, args, expected in cases:
    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:
Conflict hunks lack the original text, contrary to the diff3 conflict style.

Match base against ours and against theirs with difflib.SequenceMatcher (autojunk off). Base lines matched on both sides are stable anchors. Between consecutive anchors (and after the last) compare the three regions: unchanged ours takes theirs, unchanged theirs takes ours, identical changes take ours, anything else is a conflict rendered as "<<<<<<< ours", ours, "||||||| base", base, "=======", theirs, ">>>>>>> theirs". Return merged lines and the conflict count.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import difflib
N = 1
observations = []
def solve(base, ours, theirs):
    def matches(x):
        sm = difflib.SequenceMatcher(None, base, x, autojunk=False)
        mp = {}
        for i, j, size in sm.get_matching_blocks():
            for t in range(size):
                mp[i + t] = j + t
        return mp
    mo, mt = matches(ours), matches(theirs)
    out = []
    conflicts = 0
    i = j = k = 0
    anchors = [bi for bi in range(len(base)) if bi in mo and bi in mt]
    for bi in anchors:
        if bi is None:
            i1, j1, k1 = len(base), len(ours), len(theirs)
        else:
            i1, j1, k1 = bi, mo[bi], mt[bi]
        B, O, T = base[i:i1], ours[j:j1], theirs[k:k1]
        if O == B:
            out.extend(T)
        elif T == B:
            out.extend(O)
        elif O == T:
            out.extend(O)
        else:
            conflicts += 1
            out.append('<<<<<<< ours')
            out.extend(O)
            out.append('||||||| base')
            out.extend(B)
            out.append('=======')
            out.extend(T)
            out.append('>>>>>>> theirs')
        if bi is not None:
            out.append(base[bi])
            i, j, k = i1 + 1, j1 + 1, k1 + 1
    return {'merged': out, 'conflicts': conflicts}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C1', 'd', 'e'], ['a', 'b', 'C1', 'd', 'e']], {'merged': ['a', 'b', 'C1', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    2: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C2', 'd', 'e'], ['a', 'b', 'C2', 'd', 'e']], {'merged': ['a', 'b', 'C2', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    3: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C3', 'd', 'e'], ['a', 'b', 'C3', 'd', 'e']], {'merged': ['a', 'b', 'C3', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    4: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C4', 'd', 'e'], ['a', 'b', 'C4', 'd', 'e']], {'merged': ['a', 'b', 'C4', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2', 'o3'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', 'o3', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    5: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C5', 'd', 'e'], ['a', 'b', 'C5', 'd', 'e']], {'merged': ['a', 'b', 'C5', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2', 'o3', 'o4'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', 'o3', 'o4', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3', 't4']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3', 't4'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
}[N]
for label, args, expected in cases:
    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
import difflib
N = 1
observations = []
def solve(base, ours, theirs):
    def matches(x):
        sm = difflib.SequenceMatcher(None, base, x, autojunk=False)
        mp = {}
        for i, j, size in sm.get_matching_blocks():
            for t in range(size):
                mp[i + t] = j + t
        return mp
    mo, mt = matches(ours), matches(theirs)
    out = []
    conflicts = 0
    i = j = k = 0
    anchors = [bi for bi in range(len(base)) if bi in mo and bi in mt] + [None]
    for bi in anchors:
        if bi is None:
            if i >= len(base):
                break
            i1, j1, k1 = len(base), len(ours), len(theirs)
        else:
            i1, j1, k1 = bi, mo[bi], mt[bi]
        B, O, T = base[i:i1], ours[j:j1], theirs[k:k1]
        if O == B:
            out.extend(T)
        elif T == B:
            out.extend(O)
        elif O == T:
            out.extend(O)
        else:
            conflicts += 1
            out.append('<<<<<<< ours')
            out.extend(O)
            out.append('||||||| base')
            out.extend(B)
            out.append('=======')
            out.extend(T)
            out.append('>>>>>>> theirs')
        if bi is not None:
            out.append(base[bi])
            i, j, k = i1 + 1, j1 + 1, k1 + 1
    return {'merged': out, 'conflicts': conflicts}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C1', 'd', 'e'], ['a', 'b', 'C1', 'd', 'e']], {'merged': ['a', 'b', 'C1', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    2: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C2', 'd', 'e'], ['a', 'b', 'C2', 'd', 'e']], {'merged': ['a', 'b', 'C2', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    3: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C3', 'd', 'e'], ['a', 'b', 'C3', 'd', 'e']], {'merged': ['a', 'b', 'C3', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    4: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C4', 'd', 'e'], ['a', 'b', 'C4', 'd', 'e']], {'merged': ['a', 'b', 'C4', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2', 'o3'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', 'o3', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
    5: [('only ours changed', [['a', 'b', 'c', 'd', 'e'], ['a', 'B', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e']], {'merged': ['a', 'B', 'c', 'd', 'e'], 'conflicts': 0}), ('only theirs deleted a line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'd', 'e']], {'merged': ['a', 'b', 'd', 'e'], 'conflicts': 0}), ('both made the same change', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'C5', 'd', 'e'], ['a', 'b', 'C5', 'd', 'e']], {'merged': ['a', 'b', 'C5', 'd', 'e'], 'conflicts': 0}), ('both deleted the same line', [['a', 'b', 'c', 'd', 'e'], ['a', 'c', 'd', 'e'], ['a', 'c', 'd', 'e']], {'merged': ['a', 'c', 'd', 'e'], 'conflicts': 0}), ('conflicting edits', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'X', 'd', 'e'], ['a', 'b', 'Y', 'd', 'e']], {'merged': ['a', 'b', '<<<<<<< ours', 'X', '||||||| base', 'c', '=======', 'Y', '>>>>>>> theirs', 'd', 'e'], 'conflicts': 1}), ('changes after the last stable line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 'o0', 'o1', 'o2', 'o3', 'o4'], ['a', 'b', 'c', 'd', 'e', 'T']], {'merged': ['a', 'b', 'c', 'd', 'e', '<<<<<<< ours', 'o0', 'o1', 'o2', 'o3', 'o4', '||||||| base', '=======', 'T', '>>>>>>> theirs'], 'conflicts': 1}), ('appended by one side only', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3', 't4']], {'merged': ['a', 'b', 'c', 'd', 'e', 't0', 't1', 't2', 't3', 't4'], 'conflicts': 0}), ('changes on both sides in different places', [['a', 'b', 'c', 'd', 'e'], ['A', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'D', 'e']], {'merged': ['A', 'b', 'c', 'D', 'e'], 'conflicts': 0}), ('theirs changes the last base line', [['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'e'], ['a', 'b', 'c', 'd', 'E']], {'merged': ['a', 'b', 'c', 'd', 'E'], 'conflicts': 0})],
}[N]
for label, args, expected in cases:
    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:
Lines appended at the end of a file by either branch disappear from the merge.

Match base against ours and against theirs with difflib.SequenceMatcher (autojunk off). Base lines matched on both sides are stable anchors. Between consecutive anchors (and after the last) compare the three regions: unchanged ours takes theirs, unchanged theirs takes ours, identical changes take ours, anything else is a conflict rendered as "<<<<<<< ours", ours, "||||||| base", base, "=======", theirs, ">>>>>>> theirs". Return merged lines and the conflict count.

split: open-access


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

N = 1
observations = []
def solve(lines, strategy):
    out = []
    state = None
    o, b, t = [], [], []
    count = 0
    def marker(line, ch):
        return line.startswith(ch * 7)
    for line in lines:
        if state is None:
            if marker(line, '<'):
                state = 'o'
                o, b, t = [], [], []
            else:
                out.append(line)
        elif state == 'o' and marker(line, '|'):
            state = 'b'
        elif state in ('o', 'b') and line == '=' * 7:
            state = 't'
        elif state == 't' and marker(line, '>'):
            count += 1
            out.extend({'ours': o, 'theirs': t, 'union': o + t, 'base': b}[strategy])
            state = None
        else:
            {'o': o, 'b': b, 't': t}[state].append(line)
    if state is not None:
        return {'error': 'unterminated conflict', 'conflicts': count}
    return {'lines': out, 'conflicts': count}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('take ours', [['h', '<<<<<<< ours', 'o1', '=======', 't1', '>>>>>>> theirs', 'z'], 'ours'], {'lines': ['h', 'o1', 'z'], 'conflicts': 1}), ('take theirs with base section', [['h', '<<<<<<< ours', 'o1', '||||||| base', 'b1', '=======', 't1', 't2', '>>>>>>> theirs'], 'theirs'], {'lines': ['h', 't1', 't2'], 'conflicts': 1}), ('union keeps both in order', [['<<<<<<< ours', 'x', 'y', '=======', 'y', 'z1', '>>>>>>> theirs'], 'union'], {'lines': ['x', 'y', 'y', 'z1'], 'conflicts': 1}), ('base strategy', [['<<<<<<<', 'o', '|||||||', 'b1', '=======', 't', '>>>>>>>'], 'base'], {'lines': ['b1'], 'conflicts': 1}), ('long marker-like content lines', [['<<<<<<<<<< banner', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs', '>>>>>>>>'], 'ours'], {'lines': ['<<<<<<<<<< banner', 'a', '>>>>>>>>'], 'conflicts': 1}), ('marker prefix glued to text', [['<<<<<<<x', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs'], 'theirs'], {'lines': ['<<<<<<<x', 'b'], 'conflicts': 1}), ('setext underline inside ours', [['<<<<<<< ours', 'Title', '========', '=======', 'T', '>>>>>>> theirs'], 'ours'], {'lines': ['Title', '========'], 'conflicts': 1}), ('indented separator-like line', [['<<<<<<< ours', '  =======', 'k', '=======', 't', '>>>>>>> theirs'], 'union'], {'lines': ['  =======', 'k', 't'], 'conflicts': 1}), ('unterminated conflict is an error', [['a', '<<<<<<< ours', 'o', '=======', 't'], 'ours'], {'error': 'unterminated conflict', 'conflicts': 0}), ('two conflicts', [['<<<<<<< ours', '1', '=======', '2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs'], 'theirs'], {'lines': ['2', 'mid', '4'], 'conflicts': 2})],
    2: [('take ours', [['h', '<<<<<<< ours', 'o1', '=======', 't1', '>>>>>>> theirs', 'z'], 'ours'], {'lines': ['h', 'o1', 'z'], 'conflicts': 1}), ('take theirs with base section', [['h', '<<<<<<< ours', 'o1', '||||||| base', 'b1', '=======', 't1', 't2', '>>>>>>> theirs'], 'theirs'], {'lines': ['h', 't1', 't2'], 'conflicts': 1}), ('union keeps both in order', [['<<<<<<< ours', 'x', 'y', '=======', 'y', 'z2', '>>>>>>> theirs'], 'union'], {'lines': ['x', 'y', 'y', 'z2'], 'conflicts': 1}), ('base strategy', [['<<<<<<<', 'o', '|||||||', 'b2', '=======', 't', '>>>>>>>'], 'base'], {'lines': ['b2'], 'conflicts': 1}), ('long marker-like content lines', [['<<<<<<<<<< banner', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs', '>>>>>>>>'], 'ours'], {'lines': ['<<<<<<<<<< banner', 'a', '>>>>>>>>'], 'conflicts': 1}), ('marker prefix glued to text', [['<<<<<<<x', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs'], 'theirs'], {'lines': ['<<<<<<<x', 'b'], 'conflicts': 1}), ('setext underline inside ours', [['<<<<<<< ours', 'Title', '========', '=======', 'T', '>>>>>>> theirs'], 'ours'], {'lines': ['Title', '========'], 'conflicts': 1}), ('indented separator-like line', [['<<<<<<< ours', '  =======', 'k', '=======', 't', '>>>>>>> theirs'], 'union'], {'lines': ['  =======', 'k', 't'], 'conflicts': 1}), ('unterminated conflict is an error', [['a', '<<<<<<< ours', 'o', '=======', 't'], 'ours'], {'error': 'unterminated conflict', 'conflicts': 0}), ('two conflicts', [['<<<<<<< ours', '1', '=======', '2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs'], 'theirs'], {'lines': ['2', 'mid', '4', '4'], 'conflicts': 3})],
    3: [('take ours', [['h', '<<<<<<< ours', 'o1', '=======', 't1', '>>>>>>> theirs', 'z'], 'ours'], {'lines': ['h', 'o1', 'z'], 'conflicts': 1}), ('take theirs with base section', [['h', '<<<<<<< ours', 'o1', '||||||| base', 'b1', '=======', 't1', 't2', '>>>>>>> theirs'], 'theirs'], {'lines': ['h', 't1', 't2'], 'conflicts': 1}), ('union keeps both in order', [['<<<<<<< ours', 'x', 'y', '=======', 'y', 'z3', '>>>>>>> theirs'], 'union'], {'lines': ['x', 'y', 'y', 'z3'], 'conflicts': 1}), ('base strategy', [['<<<<<<<', 'o', '|||||||', 'b3', '=======', 't', '>>>>>>>'], 'base'], {'lines': ['b3'], 'conflicts': 1}), ('long marker-like content lines', [['<<<<<<<<<< banner', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs', '>>>>>>>>'], 'ours'], {'lines': ['<<<<<<<<<< banner', 'a', '>>>>>>>>'], 'conflicts': 1}), ('marker prefix glued to text', [['<<<<<<<x', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs'], 'theirs'], {'lines': ['<<<<<<<x', 'b'], 'conflicts': 1}), ('setext underline inside ours', [['<<<<<<< ours', 'Title', '========', '=======', 'T', '>>>>>>> theirs'], 'ours'], {'lines': ['Title', '========'], 'conflicts': 1}), ('indented separator-like line', [['<<<<<<< ours', '  =======', 'k', '=======', 't', '>>>>>>> theirs'], 'union'], {'lines': ['  =======', 'k', 't'], 'conflicts': 1}), ('unterminated conflict is an error', [['a', '<<<<<<< ours', 'o', '=======', 't'], 'ours'], {'error': 'unterminated conflict', 'conflicts': 0}), ('two conflicts', [['<<<<<<< ours', '1', '=======', '2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs'], 'theirs'], {'lines': ['2', 'mid', '4', '4', '4'], 'conflicts': 4})],
    4: [('take ours', [['h', '<<<<<<< ours', 'o1', '=======', 't1', '>>>>>>> theirs', 'z'], 'ours'], {'lines': ['h', 'o1', 'z'], 'conflicts': 1}), ('take theirs with base section', [['h', '<<<<<<< ours', 'o1', '||||||| base', 'b1', '=======', 't1', 't2', '>>>>>>> theirs'], 'theirs'], {'lines': ['h', 't1', 't2'], 'conflicts': 1}), ('union keeps both in order', [['<<<<<<< ours', 'x', 'y', '=======', 'y', 'z4', '>>>>>>> theirs'], 'union'], {'lines': ['x', 'y', 'y', 'z4'], 'conflicts': 1}), ('base strategy', [['<<<<<<<', 'o', '|||||||', 'b4', '=======', 't', '>>>>>>>'], 'base'], {'lines': ['b4'], 'conflicts': 1}), ('long marker-like content lines', [['<<<<<<<<<< banner', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs', '>>>>>>>>'], 'ours'], {'lines': ['<<<<<<<<<< banner', 'a', '>>>>>>>>'], 'conflicts': 1}), ('marker prefix glued to text', [['<<<<<<<x', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs'], 'theirs'], {'lines': ['<<<<<<<x', 'b'], 'conflicts': 1}), ('setext underline inside ours', [['<<<<<<< ours', 'Title', '========', '=======', 'T', '>>>>>>> theirs'], 'ours'], {'lines': ['Title', '========'], 'conflicts': 1}), ('indented separator-like line', [['<<<<<<< ours', '  =======', 'k', '=======', 't', '>>>>>>> theirs'], 'union'], {'lines': ['  =======', 'k', 't'], 'conflicts': 1}), ('unterminated conflict is an error', [['a', '<<<<<<< ours', 'o', '=======', 't'], 'ours'], {'error': 'unterminated conflict', 'conflicts': 0}), ('two conflicts', [['<<<<<<< ours', '1', '=======', '2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs'], 'theirs'], {'lines': ['2', 'mid', '4', '4', '4', '4'], 'conflicts': 5})],
    5: [('take ours', [['h', '<<<<<<< ours', 'o1', '=======', 't1', '>>>>>>> theirs', 'z'], 'ours'], {'lines': ['h', 'o1', 'z'], 'conflicts': 1}), ('take theirs with base section', [['h', '<<<<<<< ours', 'o1', '||||||| base', 'b1', '=======', 't1', 't2', '>>>>>>> theirs'], 'theirs'], {'lines': ['h', 't1', 't2'], 'conflicts': 1}), ('union keeps both in order', [['<<<<<<< ours', 'x', 'y', '=======', 'y', 'z5', '>>>>>>> theirs'], 'union'], {'lines': ['x', 'y', 'y', 'z5'], 'conflicts': 1}), ('base strategy', [['<<<<<<<', 'o', '|||||||', 'b5', '=======', 't', '>>>>>>>'], 'base'], {'lines': ['b5'], 'conflicts': 1}), ('long marker-like content lines', [['<<<<<<<<<< banner', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs', '>>>>>>>>'], 'ours'], {'lines': ['<<<<<<<<<< banner', 'a', '>>>>>>>>'], 'conflicts': 1}), ('marker prefix glued to text', [['<<<<<<<x', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs'], 'theirs'], {'lines': ['<<<<<<<x', 'b'], 'conflicts': 1}), ('setext underline inside ours', [['<<<<<<< ours', 'Title', '========', '=======', 'T', '>>>>>>> theirs'], 'ours'], {'lines': ['Title', '========'], 'conflicts': 1}), ('indented separator-like line', [['<<<<<<< ours', '  =======', 'k', '=======', 't', '>>>>>>> theirs'], 'union'], {'lines': ['  =======', 'k', 't'], 'conflicts': 1}), ('unterminated conflict is an error', [['a', '<<<<<<< ours', 'o', '=======', 't'], 'ours'], {'error': 'unterminated conflict', 'conflicts': 0}), ('two conflicts', [['<<<<<<< ours', '1', '=======', '2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs'], 'theirs'], {'lines': ['2', 'mid', '4', '4', '4', '4', '4'], 'conflicts': 6})],
}[N]
for label, args, expected in cases:
    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(lines, strategy):
    out = []
    state = None
    o, b, t = [], [], []
    count = 0
    def marker(line, ch):
        return line.startswith(ch * 7) and not line.startswith(ch * 8)
    for line in lines:
        if state is None:
            if marker(line, '<'):
                state = 'o'
                o, b, t = [], [], []
            else:
                out.append(line)
        elif state == 'o' and marker(line, '|'):
            state = 'b'
        elif state in ('o', 'b') and line == '=' * 7:
            state = 't'
        elif state == 't' and marker(line, '>'):
            count += 1
            out.extend({'ours': o, 'theirs': t, 'union': o + t, 'base': b}[strategy])
            state = None
        else:
            {'o': o, 'b': b, 't': t}[state].append(line)
    if state is not None:
        return {'error': 'unterminated conflict', 'conflicts': count}
    return {'lines': out, 'conflicts': count}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('take ours', [['h', '<<<<<<< ours', 'o1', '=======', 't1', '>>>>>>> theirs', 'z'], 'ours'], {'lines': ['h', 'o1', 'z'], 'conflicts': 1}), ('take theirs with base section', [['h', '<<<<<<< ours', 'o1', '||||||| base', 'b1', '=======', 't1', 't2', '>>>>>>> theirs'], 'theirs'], {'lines': ['h', 't1', 't2'], 'conflicts': 1}), ('union keeps both in order', [['<<<<<<< ours', 'x', 'y', '=======', 'y', 'z1', '>>>>>>> theirs'], 'union'], {'lines': ['x', 'y', 'y', 'z1'], 'conflicts': 1}), ('base strategy', [['<<<<<<<', 'o', '|||||||', 'b1', '=======', 't', '>>>>>>>'], 'base'], {'lines': ['b1'], 'conflicts': 1}), ('long marker-like content lines', [['<<<<<<<<<< banner', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs', '>>>>>>>>'], 'ours'], {'lines': ['<<<<<<<<<< banner', 'a', '>>>>>>>>'], 'conflicts': 1}), ('marker prefix glued to text', [['<<<<<<<x', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs'], 'theirs'], {'lines': ['<<<<<<<x', 'b'], 'conflicts': 1}), ('setext underline inside ours', [['<<<<<<< ours', 'Title', '========', '=======', 'T', '>>>>>>> theirs'], 'ours'], {'lines': ['Title', '========'], 'conflicts': 1}), ('indented separator-like line', [['<<<<<<< ours', '  =======', 'k', '=======', 't', '>>>>>>> theirs'], 'union'], {'lines': ['  =======', 'k', 't'], 'conflicts': 1}), ('unterminated conflict is an error', [['a', '<<<<<<< ours', 'o', '=======', 't'], 'ours'], {'error': 'unterminated conflict', 'conflicts': 0}), ('two conflicts', [['<<<<<<< ours', '1', '=======', '2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs'], 'theirs'], {'lines': ['2', 'mid', '4'], 'conflicts': 2})],
    2: [('take ours', [['h', '<<<<<<< ours', 'o1', '=======', 't1', '>>>>>>> theirs', 'z'], 'ours'], {'lines': ['h', 'o1', 'z'], 'conflicts': 1}), ('take theirs with base section', [['h', '<<<<<<< ours', 'o1', '||||||| base', 'b1', '=======', 't1', 't2', '>>>>>>> theirs'], 'theirs'], {'lines': ['h', 't1', 't2'], 'conflicts': 1}), ('union keeps both in order', [['<<<<<<< ours', 'x', 'y', '=======', 'y', 'z2', '>>>>>>> theirs'], 'union'], {'lines': ['x', 'y', 'y', 'z2'], 'conflicts': 1}), ('base strategy', [['<<<<<<<', 'o', '|||||||', 'b2', '=======', 't', '>>>>>>>'], 'base'], {'lines': ['b2'], 'conflicts': 1}), ('long marker-like content lines', [['<<<<<<<<<< banner', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs', '>>>>>>>>'], 'ours'], {'lines': ['<<<<<<<<<< banner', 'a', '>>>>>>>>'], 'conflicts': 1}), ('marker prefix glued to text', [['<<<<<<<x', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs'], 'theirs'], {'lines': ['<<<<<<<x', 'b'], 'conflicts': 1}), ('setext underline inside ours', [['<<<<<<< ours', 'Title', '========', '=======', 'T', '>>>>>>> theirs'], 'ours'], {'lines': ['Title', '========'], 'conflicts': 1}), ('indented separator-like line', [['<<<<<<< ours', '  =======', 'k', '=======', 't', '>>>>>>> theirs'], 'union'], {'lines': ['  =======', 'k', 't'], 'conflicts': 1}), ('unterminated conflict is an error', [['a', '<<<<<<< ours', 'o', '=======', 't'], 'ours'], {'error': 'unterminated conflict', 'conflicts': 0}), ('two conflicts', [['<<<<<<< ours', '1', '=======', '2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs'], 'theirs'], {'lines': ['2', 'mid', '4', '4'], 'conflicts': 3})],
    3: [('take ours', [['h', '<<<<<<< ours', 'o1', '=======', 't1', '>>>>>>> theirs', 'z'], 'ours'], {'lines': ['h', 'o1', 'z'], 'conflicts': 1}), ('take theirs with base section', [['h', '<<<<<<< ours', 'o1', '||||||| base', 'b1', '=======', 't1', 't2', '>>>>>>> theirs'], 'theirs'], {'lines': ['h', 't1', 't2'], 'conflicts': 1}), ('union keeps both in order', [['<<<<<<< ours', 'x', 'y', '=======', 'y', 'z3', '>>>>>>> theirs'], 'union'], {'lines': ['x', 'y', 'y', 'z3'], 'conflicts': 1}), ('base strategy', [['<<<<<<<', 'o', '|||||||', 'b3', '=======', 't', '>>>>>>>'], 'base'], {'lines': ['b3'], 'conflicts': 1}), ('long marker-like content lines', [['<<<<<<<<<< banner', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs', '>>>>>>>>'], 'ours'], {'lines': ['<<<<<<<<<< banner', 'a', '>>>>>>>>'], 'conflicts': 1}), ('marker prefix glued to text', [['<<<<<<<x', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs'], 'theirs'], {'lines': ['<<<<<<<x', 'b'], 'conflicts': 1}), ('setext underline inside ours', [['<<<<<<< ours', 'Title', '========', '=======', 'T', '>>>>>>> theirs'], 'ours'], {'lines': ['Title', '========'], 'conflicts': 1}), ('indented separator-like line', [['<<<<<<< ours', '  =======', 'k', '=======', 't', '>>>>>>> theirs'], 'union'], {'lines': ['  =======', 'k', 't'], 'conflicts': 1}), ('unterminated conflict is an error', [['a', '<<<<<<< ours', 'o', '=======', 't'], 'ours'], {'error': 'unterminated conflict', 'conflicts': 0}), ('two conflicts', [['<<<<<<< ours', '1', '=======', '2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs'], 'theirs'], {'lines': ['2', 'mid', '4', '4', '4'], 'conflicts': 4})],
    4: [('take ours', [['h', '<<<<<<< ours', 'o1', '=======', 't1', '>>>>>>> theirs', 'z'], 'ours'], {'lines': ['h', 'o1', 'z'], 'conflicts': 1}), ('take theirs with base section', [['h', '<<<<<<< ours', 'o1', '||||||| base', 'b1', '=======', 't1', 't2', '>>>>>>> theirs'], 'theirs'], {'lines': ['h', 't1', 't2'], 'conflicts': 1}), ('union keeps both in order', [['<<<<<<< ours', 'x', 'y', '=======', 'y', 'z4', '>>>>>>> theirs'], 'union'], {'lines': ['x', 'y', 'y', 'z4'], 'conflicts': 1}), ('base strategy', [['<<<<<<<', 'o', '|||||||', 'b4', '=======', 't', '>>>>>>>'], 'base'], {'lines': ['b4'], 'conflicts': 1}), ('long marker-like content lines', [['<<<<<<<<<< banner', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs', '>>>>>>>>'], 'ours'], {'lines': ['<<<<<<<<<< banner', 'a', '>>>>>>>>'], 'conflicts': 1}), ('marker prefix glued to text', [['<<<<<<<x', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs'], 'theirs'], {'lines': ['<<<<<<<x', 'b'], 'conflicts': 1}), ('setext underline inside ours', [['<<<<<<< ours', 'Title', '========', '=======', 'T', '>>>>>>> theirs'], 'ours'], {'lines': ['Title', '========'], 'conflicts': 1}), ('indented separator-like line', [['<<<<<<< ours', '  =======', 'k', '=======', 't', '>>>>>>> theirs'], 'union'], {'lines': ['  =======', 'k', 't'], 'conflicts': 1}), ('unterminated conflict is an error', [['a', '<<<<<<< ours', 'o', '=======', 't'], 'ours'], {'error': 'unterminated conflict', 'conflicts': 0}), ('two conflicts', [['<<<<<<< ours', '1', '=======', '2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs'], 'theirs'], {'lines': ['2', 'mid', '4', '4', '4', '4'], 'conflicts': 5})],
    5: [('take ours', [['h', '<<<<<<< ours', 'o1', '=======', 't1', '>>>>>>> theirs', 'z'], 'ours'], {'lines': ['h', 'o1', 'z'], 'conflicts': 1}), ('take theirs with base section', [['h', '<<<<<<< ours', 'o1', '||||||| base', 'b1', '=======', 't1', 't2', '>>>>>>> theirs'], 'theirs'], {'lines': ['h', 't1', 't2'], 'conflicts': 1}), ('union keeps both in order', [['<<<<<<< ours', 'x', 'y', '=======', 'y', 'z5', '>>>>>>> theirs'], 'union'], {'lines': ['x', 'y', 'y', 'z5'], 'conflicts': 1}), ('base strategy', [['<<<<<<<', 'o', '|||||||', 'b5', '=======', 't', '>>>>>>>'], 'base'], {'lines': ['b5'], 'conflicts': 1}), ('long marker-like content lines', [['<<<<<<<<<< banner', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs', '>>>>>>>>'], 'ours'], {'lines': ['<<<<<<<<<< banner', 'a', '>>>>>>>>'], 'conflicts': 1}), ('marker prefix glued to text', [['<<<<<<<x', '<<<<<<< ours', 'a', '=======', 'b', '>>>>>>> theirs'], 'theirs'], {'lines': ['<<<<<<<x', 'b'], 'conflicts': 1}), ('setext underline inside ours', [['<<<<<<< ours', 'Title', '========', '=======', 'T', '>>>>>>> theirs'], 'ours'], {'lines': ['Title', '========'], 'conflicts': 1}), ('indented separator-like line', [['<<<<<<< ours', '  =======', 'k', '=======', 't', '>>>>>>> theirs'], 'union'], {'lines': ['  =======', 'k', 't'], 'conflicts': 1}), ('unterminated conflict is an error', [['a', '<<<<<<< ours', 'o', '=======', 't'], 'ours'], {'error': 'unterminated conflict', 'conflicts': 0}), ('two conflicts', [['<<<<<<< ours', '1', '=======', '2', '>>>>>>> theirs', 'mid', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs', '<<<<<<< ours', '3', '||||||| base', 'b', '=======', '4', '>>>>>>> theirs'], 'theirs'], {'lines': ['2', 'mid', '4', '4', '4', '4', '4'], 'conflicts': 6})],
}[N]
for label, args, expected in cases:
    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:
Content lines such as banners made of angle brackets are mistaken for conflict markers.

Scan lines for conflict blocks. A marker is exactly seven marker characters, alone or followed by a space and a label: "<" opens, "|" starts the optional base section, a line of exactly seven "=" separates, ">" closes. Each closed block is replaced by ours, theirs, ours followed by theirs (union, no dedupe) or base. An unclosed block yields {"error": "unterminated conflict"}. Return the lines and the number of conflicts.

split: open-access