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

N = 1
observations = []
def solve(x):
    src, width = x
    chunks = ['']
    ops = []
    i = 0
    while i < len(src):
        c = src[i]
        if src.startswith('\\-', i):
            ops.append('d')
            chunks.append('')
            i += 2
            continue
        if c == ' ':
            j = i
            while j < len(src) and src[j] == ' ':
                j += 1
            ops.append('s')
            chunks.append('')
            i = j
            continue
        if c == '|':
            ops.append('z')
            chunks.append('')
            i += 1
            continue
        chunks[-1] += ' ' if c == '~' else c
        i += 1
    line = chunks[0]
    lines = []
    for op, ch in zip(ops, chunks[1:]):
        sep = ' ' if op == 's' else ''
        if len(line + sep + ch) <= width:
            line = line + sep + ch
        else:
            lines.append(line)
            line = ch
    lines.append(line)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('regression: hyphen at taken discretionary', ['a com\\-po\\-si\\-tion word', 9], ['a composi-', 'tion word']), ('partial-repair probe', ['url|path word a b|c a Dr.~Knuth  url|path  word ~10~pt', 8], ['urlpath', 'word a b', 'c a', 'Dr. Knuth', 'urlpath', 'word', ' 10 pt']), ('partial-repair probe', ['word type x ink url|path set  ~a', 10], ['word type', 'x ink url', 'path set', ' a']), ('double space collapses', ['type  set', 20], ['type set']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['type word  Dr.~Knuth  b|c', 15], ['type word', 'Dr. Knuth bc']), ('control layout', ['ink  a ~a Dr.~Knuth Dr.~Knuth  type b|c', 12], ['ink a  a', 'Dr. Knuth', 'Dr. Knuth', 'type bc'])], [('regression: hyphen at taken discretionary', ['word hy\\-phen set word ~set Dr.~Knuth  word  word', 8], ['word hy-', 'phen set', 'word', ' set', 'Dr. Knuth', 'word', 'word']), ('regression: hyphen at taken discretionary', ['Dr.~Knuth Dr.~Knuth ~url|path a a  ~hy\\-phen  ~Dr.~Knuth  hy\\-phen com\\-po\\-si\\-tion', 14], ['Dr. Knuth', 'Dr. Knuth  url', 'path a a  hy-', 'phen', ' Dr. Knuth hy-', 'phen composi-', 'tion']), ('partial-repair probe', ['x com\\-po\\-si\\-tion b|c', 15], ['x composition b', 'c']), ('partial-repair probe', ['set ink ink word url|path Dr.~Knuth hy\\-phen word', 11], ['set ink ink', 'word url', 'path', 'Dr. Knuth', 'hyphen word']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('double space collapses', ['type  set', 20], ['type set']), ('control layout', ['Dr.~Knuth hy\\-phen a type ~a type', 7], ['Dr. Knuth', 'hyphen', 'a type', ' a type']), ('control layout', ['ink  a ~a Dr.~Knuth Dr.~Knuth  type b|c', 12], ['ink a  a', 'Dr. Knuth', 'Dr. Knuth', 'type bc'])], [('regression: hyphen at taken discretionary', ['set  url|path a com\\-po\\-si\\-tion x', 12], ['set urlpath', 'a composi-', 'tion x']), ('regression: hyphen at taken discretionary', ['10~pt ink hy\\-phen word', 12], ['10 pt ink hy-', 'phen word']), ('partial-repair probe', ['ink 10~pt ink b|c hy\\-phen ~set url|path word', 16], ['ink 10 pt ink bc', 'hyphen  set url', 'path word']), ('regression: hyphen at taken discretionary', ['ink  com\\-po\\-si\\-tion a  url|path a  ink url|path  url|path ~10~pt', 6], ['ink', 'compo-', 'sition', 'a url', 'path a', 'ink', 'url', 'path', 'url', 'path', ' 10 pt']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('control layout', ['a  Dr.~Knuth  Dr.~Knuth  ink', 11], ['a Dr. Knuth', 'Dr. Knuth', 'ink']), ('control layout', ['a  x a Dr.~Knuth ~type set hy\\-phen  ~type', 16], ['a x a Dr. Knuth', ' type set hyphen', ' type'])], [('regression: hyphen at taken discretionary', ['x  10~pt a ~Dr.~Knuth b|c  Dr.~Knuth ~hy\\-phen  set  com\\-po\\-si\\-tion', 16], ['x 10 pt a', ' Dr. Knuth bc', 'Dr. Knuth  hy-', 'phen set composi-', 'tion']), ('regression: hyphen at taken discretionary', ['x set  type com\\-po\\-si\\-tion a ~ink b|c hy\\-phen url|path', 13], ['x set type', 'composition a', ' ink bc hy-', 'phen urlpath']), ('partial-repair probe', ['10~pt b|c  ~Dr.~Knuth  b|c  set', 12], ['10 pt bc', ' Dr. Knuth b', 'c set']), ('partial-repair probe', ['Dr.~Knuth url|path b|c set url|path 10~pt', 10], ['Dr. Knuth', 'urlpath bc', 'set url', 'path 10 pt']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('double space collapses', ['type  set', 20], ['type set']), ('control layout', ['hy\\-phen a ~a ~b|c set ~ink', 8], ['hyphen a', ' a  bc', 'set  ink']), ('control layout', ['Dr.~Knuth set 10~pt ink ink', 10], ['Dr. Knuth', 'set 10 pt', 'ink ink'])], [('regression: hyphen at taken discretionary', ['hy\\-phen  ~com\\-po\\-si\\-tion 10~pt word  b|c', 15], ['hyphen  composi-', 'tion 10 pt word', 'bc']), ('regression: hyphen at taken discretionary', ['a Dr.~Knuth x ~set word type  ~com\\-po\\-si\\-tion', 6], ['a', 'Dr. Knuth', 'x  set', 'word', 'type', ' compo-', 'sition']), ('partial-repair probe', ['word set  a  x ~set ~set  b|c x', 6], ['word', 'set a', 'x  set', ' set b', 'c x']), ('regression: hyphen at taken discretionary', ['url|path ink  set Dr.~Knuth hy\\-phen set type x', 5], ['url', 'path', 'ink', 'set', 'Dr. Knuth', 'hy-', 'phen', 'set', 'type', 'x']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['ink com\\-po\\-si\\-tion Dr.~Knuth', 16], ['ink composition', 'Dr. Knuth']), ('control layout', ['a b|c x b|c', 15], ['a bc x bc'])]]
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(x):
    src, width = x
    chunks = ['']
    ops = []
    i = 0
    while i < len(src):
        c = src[i]
        if src.startswith('\\-', i):
            ops.append('d')
            chunks.append('')
            i += 2
            continue
        if c == ' ':
            j = i
            while j < len(src) and src[j] == ' ':
                j += 1
            ops.append('s')
            chunks.append('')
            i = j
            continue
        if c == '|':
            ops.append('z')
            chunks.append('')
            i += 1
            continue
        chunks[-1] += ' ' if c == '~' else c
        i += 1
    line = chunks[0]
    lines = []
    for op, ch in zip(ops, chunks[1:]):
        sep = ' ' if op == 's' else ''
        if len(line + sep + ch) <= width:
            line = line + sep + ch
        else:
            lines.append(line + ('-' if op != 's' else ''))
            line = ch
    lines.append(line)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('regression: hyphen at taken discretionary', ['a com\\-po\\-si\\-tion word', 9], ['a composi-', 'tion word']), ('partial-repair probe', ['url|path word a b|c a Dr.~Knuth  url|path  word ~10~pt', 8], ['urlpath', 'word a b', 'c a', 'Dr. Knuth', 'urlpath', 'word', ' 10 pt']), ('partial-repair probe', ['word type x ink url|path set  ~a', 10], ['word type', 'x ink url', 'path set', ' a']), ('double space collapses', ['type  set', 20], ['type set']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['type word  Dr.~Knuth  b|c', 15], ['type word', 'Dr. Knuth bc']), ('control layout', ['ink  a ~a Dr.~Knuth Dr.~Knuth  type b|c', 12], ['ink a  a', 'Dr. Knuth', 'Dr. Knuth', 'type bc'])], [('regression: hyphen at taken discretionary', ['word hy\\-phen set word ~set Dr.~Knuth  word  word', 8], ['word hy-', 'phen set', 'word', ' set', 'Dr. Knuth', 'word', 'word']), ('regression: hyphen at taken discretionary', ['Dr.~Knuth Dr.~Knuth ~url|path a a  ~hy\\-phen  ~Dr.~Knuth  hy\\-phen com\\-po\\-si\\-tion', 14], ['Dr. Knuth', 'Dr. Knuth  url', 'path a a  hy-', 'phen', ' Dr. Knuth hy-', 'phen composi-', 'tion']), ('partial-repair probe', ['x com\\-po\\-si\\-tion b|c', 15], ['x composition b', 'c']), ('partial-repair probe', ['set ink ink word url|path Dr.~Knuth hy\\-phen word', 11], ['set ink ink', 'word url', 'path', 'Dr. Knuth', 'hyphen word']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('double space collapses', ['type  set', 20], ['type set']), ('control layout', ['Dr.~Knuth hy\\-phen a type ~a type', 7], ['Dr. Knuth', 'hyphen', 'a type', ' a type']), ('control layout', ['ink  a ~a Dr.~Knuth Dr.~Knuth  type b|c', 12], ['ink a  a', 'Dr. Knuth', 'Dr. Knuth', 'type bc'])], [('regression: hyphen at taken discretionary', ['set  url|path a com\\-po\\-si\\-tion x', 12], ['set urlpath', 'a composi-', 'tion x']), ('regression: hyphen at taken discretionary', ['10~pt ink hy\\-phen word', 12], ['10 pt ink hy-', 'phen word']), ('partial-repair probe', ['ink 10~pt ink b|c hy\\-phen ~set url|path word', 16], ['ink 10 pt ink bc', 'hyphen  set url', 'path word']), ('regression: hyphen at taken discretionary', ['ink  com\\-po\\-si\\-tion a  url|path a  ink url|path  url|path ~10~pt', 6], ['ink', 'compo-', 'sition', 'a url', 'path a', 'ink', 'url', 'path', 'url', 'path', ' 10 pt']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('control layout', ['a  Dr.~Knuth  Dr.~Knuth  ink', 11], ['a Dr. Knuth', 'Dr. Knuth', 'ink']), ('control layout', ['a  x a Dr.~Knuth ~type set hy\\-phen  ~type', 16], ['a x a Dr. Knuth', ' type set hyphen', ' type'])], [('regression: hyphen at taken discretionary', ['x  10~pt a ~Dr.~Knuth b|c  Dr.~Knuth ~hy\\-phen  set  com\\-po\\-si\\-tion', 16], ['x 10 pt a', ' Dr. Knuth bc', 'Dr. Knuth  hy-', 'phen set composi-', 'tion']), ('regression: hyphen at taken discretionary', ['x set  type com\\-po\\-si\\-tion a ~ink b|c hy\\-phen url|path', 13], ['x set type', 'composition a', ' ink bc hy-', 'phen urlpath']), ('partial-repair probe', ['10~pt b|c  ~Dr.~Knuth  b|c  set', 12], ['10 pt bc', ' Dr. Knuth b', 'c set']), ('partial-repair probe', ['Dr.~Knuth url|path b|c set url|path 10~pt', 10], ['Dr. Knuth', 'urlpath bc', 'set url', 'path 10 pt']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('double space collapses', ['type  set', 20], ['type set']), ('control layout', ['hy\\-phen a ~a ~b|c set ~ink', 8], ['hyphen a', ' a  bc', 'set  ink']), ('control layout', ['Dr.~Knuth set 10~pt ink ink', 10], ['Dr. Knuth', 'set 10 pt', 'ink ink'])], [('regression: hyphen at taken discretionary', ['hy\\-phen  ~com\\-po\\-si\\-tion 10~pt word  b|c', 15], ['hyphen  composi-', 'tion 10 pt word', 'bc']), ('regression: hyphen at taken discretionary', ['a Dr.~Knuth x ~set word type  ~com\\-po\\-si\\-tion', 6], ['a', 'Dr. Knuth', 'x  set', 'word', 'type', ' compo-', 'sition']), ('partial-repair probe', ['word set  a  x ~set ~set  b|c x', 6], ['word', 'set a', 'x  set', ' set b', 'c x']), ('regression: hyphen at taken discretionary', ['url|path ink  set Dr.~Knuth hy\\-phen set type x', 5], ['url', 'path', 'ink', 'set', 'Dr. Knuth', 'hy-', 'phen', 'set', 'type', 'x']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['ink com\\-po\\-si\\-tion Dr.~Knuth', 16], ['ink composition', 'Dr. Knuth']), ('control layout', ['a b|c x b|c', 15], ['a bc x bc'])]]
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:
Words broken at a discretionary lose their hyphen.

Input [source, width]. Runs of spaces are one breakable space; "~" is an unbreakable space rendered " "; "\-" is a discretionary hyphen: invisible unless the line breaks there, then rendered "-"; "|" is an invisible break opportunity. Greedy: append the next chunk if the line (without a potential hyphen) fits, else break at that opportunity. Return rendered lines.

split: open-access


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

N = 1
observations = []
def solve(x):
    src, width = x
    chunks = ['']
    ops = []
    i = 0
    while i < len(src):
        c = src[i]
        if src.startswith('\\-', i):
            ops.append('d')
            chunks.append('')
            i += 2
            continue
        if c == ' ':
            j = i
            while j < len(src) and src[j] == ' ':
                j += 1
            ops.append('s')
            chunks.append('')
            i = j
            continue
        if c == '|':
            ops.append('z')
            chunks.append('')
            i += 1
            continue
        chunks[-1] += ' ' if c == '~' else c
        i += 1
    line = chunks[0]
    lines = []
    for op, ch in zip(ops, chunks[1:]):
        sep = ' ' if op in 'sz' else ''
        if len(line + sep + ch) <= width:
            line = line + sep + ch
        else:
            lines.append(line + ('-' if op == 'd' else ''))
            line = ch
    lines.append(line)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('regression: invisible break rendering', ['url|path url|path hy\\-phen com\\-po\\-si\\-tion word b|c a word b|c', 11], ['urlpath url', 'path hyphen', 'composition', 'word bc a', 'word bc']), ('partial-repair probe', ['com\\-po\\-si\\-tion word word hy\\-phen url|path', 6], ['compo-', 'sition', 'word', 'word', 'hyphen', 'url', 'path']), ('partial-repair probe', ['word set  a  x ~set ~set  b|c x', 6], ['word', 'set a', 'x  set', ' set b', 'c x']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['ink a a ~hy\\-phen  x com\\-po\\-si\\-tion x', 6], ['ink a', 'a  hy-', 'phen x', 'compo-', 'sition', 'x']), ('control layout', ['hy\\-phen ink  type word', 11], ['hyphen ink', 'type word'])], [('regression: invisible break rendering', ['url|path ~com\\-po\\-si\\-tion b|c b|c url|path word Dr.~Knuth url|path  ~hy\\-phen', 12], ['urlpath  com-', 'position bc', 'bc urlpath', 'word', 'Dr. Knuth', 'urlpath  hy-', 'phen']), ('regression: invisible break rendering', ['com\\-po\\-si\\-tion ~b|c ~x ink  url|path a', 12], ['composition', ' bc  x ink', 'urlpath a']), ('regression: invisible break rendering', ['b|c a a set a ~com\\-po\\-si\\-tion hy\\-phen  ink ~url|path', 11], ['bc a a set', 'a  composi-', 'tion hyphen', 'ink  url', 'path']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['com\\-po\\-si\\-tion 10~pt word set com\\-po\\-si\\-tion', 6], ['compo-', 'sition', '10 pt', 'word', 'set', 'compo-', 'sition']), ('control layout', ['set com\\-po\\-si\\-tion  10~pt word', 15], ['set composition', '10 pt word'])], [('regression: invisible break rendering', ['10~pt url|path  ink set com\\-po\\-si\\-tion  set  word com\\-po\\-si\\-tion', 7], ['10 pt', 'urlpath', 'ink set', 'composi-', 'tion', 'set', 'word', 'composi-', 'tion']), ('regression: invisible break rendering', ['com\\-po\\-si\\-tion ~url|path x  ~set  ~com\\-po\\-si\\-tion  com\\-po\\-si\\-tion x ~Dr.~Knuth', 8], ['composi-', 'tion', ' urlpath', 'x  set', ' composi-', 'tion com-', 'position', 'x', ' Dr. Knuth']), ('partial-repair probe', ['x ~url|path url|path ~type word', 5], ['x', ' url', 'path', 'url', 'path', ' type', 'word']), ('partial-repair probe', ['Dr.~Knuth  10~pt  word Dr.~Knuth  word x  b|c  url|path 10~pt', 8], ['Dr. Knuth', '10 pt', 'word', 'Dr. Knuth', 'word x b', 'c url', 'path', '10 pt']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('control layout', ['word  x hy\\-phen', 6], ['word x', 'hyphen']), ('control layout', ['10~pt  ~10~pt ~type ~Dr.~Knuth  Dr.~Knuth', 5], ['10 pt', ' 10 pt', ' type', ' Dr. Knuth', 'Dr. Knuth'])], [('regression: invisible break rendering', ['url|path ~ink  ~url|path  set b|c  hy\\-phen', 13], ['urlpath  ink', ' urlpath set', 'bc hyphen']), ('regression: invisible break rendering', ['b|c url|path ~set ink  type ~b|c b|c ~url|path ink', 9], ['bc url', 'path  set', 'ink type', ' bc bc', ' urlpath', 'ink']), ('partial-repair probe', ['url|path  url|path  hy\\-phen ink url|path hy\\-phen set b|c', 5], ['url', 'path', 'url', 'path', 'hy-', 'phen', 'ink', 'url', 'path', 'hy-', 'phen', 'set b', 'c']), ('partial-repair probe', ['ink  type 10~pt  Dr.~Knuth  set a url|path', 10], ['ink type', '10 pt', 'Dr. Knuth', 'set a url', 'path']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('double space collapses', ['type  set', 20], ['type set']), ('control layout', ['hy\\-phen type ~word  hy\\-phen ~x', 14], ['hyphen type', ' word hyphen', ' x']), ('control layout', ['Dr.~Knuth a Dr.~Knuth set com\\-po\\-si\\-tion Dr.~Knuth', 12], ['Dr. Knuth a', 'Dr. Knuth', 'set composi-', 'tion', 'Dr. Knuth'])], [('regression: invisible break rendering', ['b|c b|c ~set ~com\\-po\\-si\\-tion', 12], ['bc bc  set', ' composition']), ('regression: invisible break rendering', ['10~pt  b|c a', 16], ['10 pt bc a']), ('regression: invisible break rendering', ['Dr.~Knuth  url|path a Dr.~Knuth b|c b|c com\\-po\\-si\\-tion', 15], ['Dr. Knuth url', 'path a', 'Dr. Knuth bc bc', 'composition']), ('partial-repair probe', ['x url|path word ~a', 8], ['x url', 'path', 'word  a']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['Dr.~Knuth set  com\\-po\\-si\\-tion', 5], ['Dr. Knuth', 'set', 'compo-', 'si-', 'tion']), ('control layout', ['word hy\\-phen x  ~type Dr.~Knuth ~type', 10], ['word hy-', 'phen x', ' type', 'Dr. Knuth', ' type'])]]
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(x):
    src, width = x
    chunks = ['']
    ops = []
    i = 0
    while i < len(src):
        c = src[i]
        if src.startswith('\\-', i):
            ops.append('d')
            chunks.append('')
            i += 2
            continue
        if c == ' ':
            j = i
            while j < len(src) and src[j] == ' ':
                j += 1
            ops.append('s')
            chunks.append('')
            i = j
            continue
        if c == '|':
            i += 1
            continue
        chunks[-1] += ' ' if c == '~' else c
        i += 1
    line = chunks[0]
    lines = []
    for op, ch in zip(ops, chunks[1:]):
        sep = ' ' if op == 's' else ''
        if len(line + sep + ch) <= width:
            line = line + sep + ch
        else:
            lines.append(line + ('-' if op == 'd' else ''))
            line = ch
    lines.append(line)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('regression: invisible break rendering', ['url|path url|path hy\\-phen com\\-po\\-si\\-tion word b|c a word b|c', 11], ['urlpath url', 'path hyphen', 'composition', 'word bc a', 'word bc']), ('partial-repair probe', ['com\\-po\\-si\\-tion word word hy\\-phen url|path', 6], ['compo-', 'sition', 'word', 'word', 'hyphen', 'url', 'path']), ('partial-repair probe', ['word set  a  x ~set ~set  b|c x', 6], ['word', 'set a', 'x  set', ' set b', 'c x']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['ink a a ~hy\\-phen  x com\\-po\\-si\\-tion x', 6], ['ink a', 'a  hy-', 'phen x', 'compo-', 'sition', 'x']), ('control layout', ['hy\\-phen ink  type word', 11], ['hyphen ink', 'type word'])], [('regression: invisible break rendering', ['url|path ~com\\-po\\-si\\-tion b|c b|c url|path word Dr.~Knuth url|path  ~hy\\-phen', 12], ['urlpath  com-', 'position bc', 'bc urlpath', 'word', 'Dr. Knuth', 'urlpath  hy-', 'phen']), ('regression: invisible break rendering', ['com\\-po\\-si\\-tion ~b|c ~x ink  url|path a', 12], ['composition', ' bc  x ink', 'urlpath a']), ('regression: invisible break rendering', ['b|c a a set a ~com\\-po\\-si\\-tion hy\\-phen  ink ~url|path', 11], ['bc a a set', 'a  composi-', 'tion hyphen', 'ink  url', 'path']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['com\\-po\\-si\\-tion 10~pt word set com\\-po\\-si\\-tion', 6], ['compo-', 'sition', '10 pt', 'word', 'set', 'compo-', 'sition']), ('control layout', ['set com\\-po\\-si\\-tion  10~pt word', 15], ['set composition', '10 pt word'])], [('regression: invisible break rendering', ['10~pt url|path  ink set com\\-po\\-si\\-tion  set  word com\\-po\\-si\\-tion', 7], ['10 pt', 'urlpath', 'ink set', 'composi-', 'tion', 'set', 'word', 'composi-', 'tion']), ('regression: invisible break rendering', ['com\\-po\\-si\\-tion ~url|path x  ~set  ~com\\-po\\-si\\-tion  com\\-po\\-si\\-tion x ~Dr.~Knuth', 8], ['composi-', 'tion', ' urlpath', 'x  set', ' composi-', 'tion com-', 'position', 'x', ' Dr. Knuth']), ('partial-repair probe', ['x ~url|path url|path ~type word', 5], ['x', ' url', 'path', 'url', 'path', ' type', 'word']), ('partial-repair probe', ['Dr.~Knuth  10~pt  word Dr.~Knuth  word x  b|c  url|path 10~pt', 8], ['Dr. Knuth', '10 pt', 'word', 'Dr. Knuth', 'word x b', 'c url', 'path', '10 pt']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('control layout', ['word  x hy\\-phen', 6], ['word x', 'hyphen']), ('control layout', ['10~pt  ~10~pt ~type ~Dr.~Knuth  Dr.~Knuth', 5], ['10 pt', ' 10 pt', ' type', ' Dr. Knuth', 'Dr. Knuth'])], [('regression: invisible break rendering', ['url|path ~ink  ~url|path  set b|c  hy\\-phen', 13], ['urlpath  ink', ' urlpath set', 'bc hyphen']), ('regression: invisible break rendering', ['b|c url|path ~set ink  type ~b|c b|c ~url|path ink', 9], ['bc url', 'path  set', 'ink type', ' bc bc', ' urlpath', 'ink']), ('partial-repair probe', ['url|path  url|path  hy\\-phen ink url|path hy\\-phen set b|c', 5], ['url', 'path', 'url', 'path', 'hy-', 'phen', 'ink', 'url', 'path', 'hy-', 'phen', 'set b', 'c']), ('partial-repair probe', ['ink  type 10~pt  Dr.~Knuth  set a url|path', 10], ['ink type', '10 pt', 'Dr. Knuth', 'set a url', 'path']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('double space collapses', ['type  set', 20], ['type set']), ('control layout', ['hy\\-phen type ~word  hy\\-phen ~x', 14], ['hyphen type', ' word hyphen', ' x']), ('control layout', ['Dr.~Knuth a Dr.~Knuth set com\\-po\\-si\\-tion Dr.~Knuth', 12], ['Dr. Knuth a', 'Dr. Knuth', 'set composi-', 'tion', 'Dr. Knuth'])], [('regression: invisible break rendering', ['b|c b|c ~set ~com\\-po\\-si\\-tion', 12], ['bc bc  set', ' composition']), ('regression: invisible break rendering', ['10~pt  b|c a', 16], ['10 pt bc a']), ('regression: invisible break rendering', ['Dr.~Knuth  url|path a Dr.~Knuth b|c b|c com\\-po\\-si\\-tion', 15], ['Dr. Knuth url', 'path a', 'Dr. Knuth bc bc', 'composition']), ('partial-repair probe', ['x url|path word ~a', 8], ['x url', 'path', 'word  a']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['Dr.~Knuth set  com\\-po\\-si\\-tion', 5], ['Dr. Knuth', 'set', 'compo-', 'si-', 'tion']), ('control layout', ['word hy\\-phen x  ~type Dr.~Knuth ~type', 10], ['word hy-', 'phen x', ' type', 'Dr. Knuth', ' type'])]]
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:
Zero-width break opportunities show up as spaces.

Input [source, width]. Runs of spaces are one breakable space; "~" is an unbreakable space rendered " "; "\-" is a discretionary hyphen: invisible unless the line breaks there, then rendered "-"; "|" is an invisible break opportunity. Greedy: append the next chunk if the line (without a potential hyphen) fits, else break at that opportunity. Return rendered lines.

split: open-access


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

N = 1
observations = []
def solve(x):
    src, width = x
    chunks = ['']
    ops = []
    i = 0
    while i < len(src):
        c = src[i]
        if src.startswith('\\-', i):
            ops.append('d')
            chunks.append('')
            i += 2
            continue
        if c == ' ':
            j = i
            j += 1
            ops.append('s')
            chunks.append('')
            i = j
            continue
        if c == '|':
            ops.append('z')
            chunks.append('')
            i += 1
            continue
        chunks[-1] += ' ' if c == '~' else c
        i += 1
    line = chunks[0]
    lines = []
    for op, ch in zip(ops, chunks[1:]):
        sep = ' ' if op == 's' else ''
        if len(line + sep + ch) <= width:
            line = line + sep + ch
        else:
            lines.append(line + ('-' if op == 'd' else ''))
            line = ch
    lines.append(line)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('double space collapses', ['type  set', 20], ['type set']), ('regression: space run collapsing', ['url|path  ~a a  hy\\-phen', 12], ['urlpath  a a', 'hyphen']), ('partial-repair probe', ['url|path x Dr.~Knuth ~10~pt', 12], ['urlpath x', 'Dr. Knuth', ' 10 pt']), ('regression: space run collapsing', ['hy\\-phen x hy\\-phen  x a ~com\\-po\\-si\\-tion  hy\\-phen  word ~type', 5], ['hy-', 'phen', 'x hy-', 'phen', 'x a', ' com-', 'posi-', 'tion', 'hy-', 'phen', 'word', ' type']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('control layout', ['b|c ink hy\\-phen url|path type com\\-po\\-si\\-tion', 8], ['bc ink', 'hyphen', 'urlpath', 'type com-', 'position']), ('control layout', ['set type type', 5], ['set', 'type', 'type'])], [('regression: space run collapsing', ['type ~x ~x  ~word url|path  set  hy\\-phen', 15], ['type  x  x', ' word urlpath', 'set hyphen']), ('regression: space run collapsing', ['hy\\-phen ~type set ~com\\-po\\-si\\-tion set  type  com\\-po\\-si\\-tion word set', 15], ['hyphen  type', 'set  composi-', 'tion set type', 'composition', 'word set']), ('regression: space run collapsing', ['a  ~Dr.~Knuth ~ink x url|path b|c', 8], ['a', ' Dr. Knuth', ' ink x', 'urlpath', 'bc']), ('partial-repair probe', ['set type ~type url|path ~ink ~type', 10], ['set type', ' type url', 'path  ink', ' type']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('double space collapses', ['type  set', 20], ['type set']), ('control layout', ['hy\\-phen Dr.~Knuth a ink b|c', 10], ['hyphen', 'Dr. Knuth', 'a ink bc']), ('control layout', ['com\\-po\\-si\\-tion a x', 16], ['composition a x'])], [('regression: space run collapsing', ['hy\\-phen x hy\\-phen  x a ~com\\-po\\-si\\-tion  hy\\-phen  word ~type', 5], ['hy-', 'phen', 'x hy-', 'phen', 'x a', ' com-', 'posi-', 'tion', 'hy-', 'phen', 'word', ' type']), ('regression: space run collapsing', ['word  b|c  com\\-po\\-si\\-tion ~hy\\-phen x word type', 10], ['word bc', 'composi-', 'tion  hy-', 'phen x', 'word type']), ('regression: space run collapsing', ['hy\\-phen com\\-po\\-si\\-tion ~url|path  Dr.~Knuth', 12], ['hyphen compo-', 'sition  url', 'path', 'Dr. Knuth']), ('regression: space run collapsing', ['x  10~pt a ~Dr.~Knuth b|c  Dr.~Knuth ~hy\\-phen  set  com\\-po\\-si\\-tion', 16], ['x 10 pt a', ' Dr. Knuth bc', 'Dr. Knuth  hy-', 'phen set composi-', 'tion']), ('double space collapses', ['type  set', 20], ['type set']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('control layout', ['10~pt word b|c', 11], ['10 pt word', 'bc']), ('control layout', ['b|c type type', 6], ['bc', 'type', 'type'])], [('regression: space run collapsing', ['b|c url|path  Dr.~Knuth', 5], ['bc', 'url', 'path', 'Dr. Knuth']), ('regression: space run collapsing', ['word a ~url|path com\\-po\\-si\\-tion  com\\-po\\-si\\-tion', 7], ['word a', ' url', 'path', 'composi-', 'tion', 'composi-', 'tion']), ('regression: space run collapsing', ['com\\-po\\-si\\-tion ~word ink  Dr.~Knuth type word a', 9], ['composi-', 'tion', ' word ink', 'Dr. Knuth', 'type word', 'a']), ('regression: space run collapsing', ['set  ~b|c 10~pt  b|c', 14], ['set  bc 10 pt', 'bc']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['b|c ink type', 14], ['bc ink type']), ('control layout', ['word a hy\\-phen b|c ink a hy\\-phen', 5], ['word', 'a hy-', 'phen', 'bc', 'ink a', 'hy-', 'phen'])], [('regression: space run collapsing', ['b|c a Dr.~Knuth  a  Dr.~Knuth ~word', 6], ['bc a', 'Dr. Knuth', 'a', 'Dr. Knuth', ' word']), ('regression: space run collapsing', ['b|c set  ink com\\-po\\-si\\-tion a', 9], ['bc set', 'ink compo-', 'sition a']), ('partial-repair probe', ['word ~Dr.~Knuth 10~pt set', 16], ['word  Dr. Knuth', '10 pt set']), ('regression: space run collapsing', ['type a hy\\-phen com\\-po\\-si\\-tion ~set set ink  type', 8], ['type a', 'hyphen', 'composi-', 'tion', ' set set', 'ink type']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('control layout', ['hy\\-phen word word com\\-po\\-si\\-tion b|c b|c', 11], ['hyphen word', 'word compo-', 'sition bc b', 'c']), ('control layout', ['hy\\-phen 10~pt hy\\-phen x', 9], ['hyphen', '10 pt hy-', 'phen x'])]]
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(x):
    src, width = x
    chunks = ['']
    ops = []
    i = 0
    while i < len(src):
        c = src[i]
        if src.startswith('\\-', i):
            ops.append('d')
            chunks.append('')
            i += 2
            continue
        if c == ' ':
            j = i
            while j < len(src) and src[j] in ' ~':
                j += 1
            ops.append('s')
            chunks.append('')
            i = j
            continue
        if c == '|':
            ops.append('z')
            chunks.append('')
            i += 1
            continue
        chunks[-1] += ' ' if c == '~' else c
        i += 1
    line = chunks[0]
    lines = []
    for op, ch in zip(ops, chunks[1:]):
        sep = ' ' if op == 's' else ''
        if len(line + sep + ch) <= width:
            line = line + sep + ch
        else:
            lines.append(line + ('-' if op == 'd' else ''))
            line = ch
    lines.append(line)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('double space collapses', ['type  set', 20], ['type set']), ('regression: space run collapsing', ['url|path  ~a a  hy\\-phen', 12], ['urlpath  a a', 'hyphen']), ('partial-repair probe', ['url|path x Dr.~Knuth ~10~pt', 12], ['urlpath x', 'Dr. Knuth', ' 10 pt']), ('regression: space run collapsing', ['hy\\-phen x hy\\-phen  x a ~com\\-po\\-si\\-tion  hy\\-phen  word ~type', 5], ['hy-', 'phen', 'x hy-', 'phen', 'x a', ' com-', 'posi-', 'tion', 'hy-', 'phen', 'word', ' type']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('control layout', ['b|c ink hy\\-phen url|path type com\\-po\\-si\\-tion', 8], ['bc ink', 'hyphen', 'urlpath', 'type com-', 'position']), ('control layout', ['set type type', 5], ['set', 'type', 'type'])], [('regression: space run collapsing', ['type ~x ~x  ~word url|path  set  hy\\-phen', 15], ['type  x  x', ' word urlpath', 'set hyphen']), ('regression: space run collapsing', ['hy\\-phen ~type set ~com\\-po\\-si\\-tion set  type  com\\-po\\-si\\-tion word set', 15], ['hyphen  type', 'set  composi-', 'tion set type', 'composition', 'word set']), ('regression: space run collapsing', ['a  ~Dr.~Knuth ~ink x url|path b|c', 8], ['a', ' Dr. Knuth', ' ink x', 'urlpath', 'bc']), ('partial-repair probe', ['set type ~type url|path ~ink ~type', 10], ['set type', ' type url', 'path  ink', ' type']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('double space collapses', ['type  set', 20], ['type set']), ('control layout', ['hy\\-phen Dr.~Knuth a ink b|c', 10], ['hyphen', 'Dr. Knuth', 'a ink bc']), ('control layout', ['com\\-po\\-si\\-tion a x', 16], ['composition a x'])], [('regression: space run collapsing', ['hy\\-phen x hy\\-phen  x a ~com\\-po\\-si\\-tion  hy\\-phen  word ~type', 5], ['hy-', 'phen', 'x hy-', 'phen', 'x a', ' com-', 'posi-', 'tion', 'hy-', 'phen', 'word', ' type']), ('regression: space run collapsing', ['word  b|c  com\\-po\\-si\\-tion ~hy\\-phen x word type', 10], ['word bc', 'composi-', 'tion  hy-', 'phen x', 'word type']), ('regression: space run collapsing', ['hy\\-phen com\\-po\\-si\\-tion ~url|path  Dr.~Knuth', 12], ['hyphen compo-', 'sition  url', 'path', 'Dr. Knuth']), ('regression: space run collapsing', ['x  10~pt a ~Dr.~Knuth b|c  Dr.~Knuth ~hy\\-phen  set  com\\-po\\-si\\-tion', 16], ['x 10 pt a', ' Dr. Knuth bc', 'Dr. Knuth  hy-', 'phen set composi-', 'tion']), ('double space collapses', ['type  set', 20], ['type set']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('control layout', ['10~pt word b|c', 11], ['10 pt word', 'bc']), ('control layout', ['b|c type type', 6], ['bc', 'type', 'type'])], [('regression: space run collapsing', ['b|c url|path  Dr.~Knuth', 5], ['bc', 'url', 'path', 'Dr. Knuth']), ('regression: space run collapsing', ['word a ~url|path com\\-po\\-si\\-tion  com\\-po\\-si\\-tion', 7], ['word a', ' url', 'path', 'composi-', 'tion', 'composi-', 'tion']), ('regression: space run collapsing', ['com\\-po\\-si\\-tion ~word ink  Dr.~Knuth type word a', 9], ['composi-', 'tion', ' word ink', 'Dr. Knuth', 'type word', 'a']), ('regression: space run collapsing', ['set  ~b|c 10~pt  b|c', 14], ['set  bc 10 pt', 'bc']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['b|c ink type', 14], ['bc ink type']), ('control layout', ['word a hy\\-phen b|c ink a hy\\-phen', 5], ['word', 'a hy-', 'phen', 'bc', 'ink a', 'hy-', 'phen'])], [('regression: space run collapsing', ['b|c a Dr.~Knuth  a  Dr.~Knuth ~word', 6], ['bc a', 'Dr. Knuth', 'a', 'Dr. Knuth', ' word']), ('regression: space run collapsing', ['b|c set  ink com\\-po\\-si\\-tion a', 9], ['bc set', 'ink compo-', 'sition a']), ('partial-repair probe', ['word ~Dr.~Knuth 10~pt set', 16], ['word  Dr. Knuth', '10 pt set']), ('regression: space run collapsing', ['type a hy\\-phen com\\-po\\-si\\-tion ~set set ink  type', 8], ['type a', 'hyphen', 'composi-', 'tion', ' set set', 'ink type']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('control layout', ['hy\\-phen word word com\\-po\\-si\\-tion b|c b|c', 11], ['hyphen word', 'word compo-', 'sition bc b', 'c']), ('control layout', ['hy\\-phen 10~pt hy\\-phen x', 9], ['hyphen', '10 pt hy-', 'phen x'])]]
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:
Double spaces in the source produce double spaces or empty chunks in the output.

Input [source, width]. Runs of spaces are one breakable space; "~" is an unbreakable space rendered " "; "\-" is a discretionary hyphen: invisible unless the line breaks there, then rendered "-"; "|" is an invisible break opportunity. Greedy: append the next chunk if the line (without a potential hyphen) fits, else break at that opportunity. Return rendered lines.

split: open-access


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

N = 1
observations = []
def solve(x):
    src, width = x
    chunks = ['']
    ops = []
    i = 0
    while i < len(src):
        c = src[i]
        if src.startswith('\\-', i):
            ops.append('d')
            chunks.append('')
            i += 2
            continue
        if c == ' ':
            j = i
            while j < len(src) and src[j] == ' ':
                j += 1
            ops.append('s')
            chunks.append('')
            i = j
            continue
        if c == '|':
            ops.append('z')
            chunks.append('')
            i += 1
            continue
        chunks[-1] += ' ' if c == '~' else c
        i += 1
    line = chunks[0]
    lines = []
    for op, ch in zip(ops, chunks[1:]):
        sep = ' ' if op == 's' else ''
        if len(line + sep + ch) < width:
            line = line + sep + ch
        else:
            lines.append(line + ('-' if op == 'd' else ''))
            line = ch
    lines.append(line)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('regression: chunk fit comparison', ['set ~com\\-po\\-si\\-tion a word  hy\\-phen Dr.~Knuth b|c', 16], ['set  composition', 'a word hyphen', 'Dr. Knuth bc']), ('partial-repair probe', ['hy\\-phen x hy\\-phen  x a ~com\\-po\\-si\\-tion  hy\\-phen  word ~type', 5], ['hy-', 'phen', 'x hy-', 'phen', 'x a', ' com-', 'posi-', 'tion', 'hy-', 'phen', 'word', ' type']), ('partial-repair probe', ['x url|path hy\\-phen ~com\\-po\\-si\\-tion  b|c  ink  com\\-po\\-si\\-tion hy\\-phen word', 10], ['x urlpath', 'hyphen', ' composi-', 'tion bc', 'ink compo-', 'sition hy-', 'phen word']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['10~pt ink 10~pt  Dr.~Knuth', 5], ['10 pt', 'ink', '10 pt', 'Dr. Knuth']), ('control layout', ['word b|c  Dr.~Knuth word 10~pt', 8], ['word bc', 'Dr. Knuth', 'word', '10 pt'])], [('regression: chunk fit comparison', ['x  word word word', 6], ['x word', 'word', 'word']), ('regression: chunk fit comparison', ['com\\-po\\-si\\-tion  set a', 10], ['composi-', 'tion set a']), ('partial-repair probe', ['set type  a  a  type  Dr.~Knuth  Dr.~Knuth a', 9], ['set type', 'a a type', 'Dr. Knuth', 'Dr. Knuth', 'a']), ('regression: chunk fit comparison', ['hy\\-phen  a ~set url|path ~url|path', 16], ['hyphen a  set', 'urlpath  urlpath']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('control layout', ['ink  ~Dr.~Knuth  ~Dr.~Knuth', 11], ['ink', ' Dr. Knuth', ' Dr. Knuth']), ('control layout', ['x Dr.~Knuth word  hy\\-phen', 8], ['x', 'Dr. Knuth', 'word hy-', 'phen'])], [('regression: chunk fit comparison', ['ink  a b|c  url|path ~url|path 10~pt word', 5], ['ink a', 'bc', 'url', 'path', ' url', 'path', '10 pt', 'word']), ('regression: chunk fit comparison', ['type a ~hy\\-phen  word hy\\-phen ~b|c', 12], ['type a  hy-', 'phen word hy-', 'phen  bc']), ('regression: chunk fit comparison', ['set url|path ~x com\\-po\\-si\\-tion ~10~pt hy\\-phen url|path url|path hy\\-phen', 6], ['set', 'url', 'path', ' x com-', 'posi-', 'tion', ' 10 pt', 'hyphen', 'url', 'path', 'url', 'path', 'hyphen']), ('partial-repair probe', ['word ink  Dr.~Knuth x  b|c  type  a', 5], ['word', 'ink', 'Dr. Knuth', 'x bc', 'type', 'a']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['hy\\-phen 10~pt hy\\-phen x', 9], ['hyphen', '10 pt hy-', 'phen x']), ('control layout', ['url|path x Dr.~Knuth ~10~pt', 12], ['urlpath x', 'Dr. Knuth', ' 10 pt'])], [('regression: chunk fit comparison', ['10~pt b|c a set word  word  ~a Dr.~Knuth', 7], ['10 pt b', 'c a set', 'word', 'word  a', 'Dr. Knuth']), ('regression: chunk fit comparison', ['a word url|path  ~x Dr.~Knuth  hy\\-phen 10~pt', 6], ['a word', 'url', 'path', ' x', 'Dr. Knuth', 'hyphen', '10 pt']), ('regression: chunk fit comparison', ['hy\\-phen x  url|path ~x hy\\-phen Dr.~Knuth 10~pt ink ~ink', 6], ['hyphen', 'x url', 'path', ' x hy-', 'phen', 'Dr. Knuth', '10 pt', 'ink', ' ink']), ('partial-repair probe', ['word  Dr.~Knuth Dr.~Knuth Dr.~Knuth type set', 13], ['word', 'Dr. Knuth', 'Dr. Knuth', 'Dr. Knuth', 'type set']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('double space collapses', ['type  set', 20], ['type set']), ('control layout', ['ink b|c ink type 10~pt', 13], ['ink bc ink', 'type 10 pt']), ('control layout', ['ink type 10~pt Dr.~Knuth', 9], ['ink type', '10 pt', 'Dr. Knuth'])], [('regression: chunk fit comparison', ['set  ~url|path url|path word', 12], ['set  urlpath', 'urlpath word']), ('regression: chunk fit comparison', ['10~pt b|c a set word  word  ~a Dr.~Knuth', 7], ['10 pt b', 'c a set', 'word', 'word  a', 'Dr. Knuth']), ('regression: chunk fit comparison', ['b|c ink hy\\-phen url|path type com\\-po\\-si\\-tion', 8], ['bc ink', 'hyphen', 'urlpath', 'type com-', 'position']), ('regression: chunk fit comparison', ['com\\-po\\-si\\-tion  hy\\-phen com\\-po\\-si\\-tion ink  b|c a', 10], ['composi-', 'tion hy-', 'phen compo-', 'sition ink', 'bc a']), ('double space collapses', ['type  set', 20], ['type set']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('control layout', ['Dr.~Knuth type  b|c ink ink url|path url|path hy\\-phen', 12], ['Dr. Knuth', 'type bc ink', 'ink urlpath', 'urlpath hy-', 'phen']), ('control layout', ['a ~a  ~type hy\\-phen type', 15], ['a  a  type hy-', 'phen type'])]]
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(x):
    src, width = x
    chunks = ['']
    ops = []
    i = 0
    while i < len(src):
        c = src[i]
        if src.startswith('\\-', i):
            ops.append('d')
            chunks.append('')
            i += 2
            continue
        if c == ' ':
            j = i
            while j < len(src) and src[j] == ' ':
                j += 1
            ops.append('s')
            chunks.append('')
            i = j
            continue
        if c == '|':
            ops.append('z')
            chunks.append('')
            i += 1
            continue
        chunks[-1] += ' ' if c == '~' else c
        i += 1
    line = chunks[0]
    lines = []
    for op, ch in zip(ops, chunks[1:]):
        sep = ' ' if op == 's' else ''
        if len(line + sep + ch) <= width + 1:
            line = line + sep + ch
        else:
            lines.append(line + ('-' if op == 'd' else ''))
            line = ch
    lines.append(line)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('regression: chunk fit comparison', ['set ~com\\-po\\-si\\-tion a word  hy\\-phen Dr.~Knuth b|c', 16], ['set  composition', 'a word hyphen', 'Dr. Knuth bc']), ('partial-repair probe', ['hy\\-phen x hy\\-phen  x a ~com\\-po\\-si\\-tion  hy\\-phen  word ~type', 5], ['hy-', 'phen', 'x hy-', 'phen', 'x a', ' com-', 'posi-', 'tion', 'hy-', 'phen', 'word', ' type']), ('partial-repair probe', ['x url|path hy\\-phen ~com\\-po\\-si\\-tion  b|c  ink  com\\-po\\-si\\-tion hy\\-phen word', 10], ['x urlpath', 'hyphen', ' composi-', 'tion bc', 'ink compo-', 'sition hy-', 'phen word']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['10~pt ink 10~pt  Dr.~Knuth', 5], ['10 pt', 'ink', '10 pt', 'Dr. Knuth']), ('control layout', ['word b|c  Dr.~Knuth word 10~pt', 8], ['word bc', 'Dr. Knuth', 'word', '10 pt'])], [('regression: chunk fit comparison', ['x  word word word', 6], ['x word', 'word', 'word']), ('regression: chunk fit comparison', ['com\\-po\\-si\\-tion  set a', 10], ['composi-', 'tion set a']), ('partial-repair probe', ['set type  a  a  type  Dr.~Knuth  Dr.~Knuth a', 9], ['set type', 'a a type', 'Dr. Knuth', 'Dr. Knuth', 'a']), ('regression: chunk fit comparison', ['hy\\-phen  a ~set url|path ~url|path', 16], ['hyphen a  set', 'urlpath  urlpath']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('control layout', ['ink  ~Dr.~Knuth  ~Dr.~Knuth', 11], ['ink', ' Dr. Knuth', ' Dr. Knuth']), ('control layout', ['x Dr.~Knuth word  hy\\-phen', 8], ['x', 'Dr. Knuth', 'word hy-', 'phen'])], [('regression: chunk fit comparison', ['ink  a b|c  url|path ~url|path 10~pt word', 5], ['ink a', 'bc', 'url', 'path', ' url', 'path', '10 pt', 'word']), ('regression: chunk fit comparison', ['type a ~hy\\-phen  word hy\\-phen ~b|c', 12], ['type a  hy-', 'phen word hy-', 'phen  bc']), ('regression: chunk fit comparison', ['set url|path ~x com\\-po\\-si\\-tion ~10~pt hy\\-phen url|path url|path hy\\-phen', 6], ['set', 'url', 'path', ' x com-', 'posi-', 'tion', ' 10 pt', 'hyphen', 'url', 'path', 'url', 'path', 'hyphen']), ('partial-repair probe', ['word ink  Dr.~Knuth x  b|c  type  a', 5], ['word', 'ink', 'Dr. Knuth', 'x bc', 'type', 'a']), ('invisible break', ['url|path|x', 5], ['url', 'pathx']), ('tie keeps title with name', ['see Dr.~Knuth now', 9], ['see', 'Dr. Knuth', 'now']), ('control layout', ['hy\\-phen 10~pt hy\\-phen x', 9], ['hyphen', '10 pt hy-', 'phen x']), ('control layout', ['url|path x Dr.~Knuth ~10~pt', 12], ['urlpath x', 'Dr. Knuth', ' 10 pt'])], [('regression: chunk fit comparison', ['10~pt b|c a set word  word  ~a Dr.~Knuth', 7], ['10 pt b', 'c a set', 'word', 'word  a', 'Dr. Knuth']), ('regression: chunk fit comparison', ['a word url|path  ~x Dr.~Knuth  hy\\-phen 10~pt', 6], ['a word', 'url', 'path', ' x', 'Dr. Knuth', 'hyphen', '10 pt']), ('regression: chunk fit comparison', ['hy\\-phen x  url|path ~x hy\\-phen Dr.~Knuth 10~pt ink ~ink', 6], ['hyphen', 'x url', 'path', ' x hy-', 'phen', 'Dr. Knuth', '10 pt', 'ink', ' ink']), ('partial-repair probe', ['word  Dr.~Knuth Dr.~Knuth Dr.~Knuth type set', 13], ['word', 'Dr. Knuth', 'Dr. Knuth', 'Dr. Knuth', 'type set']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('double space collapses', ['type  set', 20], ['type set']), ('control layout', ['ink b|c ink type 10~pt', 13], ['ink bc ink', 'type 10 pt']), ('control layout', ['ink type 10~pt Dr.~Knuth', 9], ['ink type', '10 pt', 'Dr. Knuth'])], [('regression: chunk fit comparison', ['set  ~url|path url|path word', 12], ['set  urlpath', 'urlpath word']), ('regression: chunk fit comparison', ['10~pt b|c a set word  word  ~a Dr.~Knuth', 7], ['10 pt b', 'c a set', 'word', 'word  a', 'Dr. Knuth']), ('regression: chunk fit comparison', ['b|c ink hy\\-phen url|path type com\\-po\\-si\\-tion', 8], ['bc ink', 'hyphen', 'urlpath', 'type com-', 'position']), ('regression: chunk fit comparison', ['com\\-po\\-si\\-tion  hy\\-phen com\\-po\\-si\\-tion ink  b|c a', 10], ['composi-', 'tion hy-', 'phen compo-', 'sition ink', 'bc a']), ('double space collapses', ['type  set', 20], ['type set']), ('discretionary taken', ['the com\\-po\\-si\\-tion', 9], ['the compo-', 'sition']), ('control layout', ['Dr.~Knuth type  b|c ink ink url|path url|path hy\\-phen', 12], ['Dr. Knuth', 'type bc ink', 'ink urlpath', 'urlpath hy-', 'phen']), ('control layout', ['a ~a  ~type hy\\-phen type', 15], ['a  a  type hy-', 'phen type'])]]
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:
Chunks that exactly fill the line are wrapped.

Input [source, width]. Runs of spaces are one breakable space; "~" is an unbreakable space rendered " "; "\-" is a discretionary hyphen: invisible unless the line breaks there, then rendered "-"; "|" is an invisible break opportunity. Greedy: append the next chunk if the line (without a potential hyphen) fits, else break at that opportunity. Return rendered lines.

split: open-access


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

N = 1
observations = []
def solve(x):
    words, width, zone, min_word, min_before, min_after, caps = x
    lines = []
    cur = ''
    k = 0
    queue = [list(w) for w in words]
    while k < len(queue):
        syl = queue[k]
        token = ''.join(syl)
        cand = token if not cur else cur + ' ' + token
        if len(cand) <= width:
            cur = cand
            k += 1
            continue
        slack = width - len(cur) - (1 if cur else 0)
        eligible = slack > zone and len(token) >= min_word and (caps or not token[0].isupper())
        cut = None
        if eligible:
            for c in range(len(syl) - 1, 0, -1):
                head = ''.join(syl[:c])
                tail = ''.join(syl[c:])
                if len(head) < min_before or len(tail) < min_after:
                    continue
                fit = len(head) + 1 if not cur else len(cur) + len(head) + 2
                if fit <= width:
                    cut = c
                    break
        if cut is not None:
            head = ''.join(syl[:cut]) + '-'
            lines.append(head if not cur else cur + ' ' + head)
            queue[k] = syl[cut:]
            cur = ''
        elif cur:
            lines.append(cur)
            cur = ''
        else:
            cur = token
            k += 1
    if cur:
        lines.append(cur)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('regression: zone boundary', [[['hy', 'phen', 'at', 'ed'], ['e', 'Book', 'ing'], ['on', 'ly'], ['hy', 'phen', 'at', 'ed'], ['e', 'Book', 'ing'], ['on', 'ly']], 11, 2, 6, 1, 2, False], ['hyphenated', 'eBooking', 'only hy-', 'phenated e-', 'Booking', 'only']), ('partial-repair probe', [[['in', 'ter', 'na', 'tion', 'al'], ['a'], ['on', 'ly'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy']], 9, 5, 5, 1, 2, False], ['interna-', 'tional a', 'only', 'interna-', 'tional', 'typogra-', 'phy']), ('partial-repair probe', [[['Mc', 'Don', 'ald'], ['of'], ['the'], ['Mc', 'Don', 'ald'], ['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['hy', 'phen', 'at', 'ed'], ['on', 'ly']], 8, 4, 4, 1, 2, False], ['McDonald', 'of the', 'McDonald', 'impor-', 'tant', 'eBooking', 'hyphen-', 'ated', 'only']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['rag', 'ged'], ['of'], ['the'], ['Ber', 'lin'], ['of'], ['Ber', 'lin'], ['set'], ['a'], ['rag', 'ged'], ['of']], 15, 1, 7, 3, 2, False], ['ragged of the', 'Berlin of', 'Berlin set a', 'ragged of']), ('control layout', [[['Ber', 'lin'], ['Ber', 'lin'], ['a'], ['in', 'ter', 'na', 'tion', 'al']], 11, 3, 5, 1, 2, False], ['Berlin', 'Berlin a', 'interna-', 'tional']), ('control layout', [[['Ber', 'lin'], ['e', 'Book', 'ing'], ['rag', 'ged'], ['hy', 'phen', 'at', 'ed']], 13, 3, 7, 3, 3, False], ['Berlin eBook-', 'ing ragged', 'hyphenated'])], [('regression: zone boundary', [[['ty', 'po', 'gra', 'phy'], ['hy', 'phen', 'at', 'ed'], ['of'], ['rag', 'ged'], ['hy', 'phen', 'at', 'ed'], ['e', 'Book', 'ing'], ['e', 'Book', 'ing'], ['im', 'por', 'tant'], ['the'], ['on', 'ly']], 16, 4, 5, 2, 3, False], ['typography hy-', 'phenated of rag-', 'ged hyphenated', 'eBooking eBook-', 'ing important', 'the only']), ('regression: zone boundary', [[['e', 'Book', 'ing'], ['in', 'ter', 'na', 'tion', 'al'], ['e', 'Book', 'ing'], ['Mc', 'Don', 'ald'], ['rag', 'ged'], ['of'], ['im', 'por', 'tant'], ['set'], ['in', 'ter', 'na', 'tion', 'al']], 14, 5, 7, 1, 3, False], ['eBooking in-', 'ternational', 'eBooking', 'McDonald', 'ragged of', 'important set', 'international']), ('partial-repair probe', [[['on', 'ly'], ['Mc', 'Don', 'ald'], ['rag', 'ged'], ['ty', 'po', 'gra', 'phy'], ['ty', 'po', 'gra', 'phy'], ['rag', 'ged'], ['Ber', 'lin'], ['im', 'por', 'tant'], ['a']], 10, 4, 4, 1, 2, False], ['only', 'McDonald', 'ragged', 'typography', 'typography', 'ragged', 'Berlin', 'important', 'a']), ('partial-repair probe', [[['rag', 'ged'], ['Mc', 'Don', 'ald'], ['on', 'ly'], ['ty', 'po', 'gra', 'phy']], 9, 5, 7, 1, 2, False], ['ragged', 'McDonald', 'only', 'typogra-', 'phy']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['of'], ['Ber', 'lin'], ['e', 'Book', 'ing'], ['im', 'por', 'tant'], ['of'], ['Ber', 'lin'], ['the']], 15, 3, 6, 1, 2, True], ['of Berlin e-', 'Booking impor-', 'tant of Berlin', 'the']), ('control layout', [[['ty', 'po', 'gra', 'phy'], ['set'], ['Mc', 'Don', 'ald'], ['the']], 15, 5, 4, 2, 3, True], ['typography set', 'McDonald the'])], [('regression: zone boundary', [[['im', 'por', 'tant'], ['Mc', 'Don', 'ald'], ['hy', 'phen', 'at', 'ed'], ['of'], ['hy', 'phen', 'at', 'ed'], ['rag', 'ged']], 12, 3, 5, 1, 2, True], ['important', 'McDonald hy-', 'phenated of', 'hyphenated', 'ragged']), ('regression: zone boundary', [[['Mc', 'Don', 'ald'], ['rag', 'ged'], ['e', 'Book', 'ing'], ['ty', 'po', 'gra', 'phy'], ['Ber', 'lin'], ['ty', 'po', 'gra', 'phy'], ['a'], ['rag', 'ged'], ['ty', 'po', 'gra', 'phy']], 11, 3, 6, 1, 2, False], ['McDonald', 'ragged e-', 'Booking ty-', 'pography', 'Berlin ty-', 'pography a', 'ragged ty-', 'pography']), ('partial-repair probe', [[['the'], ['in', 'ter', 'na', 'tion', 'al'], ['on', 'ly'], ['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy'], ['ty', 'po', 'gra', 'phy']], 14, 4, 4, 2, 2, False], ['the interna-', 'tional only', 'hyphenated', 'international', 'international', 'typography', 'typography']), ('partial-repair probe', [[['rag', 'ged'], ['Ber', 'lin'], ['on', 'ly'], ['hy', 'phen', 'at', 'ed'], ['set'], ['Ber', 'lin']], 12, 4, 4, 2, 3, True], ['ragged Ber-', 'lin only', 'hyphenated', 'set Berlin']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('control layout', [[['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['in', 'ter', 'na', 'tion', 'al'], ['Mc', 'Don', 'ald'], ['of'], ['on', 'ly'], ['e', 'Book', 'ing'], ['rag', 'ged']], 9, 5, 7, 2, 3, False], ['important', 'eBooking', 'interna-', 'tional', 'McDonald', 'of only', 'eBooking', 'ragged']), ('control layout', [[['e', 'Book', 'ing'], ['on', 'ly'], ['Ber', 'lin'], ['in', 'ter', 'na', 'tion', 'al'], ['Ber', 'lin'], ['hy', 'phen', 'at', 'ed'], ['e', 'Book', 'ing']], 14, 3, 6, 1, 2, True], ['eBooking only', 'Berlin inter-', 'national Ber-', 'lin hyphenated', 'eBooking'])], [('regression: zone boundary', [[['e', 'Book', 'ing'], ['rag', 'ged'], ['ty', 'po', 'gra', 'phy'], ['Mc', 'Don', 'ald'], ['Mc', 'Don', 'ald'], ['of'], ['a'], ['on', 'ly']], 15, 4, 4, 1, 3, True], ['eBooking ragged', 'typography Mc-', 'Donald McDonald', 'of a only']), ('regression: zone boundary', [[['rag', 'ged'], ['rag', 'ged'], ['in', 'ter', 'na', 'tion', 'al'], ['a'], ['im', 'por', 'tant'], ['hy', 'phen', 'at', 'ed'], ['rag', 'ged'], ['e', 'Book', 'ing'], ['on', 'ly']], 10, 3, 5, 2, 2, True], ['ragged', 'ragged in-', 'ternation-', 'al a im-', 'portant', 'hyphenated', 'ragged', 'eBooking', 'only']), ('regression: zone boundary', [[['ty', 'po', 'gra', 'phy'], ['Mc', 'Don', 'ald'], ['hy', 'phen', 'at', 'ed'], ['the'], ['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['Mc', 'Don', 'ald'], ['the'], ['in', 'ter', 'na', 'tion', 'al'], ['a']], 14, 5, 5, 1, 2, True], ['typography', 'McDonald hy-', 'phenated the', 'important', 'eBooking Mc-', 'Donald the', 'international', 'a']), ('partial-repair probe', [[['in', 'ter', 'na', 'tion', 'al'], ['a'], ['on', 'ly'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy']], 9, 5, 5, 1, 2, False], ['interna-', 'tional a', 'only', 'interna-', 'tional', 'typogra-', 'phy']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['of'], ['Ber', 'lin'], ['e', 'Book', 'ing'], ['Ber', 'lin'], ['Mc', 'Don', 'ald'], ['Mc', 'Don', 'ald'], ['Ber', 'lin'], ['e', 'Book', 'ing'], ['Mc', 'Don', 'ald'], ['e', 'Book', 'ing']], 10, 2, 7, 3, 3, False], ['of Berlin', 'eBooking', 'Berlin', 'McDonald', 'McDonald', 'Berlin', 'eBooking', 'McDonald', 'eBooking']), ('control layout', [[['hy', 'phen', 'at', 'ed'], ['im', 'por', 'tant'], ['the'], ['on', 'ly']], 14, 5, 6, 2, 2, True], ['hyphenated', 'important the', 'only'])], [('regression: zone boundary', [[['Mc', 'Don', 'ald'], ['in', 'ter', 'na', 'tion', 'al'], ['rag', 'ged'], ['the'], ['hy', 'phen', 'at', 'ed'], ['the'], ['of'], ['on', 'ly'], ['set']], 9, 5, 5, 1, 2, False], ['McDonald', 'interna-', 'tional', 'ragged', 'the hy-', 'phenated', 'the of', 'only set']), ('regression: zone boundary', [[['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['im', 'por', 'tant'], ['the'], ['on', 'ly'], ['im', 'por', 'tant'], ['a'], ['Mc', 'Don', 'ald']], 15, 3, 5, 1, 3, False], ['hyphenated in-', 'ternational im-', 'portant the', 'only important', 'a McDonald']), ('partial-repair probe', [[['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['on', 'ly'], ['set'], ['set'], ['im', 'por', 'tant'], ['hy', 'phen', 'at', 'ed']], 14, 5, 7, 2, 3, False], ['hyphenated', 'international', 'only set set', 'important', 'hyphenated']), ('partial-repair probe', [[['of'], ['rag', 'ged'], ['Mc', 'Don', 'ald'], ['the'], ['in', 'ter', 'na', 'tion', 'al']], 13, 4, 6, 1, 3, True], ['of ragged', 'McDonald the', 'international']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['set'], ['Ber', 'lin'], ['Ber', 'lin'], ['e', 'Book', 'ing'], ['the']], 14, 4, 7, 1, 3, True], ['set Berlin', 'Berlin eBook-', 'ing the']), ('control layout', [[['of'], ['rag', 'ged'], ['im', 'por', 'tant'], ['set'], ['ty', 'po', 'gra', 'phy']], 16, 1, 7, 1, 3, True], ['of ragged impor-', 'tant set typo-', 'graphy'])]]
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(x):
    words, width, zone, min_word, min_before, min_after, caps = x
    lines = []
    cur = ''
    k = 0
    queue = [list(w) for w in words]
    while k < len(queue):
        syl = queue[k]
        token = ''.join(syl)
        cand = token if not cur else cur + ' ' + token
        if len(cand) <= width:
            cur = cand
            k += 1
            continue
        slack = width - len(cur) - (1 if cur else 0)
        eligible = slack >= zone - 1 and len(token) >= min_word and (caps or not token[0].isupper())
        cut = None
        if eligible:
            for c in range(len(syl) - 1, 0, -1):
                head = ''.join(syl[:c])
                tail = ''.join(syl[c:])
                if len(head) < min_before or len(tail) < min_after:
                    continue
                fit = len(head) + 1 if not cur else len(cur) + len(head) + 2
                if fit <= width:
                    cut = c
                    break
        if cut is not None:
            head = ''.join(syl[:cut]) + '-'
            lines.append(head if not cur else cur + ' ' + head)
            queue[k] = syl[cut:]
            cur = ''
        elif cur:
            lines.append(cur)
            cur = ''
        else:
            cur = token
            k += 1
    if cur:
        lines.append(cur)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('regression: zone boundary', [[['hy', 'phen', 'at', 'ed'], ['e', 'Book', 'ing'], ['on', 'ly'], ['hy', 'phen', 'at', 'ed'], ['e', 'Book', 'ing'], ['on', 'ly']], 11, 2, 6, 1, 2, False], ['hyphenated', 'eBooking', 'only hy-', 'phenated e-', 'Booking', 'only']), ('partial-repair probe', [[['in', 'ter', 'na', 'tion', 'al'], ['a'], ['on', 'ly'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy']], 9, 5, 5, 1, 2, False], ['interna-', 'tional a', 'only', 'interna-', 'tional', 'typogra-', 'phy']), ('partial-repair probe', [[['Mc', 'Don', 'ald'], ['of'], ['the'], ['Mc', 'Don', 'ald'], ['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['hy', 'phen', 'at', 'ed'], ['on', 'ly']], 8, 4, 4, 1, 2, False], ['McDonald', 'of the', 'McDonald', 'impor-', 'tant', 'eBooking', 'hyphen-', 'ated', 'only']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['rag', 'ged'], ['of'], ['the'], ['Ber', 'lin'], ['of'], ['Ber', 'lin'], ['set'], ['a'], ['rag', 'ged'], ['of']], 15, 1, 7, 3, 2, False], ['ragged of the', 'Berlin of', 'Berlin set a', 'ragged of']), ('control layout', [[['Ber', 'lin'], ['Ber', 'lin'], ['a'], ['in', 'ter', 'na', 'tion', 'al']], 11, 3, 5, 1, 2, False], ['Berlin', 'Berlin a', 'interna-', 'tional']), ('control layout', [[['Ber', 'lin'], ['e', 'Book', 'ing'], ['rag', 'ged'], ['hy', 'phen', 'at', 'ed']], 13, 3, 7, 3, 3, False], ['Berlin eBook-', 'ing ragged', 'hyphenated'])], [('regression: zone boundary', [[['ty', 'po', 'gra', 'phy'], ['hy', 'phen', 'at', 'ed'], ['of'], ['rag', 'ged'], ['hy', 'phen', 'at', 'ed'], ['e', 'Book', 'ing'], ['e', 'Book', 'ing'], ['im', 'por', 'tant'], ['the'], ['on', 'ly']], 16, 4, 5, 2, 3, False], ['typography hy-', 'phenated of rag-', 'ged hyphenated', 'eBooking eBook-', 'ing important', 'the only']), ('regression: zone boundary', [[['e', 'Book', 'ing'], ['in', 'ter', 'na', 'tion', 'al'], ['e', 'Book', 'ing'], ['Mc', 'Don', 'ald'], ['rag', 'ged'], ['of'], ['im', 'por', 'tant'], ['set'], ['in', 'ter', 'na', 'tion', 'al']], 14, 5, 7, 1, 3, False], ['eBooking in-', 'ternational', 'eBooking', 'McDonald', 'ragged of', 'important set', 'international']), ('partial-repair probe', [[['on', 'ly'], ['Mc', 'Don', 'ald'], ['rag', 'ged'], ['ty', 'po', 'gra', 'phy'], ['ty', 'po', 'gra', 'phy'], ['rag', 'ged'], ['Ber', 'lin'], ['im', 'por', 'tant'], ['a']], 10, 4, 4, 1, 2, False], ['only', 'McDonald', 'ragged', 'typography', 'typography', 'ragged', 'Berlin', 'important', 'a']), ('partial-repair probe', [[['rag', 'ged'], ['Mc', 'Don', 'ald'], ['on', 'ly'], ['ty', 'po', 'gra', 'phy']], 9, 5, 7, 1, 2, False], ['ragged', 'McDonald', 'only', 'typogra-', 'phy']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['of'], ['Ber', 'lin'], ['e', 'Book', 'ing'], ['im', 'por', 'tant'], ['of'], ['Ber', 'lin'], ['the']], 15, 3, 6, 1, 2, True], ['of Berlin e-', 'Booking impor-', 'tant of Berlin', 'the']), ('control layout', [[['ty', 'po', 'gra', 'phy'], ['set'], ['Mc', 'Don', 'ald'], ['the']], 15, 5, 4, 2, 3, True], ['typography set', 'McDonald the'])], [('regression: zone boundary', [[['im', 'por', 'tant'], ['Mc', 'Don', 'ald'], ['hy', 'phen', 'at', 'ed'], ['of'], ['hy', 'phen', 'at', 'ed'], ['rag', 'ged']], 12, 3, 5, 1, 2, True], ['important', 'McDonald hy-', 'phenated of', 'hyphenated', 'ragged']), ('regression: zone boundary', [[['Mc', 'Don', 'ald'], ['rag', 'ged'], ['e', 'Book', 'ing'], ['ty', 'po', 'gra', 'phy'], ['Ber', 'lin'], ['ty', 'po', 'gra', 'phy'], ['a'], ['rag', 'ged'], ['ty', 'po', 'gra', 'phy']], 11, 3, 6, 1, 2, False], ['McDonald', 'ragged e-', 'Booking ty-', 'pography', 'Berlin ty-', 'pography a', 'ragged ty-', 'pography']), ('partial-repair probe', [[['the'], ['in', 'ter', 'na', 'tion', 'al'], ['on', 'ly'], ['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy'], ['ty', 'po', 'gra', 'phy']], 14, 4, 4, 2, 2, False], ['the interna-', 'tional only', 'hyphenated', 'international', 'international', 'typography', 'typography']), ('partial-repair probe', [[['rag', 'ged'], ['Ber', 'lin'], ['on', 'ly'], ['hy', 'phen', 'at', 'ed'], ['set'], ['Ber', 'lin']], 12, 4, 4, 2, 3, True], ['ragged Ber-', 'lin only', 'hyphenated', 'set Berlin']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('control layout', [[['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['in', 'ter', 'na', 'tion', 'al'], ['Mc', 'Don', 'ald'], ['of'], ['on', 'ly'], ['e', 'Book', 'ing'], ['rag', 'ged']], 9, 5, 7, 2, 3, False], ['important', 'eBooking', 'interna-', 'tional', 'McDonald', 'of only', 'eBooking', 'ragged']), ('control layout', [[['e', 'Book', 'ing'], ['on', 'ly'], ['Ber', 'lin'], ['in', 'ter', 'na', 'tion', 'al'], ['Ber', 'lin'], ['hy', 'phen', 'at', 'ed'], ['e', 'Book', 'ing']], 14, 3, 6, 1, 2, True], ['eBooking only', 'Berlin inter-', 'national Ber-', 'lin hyphenated', 'eBooking'])], [('regression: zone boundary', [[['e', 'Book', 'ing'], ['rag', 'ged'], ['ty', 'po', 'gra', 'phy'], ['Mc', 'Don', 'ald'], ['Mc', 'Don', 'ald'], ['of'], ['a'], ['on', 'ly']], 15, 4, 4, 1, 3, True], ['eBooking ragged', 'typography Mc-', 'Donald McDonald', 'of a only']), ('regression: zone boundary', [[['rag', 'ged'], ['rag', 'ged'], ['in', 'ter', 'na', 'tion', 'al'], ['a'], ['im', 'por', 'tant'], ['hy', 'phen', 'at', 'ed'], ['rag', 'ged'], ['e', 'Book', 'ing'], ['on', 'ly']], 10, 3, 5, 2, 2, True], ['ragged', 'ragged in-', 'ternation-', 'al a im-', 'portant', 'hyphenated', 'ragged', 'eBooking', 'only']), ('regression: zone boundary', [[['ty', 'po', 'gra', 'phy'], ['Mc', 'Don', 'ald'], ['hy', 'phen', 'at', 'ed'], ['the'], ['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['Mc', 'Don', 'ald'], ['the'], ['in', 'ter', 'na', 'tion', 'al'], ['a']], 14, 5, 5, 1, 2, True], ['typography', 'McDonald hy-', 'phenated the', 'important', 'eBooking Mc-', 'Donald the', 'international', 'a']), ('partial-repair probe', [[['in', 'ter', 'na', 'tion', 'al'], ['a'], ['on', 'ly'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy']], 9, 5, 5, 1, 2, False], ['interna-', 'tional a', 'only', 'interna-', 'tional', 'typogra-', 'phy']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['of'], ['Ber', 'lin'], ['e', 'Book', 'ing'], ['Ber', 'lin'], ['Mc', 'Don', 'ald'], ['Mc', 'Don', 'ald'], ['Ber', 'lin'], ['e', 'Book', 'ing'], ['Mc', 'Don', 'ald'], ['e', 'Book', 'ing']], 10, 2, 7, 3, 3, False], ['of Berlin', 'eBooking', 'Berlin', 'McDonald', 'McDonald', 'Berlin', 'eBooking', 'McDonald', 'eBooking']), ('control layout', [[['hy', 'phen', 'at', 'ed'], ['im', 'por', 'tant'], ['the'], ['on', 'ly']], 14, 5, 6, 2, 2, True], ['hyphenated', 'important the', 'only'])], [('regression: zone boundary', [[['Mc', 'Don', 'ald'], ['in', 'ter', 'na', 'tion', 'al'], ['rag', 'ged'], ['the'], ['hy', 'phen', 'at', 'ed'], ['the'], ['of'], ['on', 'ly'], ['set']], 9, 5, 5, 1, 2, False], ['McDonald', 'interna-', 'tional', 'ragged', 'the hy-', 'phenated', 'the of', 'only set']), ('regression: zone boundary', [[['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['im', 'por', 'tant'], ['the'], ['on', 'ly'], ['im', 'por', 'tant'], ['a'], ['Mc', 'Don', 'ald']], 15, 3, 5, 1, 3, False], ['hyphenated in-', 'ternational im-', 'portant the', 'only important', 'a McDonald']), ('partial-repair probe', [[['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['on', 'ly'], ['set'], ['set'], ['im', 'por', 'tant'], ['hy', 'phen', 'at', 'ed']], 14, 5, 7, 2, 3, False], ['hyphenated', 'international', 'only set set', 'important', 'hyphenated']), ('partial-repair probe', [[['of'], ['rag', 'ged'], ['Mc', 'Don', 'ald'], ['the'], ['in', 'ter', 'na', 'tion', 'al']], 13, 4, 6, 1, 3, True], ['of ragged', 'McDonald the', 'international']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['set'], ['Ber', 'lin'], ['Ber', 'lin'], ['e', 'Book', 'ing'], ['the']], 14, 4, 7, 1, 3, True], ['set Berlin', 'Berlin eBook-', 'ing the']), ('control layout', [[['of'], ['rag', 'ged'], ['im', 'por', 'tant'], ['set'], ['ty', 'po', 'gra', 'phy']], 16, 1, 7, 1, 3, True], ['of ragged impor-', 'tant set typo-', 'graphy'])]]
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:
Words are not hyphenated when the slack exactly equals the zone.

Input [words as syllables, width, zone, min word length, min chars before, min chars after, hyphenate capitals]. Greedy fill. A word that does not fit may be hyphenated only if the slack left on the line (width minus current text minus the separating space) is at least the zone, the word has at least min-word characters, and (capitals allowed or its first letter is not uppercase). Pick the latest syllable boundary with head >= min before and tail >= min after chars whose head plus "-" fits. Return lines.

split: open-access


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

N = 1
observations = []
def solve(x):
    words, width, zone, min_word, min_before, min_after, caps = x
    lines = []
    cur = ''
    k = 0
    queue = [list(w) for w in words]
    while k < len(queue):
        syl = queue[k]
        token = ''.join(syl)
        cand = token if not cur else cur + ' ' + token
        if len(cand) <= width:
            cur = cand
            k += 1
            continue
        slack = width - len(cur)
        eligible = slack >= zone and len(token) >= min_word and (caps or not token[0].isupper())
        cut = None
        if eligible:
            for c in range(len(syl) - 1, 0, -1):
                head = ''.join(syl[:c])
                tail = ''.join(syl[c:])
                if len(head) < min_before or len(tail) < min_after:
                    continue
                fit = len(head) + 1 if not cur else len(cur) + len(head) + 2
                if fit <= width:
                    cut = c
                    break
        if cut is not None:
            head = ''.join(syl[:cut]) + '-'
            lines.append(head if not cur else cur + ' ' + head)
            queue[k] = syl[cut:]
            cur = ''
        elif cur:
            lines.append(cur)
            cur = ''
        else:
            cur = token
            k += 1
    if cur:
        lines.append(cur)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: slack separator', [[['Mc', 'Don', 'ald'], ['rag', 'ged'], ['Ber', 'lin'], ['the'], ['Mc', 'Don', 'ald'], ['in', 'ter', 'na', 'tion', 'al'], ['rag', 'ged'], ['ty', 'po', 'gra', 'phy'], ['in', 'ter', 'na', 'tion', 'al']], 12, 4, 6, 2, 3, False], ['McDonald', 'ragged', 'Berlin the', 'McDonald', 'interna-', 'tional rag-', 'ged typogra-', 'phy interna-', 'tional']), ('regression: slack separator', [[['a'], ['the'], ['hy', 'phen', 'at', 'ed'], ['a'], ['ty', 'po', 'gra', 'phy'], ['ty', 'po', 'gra', 'phy'], ['e', 'Book', 'ing'], ['im', 'por', 'tant'], ['im', 'por', 'tant'], ['in', 'ter', 'na', 'tion', 'al']], 9, 4, 7, 2, 3, False], ['a the', 'hyphen-', 'ated a', 'typogra-', 'phy typo-', 'graphy', 'eBooking', 'important', 'important', 'interna-', 'tional']), ('regression: slack separator', [[['hy', 'phen', 'at', 'ed'], ['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['hy', 'phen', 'at', 'ed']], 15, 5, 7, 2, 2, False], ['hyphenated', 'hyphenated', 'international', 'hyphenated']), ('partial-repair probe', [[['rag', 'ged'], ['ty', 'po', 'gra', 'phy'], ['e', 'Book', 'ing'], ['Ber', 'lin'], ['Mc', 'Don', 'ald'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy'], ['hy', 'phen', 'at', 'ed']], 10, 5, 5, 2, 2, False], ['ragged', 'typography', 'eBooking', 'Berlin', 'McDonald', 'interna-', 'tional', 'typography', 'hyphenated']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['on', 'ly'], ['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['Mc', 'Don', 'ald'], ['ty', 'po', 'gra', 'phy'], ['Mc', 'Don', 'ald'], ['Mc', 'Don', 'ald'], ['hy', 'phen', 'at', 'ed']], 13, 3, 6, 2, 2, False], ['only impor-', 'tant eBooking', 'McDonald ty-', 'pography', 'McDonald', 'McDonald hy-', 'phenated']), ('control layout', [[['hy', 'phen', 'at', 'ed'], ['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['a'], ['im', 'por', 'tant'], ['the'], ['Ber', 'lin'], ['set'], ['a']], 13, 2, 6, 3, 2, False], ['hyphenated', 'hyphenated', 'international', 'a important', 'the Berlin', 'set a'])], [('regression: slack separator', [[['Mc', 'Don', 'ald'], ['of'], ['the'], ['Mc', 'Don', 'ald'], ['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['hy', 'phen', 'at', 'ed'], ['on', 'ly']], 8, 4, 4, 1, 2, False], ['McDonald', 'of the', 'McDonald', 'impor-', 'tant', 'eBooking', 'hyphen-', 'ated', 'only']), ('regression: slack separator', [[['on', 'ly'], ['of'], ['set'], ['in', 'ter', 'na', 'tion', 'al'], ['set'], ['the'], ['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['e', 'Book', 'ing']], 12, 3, 4, 1, 2, True], ['only of set', 'internation-', 'al set the', 'important', 'eBooking e-', 'Booking']), ('regression: slack separator', [[['a'], ['Ber', 'lin'], ['a'], ['in', 'ter', 'na', 'tion', 'al'], ['of'], ['im', 'por', 'tant'], ['a'], ['in', 'ter', 'na', 'tion', 'al'], ['e', 'Book', 'ing'], ['on', 'ly']], 15, 4, 5, 1, 3, True], ['a Berlin a in-', 'ternational of', 'important a', 'international', 'eBooking only']), ('regression: slack separator', [[['on', 'ly'], ['the'], ['of'], ['Mc', 'Don', 'ald'], ['ty', 'po', 'gra', 'phy']], 15, 4, 6, 1, 3, True], ['only the of', 'McDonald typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('control layout', [[['hy', 'phen', 'at', 'ed'], ['Ber', 'lin'], ['set'], ['e', 'Book', 'ing'], ['e', 'Book', 'ing'], ['the']], 8, 3, 6, 2, 3, True], ['hyphen-', 'ated', 'Berlin', 'set', 'eBooking', 'eBooking', 'the']), ('control layout', [[['of'], ['of'], ['set'], ['ty', 'po', 'gra', 'phy'], ['rag', 'ged']], 9, 1, 6, 3, 2, True], ['of of set', 'typogra-', 'phy rag-', 'ged'])], [('regression: slack separator', [[['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['the'], ['a'], ['the'], ['ty', 'po', 'gra', 'phy'], ['Mc', 'Don', 'ald'], ['set'], ['Ber', 'lin']], 15, 5, 7, 2, 3, False], ['hyphenated', 'international', 'the a the typo-', 'graphy McDonald', 'set Berlin']), ('regression: slack separator', [[['e', 'Book', 'ing'], ['im', 'por', 'tant'], ['of'], ['hy', 'phen', 'at', 'ed'], ['im', 'por', 'tant'], ['hy', 'phen', 'at', 'ed'], ['e', 'Book', 'ing']], 14, 5, 5, 1, 2, True], ['eBooking im-', 'portant of', 'hyphenated', 'important', 'hyphenated', 'eBooking']), ('regression: slack separator', [[['Mc', 'Don', 'ald'], ['of'], ['the'], ['Mc', 'Don', 'ald'], ['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['hy', 'phen', 'at', 'ed'], ['on', 'ly']], 8, 4, 4, 1, 2, False], ['McDonald', 'of the', 'McDonald', 'impor-', 'tant', 'eBooking', 'hyphen-', 'ated', 'only']), ('regression: slack separator', [[['hy', 'phen', 'at', 'ed'], ['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['hy', 'phen', 'at', 'ed']], 15, 5, 7, 2, 2, False], ['hyphenated', 'hyphenated', 'international', 'hyphenated']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['on', 'ly'], ['the'], ['im', 'por', 'tant'], ['of']], 14, 1, 4, 3, 2, True], ['only the', 'important of']), ('control layout', [[['a'], ['Ber', 'lin'], ['on', 'ly'], ['e', 'Book', 'ing']], 8, 5, 5, 2, 3, True], ['a Berlin', 'only', 'eBooking'])], [('regression: slack separator', [[['the'], ['in', 'ter', 'na', 'tion', 'al'], ['on', 'ly'], ['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy'], ['ty', 'po', 'gra', 'phy']], 14, 4, 4, 2, 2, False], ['the interna-', 'tional only', 'hyphenated', 'international', 'international', 'typography', 'typography']), ('regression: slack separator', [[['the'], ['ty', 'po', 'gra', 'phy'], ['ty', 'po', 'gra', 'phy'], ['set'], ['rag', 'ged'], ['set'], ['Mc', 'Don', 'ald'], ['rag', 'ged']], 8, 5, 4, 1, 2, True], ['the', 'typogra-', 'phy', 'typogra-', 'phy set', 'ragged', 'set', 'McDonald', 'ragged']), ('regression: slack separator', [[['ty', 'po', 'gra', 'phy'], ['in', 'ter', 'na', 'tion', 'al'], ['Mc', 'Don', 'ald'], ['Ber', 'lin'], ['rag', 'ged'], ['rag', 'ged'], ['on', 'ly'], ['of'], ['hy', 'phen', 'at', 'ed']], 11, 5, 5, 2, 3, False], ['typography', 'interna-', 'tional', 'McDonald', 'Berlin', 'ragged', 'ragged only', 'of hyphen-', 'ated']), ('regression: slack separator', [[['in', 'ter', 'na', 'tion', 'al'], ['a'], ['on', 'ly'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy']], 9, 5, 5, 1, 2, False], ['interna-', 'tional a', 'only', 'interna-', 'tional', 'typogra-', 'phy']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['a'], ['e', 'Book', 'ing'], ['rag', 'ged'], ['e', 'Book', 'ing'], ['the'], ['of'], ['a'], ['ty', 'po', 'gra', 'phy']], 11, 4, 7, 2, 2, False], ['a eBooking', 'ragged', 'eBooking', 'the of a', 'typography']), ('control layout', [[['of'], ['on', 'ly'], ['ty', 'po', 'gra', 'phy'], ['Ber', 'lin'], ['rag', 'ged'], ['hy', 'phen', 'at', 'ed'], ['a'], ['im', 'por', 'tant'], ['a'], ['Mc', 'Don', 'ald']], 9, 1, 7, 2, 2, False], ['of only', 'typogra-', 'phy', 'Berlin', 'ragged', 'hyphenat-', 'ed a im-', 'portant a', 'McDonald'])], [('regression: slack separator', [[['ty', 'po', 'gra', 'phy'], ['ty', 'po', 'gra', 'phy'], ['e', 'Book', 'ing'], ['Mc', 'Don', 'ald'], ['the'], ['hy', 'phen', 'at', 'ed'], ['Mc', 'Don', 'ald'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy'], ['of']], 13, 3, 7, 1, 3, True], ['typography', 'typography', 'eBooking Mc-', 'Donald the', 'hyphenated', 'McDonald in-', 'ternational', 'typography of']), ('regression: slack separator', [[['the'], ['Mc', 'Don', 'ald'], ['Ber', 'lin'], ['a'], ['Ber', 'lin'], ['a'], ['a'], ['in', 'ter', 'na', 'tion', 'al'], ['hy', 'phen', 'at', 'ed'], ['ty', 'po', 'gra', 'phy']], 11, 5, 5, 2, 2, False], ['the', 'McDonald', 'Berlin a', 'Berlin a a', 'interna-', 'tional', 'hyphenated', 'typography']), ('regression: slack separator', [[['hy', 'phen', 'at', 'ed'], ['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['hy', 'phen', 'at', 'ed']], 15, 5, 7, 2, 2, False], ['hyphenated', 'hyphenated', 'international', 'hyphenated']), ('regression: slack separator', [[['Mc', 'Don', 'ald'], ['on', 'ly'], ['Ber', 'lin'], ['hy', 'phen', 'at', 'ed'], ['e', 'Book', 'ing'], ['Ber', 'lin'], ['on', 'ly'], ['rag', 'ged'], ['rag', 'ged'], ['im', 'por', 'tant']], 8, 4, 7, 1, 2, False], ['McDonald', 'only', 'Berlin', 'hyphen-', 'ated', 'eBooking', 'Berlin', 'only', 'ragged', 'ragged', 'impor-', 'tant']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('control layout', [[['the'], ['in', 'ter', 'na', 'tion', 'al'], ['on', 'ly'], ['ty', 'po', 'gra', 'phy'], ['set'], ['hy', 'phen', 'at', 'ed'], ['of']], 11, 3, 4, 2, 2, False], ['the inter-', 'national', 'only typo-', 'graphy set', 'hyphenated', 'of']), ('control layout', [[['a'], ['rag', 'ged'], ['of'], ['of'], ['the'], ['of'], ['on', 'ly'], ['Mc', 'Don', 'ald'], ['the']], 11, 3, 7, 2, 2, False], ['a ragged of', 'of the of', 'only', 'McDonald', 'the'])]]
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(x):
    words, width, zone, min_word, min_before, min_after, caps = x
    lines = []
    cur = ''
    k = 0
    queue = [list(w) for w in words]
    while k < len(queue):
        syl = queue[k]
        token = ''.join(syl)
        cand = token if not cur else cur + ' ' + token
        if len(cand) <= width:
            cur = cand
            k += 1
            continue
        slack = width - len(cur) + (1 if cur else 0)
        eligible = slack >= zone and len(token) >= min_word and (caps or not token[0].isupper())
        cut = None
        if eligible:
            for c in range(len(syl) - 1, 0, -1):
                head = ''.join(syl[:c])
                tail = ''.join(syl[c:])
                if len(head) < min_before or len(tail) < min_after:
                    continue
                fit = len(head) + 1 if not cur else len(cur) + len(head) + 2
                if fit <= width:
                    cut = c
                    break
        if cut is not None:
            head = ''.join(syl[:cut]) + '-'
            lines.append(head if not cur else cur + ' ' + head)
            queue[k] = syl[cut:]
            cur = ''
        elif cur:
            lines.append(cur)
            cur = ''
        else:
            cur = token
            k += 1
    if cur:
        lines.append(cur)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: slack separator', [[['Mc', 'Don', 'ald'], ['rag', 'ged'], ['Ber', 'lin'], ['the'], ['Mc', 'Don', 'ald'], ['in', 'ter', 'na', 'tion', 'al'], ['rag', 'ged'], ['ty', 'po', 'gra', 'phy'], ['in', 'ter', 'na', 'tion', 'al']], 12, 4, 6, 2, 3, False], ['McDonald', 'ragged', 'Berlin the', 'McDonald', 'interna-', 'tional rag-', 'ged typogra-', 'phy interna-', 'tional']), ('regression: slack separator', [[['a'], ['the'], ['hy', 'phen', 'at', 'ed'], ['a'], ['ty', 'po', 'gra', 'phy'], ['ty', 'po', 'gra', 'phy'], ['e', 'Book', 'ing'], ['im', 'por', 'tant'], ['im', 'por', 'tant'], ['in', 'ter', 'na', 'tion', 'al']], 9, 4, 7, 2, 3, False], ['a the', 'hyphen-', 'ated a', 'typogra-', 'phy typo-', 'graphy', 'eBooking', 'important', 'important', 'interna-', 'tional']), ('regression: slack separator', [[['hy', 'phen', 'at', 'ed'], ['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['hy', 'phen', 'at', 'ed']], 15, 5, 7, 2, 2, False], ['hyphenated', 'hyphenated', 'international', 'hyphenated']), ('partial-repair probe', [[['rag', 'ged'], ['ty', 'po', 'gra', 'phy'], ['e', 'Book', 'ing'], ['Ber', 'lin'], ['Mc', 'Don', 'ald'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy'], ['hy', 'phen', 'at', 'ed']], 10, 5, 5, 2, 2, False], ['ragged', 'typography', 'eBooking', 'Berlin', 'McDonald', 'interna-', 'tional', 'typography', 'hyphenated']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['on', 'ly'], ['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['Mc', 'Don', 'ald'], ['ty', 'po', 'gra', 'phy'], ['Mc', 'Don', 'ald'], ['Mc', 'Don', 'ald'], ['hy', 'phen', 'at', 'ed']], 13, 3, 6, 2, 2, False], ['only impor-', 'tant eBooking', 'McDonald ty-', 'pography', 'McDonald', 'McDonald hy-', 'phenated']), ('control layout', [[['hy', 'phen', 'at', 'ed'], ['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['a'], ['im', 'por', 'tant'], ['the'], ['Ber', 'lin'], ['set'], ['a']], 13, 2, 6, 3, 2, False], ['hyphenated', 'hyphenated', 'international', 'a important', 'the Berlin', 'set a'])], [('regression: slack separator', [[['Mc', 'Don', 'ald'], ['of'], ['the'], ['Mc', 'Don', 'ald'], ['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['hy', 'phen', 'at', 'ed'], ['on', 'ly']], 8, 4, 4, 1, 2, False], ['McDonald', 'of the', 'McDonald', 'impor-', 'tant', 'eBooking', 'hyphen-', 'ated', 'only']), ('regression: slack separator', [[['on', 'ly'], ['of'], ['set'], ['in', 'ter', 'na', 'tion', 'al'], ['set'], ['the'], ['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['e', 'Book', 'ing']], 12, 3, 4, 1, 2, True], ['only of set', 'internation-', 'al set the', 'important', 'eBooking e-', 'Booking']), ('regression: slack separator', [[['a'], ['Ber', 'lin'], ['a'], ['in', 'ter', 'na', 'tion', 'al'], ['of'], ['im', 'por', 'tant'], ['a'], ['in', 'ter', 'na', 'tion', 'al'], ['e', 'Book', 'ing'], ['on', 'ly']], 15, 4, 5, 1, 3, True], ['a Berlin a in-', 'ternational of', 'important a', 'international', 'eBooking only']), ('regression: slack separator', [[['on', 'ly'], ['the'], ['of'], ['Mc', 'Don', 'ald'], ['ty', 'po', 'gra', 'phy']], 15, 4, 6, 1, 3, True], ['only the of', 'McDonald typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('control layout', [[['hy', 'phen', 'at', 'ed'], ['Ber', 'lin'], ['set'], ['e', 'Book', 'ing'], ['e', 'Book', 'ing'], ['the']], 8, 3, 6, 2, 3, True], ['hyphen-', 'ated', 'Berlin', 'set', 'eBooking', 'eBooking', 'the']), ('control layout', [[['of'], ['of'], ['set'], ['ty', 'po', 'gra', 'phy'], ['rag', 'ged']], 9, 1, 6, 3, 2, True], ['of of set', 'typogra-', 'phy rag-', 'ged'])], [('regression: slack separator', [[['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['the'], ['a'], ['the'], ['ty', 'po', 'gra', 'phy'], ['Mc', 'Don', 'ald'], ['set'], ['Ber', 'lin']], 15, 5, 7, 2, 3, False], ['hyphenated', 'international', 'the a the typo-', 'graphy McDonald', 'set Berlin']), ('regression: slack separator', [[['e', 'Book', 'ing'], ['im', 'por', 'tant'], ['of'], ['hy', 'phen', 'at', 'ed'], ['im', 'por', 'tant'], ['hy', 'phen', 'at', 'ed'], ['e', 'Book', 'ing']], 14, 5, 5, 1, 2, True], ['eBooking im-', 'portant of', 'hyphenated', 'important', 'hyphenated', 'eBooking']), ('regression: slack separator', [[['Mc', 'Don', 'ald'], ['of'], ['the'], ['Mc', 'Don', 'ald'], ['im', 'por', 'tant'], ['e', 'Book', 'ing'], ['hy', 'phen', 'at', 'ed'], ['on', 'ly']], 8, 4, 4, 1, 2, False], ['McDonald', 'of the', 'McDonald', 'impor-', 'tant', 'eBooking', 'hyphen-', 'ated', 'only']), ('regression: slack separator', [[['hy', 'phen', 'at', 'ed'], ['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['hy', 'phen', 'at', 'ed']], 15, 5, 7, 2, 2, False], ['hyphenated', 'hyphenated', 'international', 'hyphenated']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['on', 'ly'], ['the'], ['im', 'por', 'tant'], ['of']], 14, 1, 4, 3, 2, True], ['only the', 'important of']), ('control layout', [[['a'], ['Ber', 'lin'], ['on', 'ly'], ['e', 'Book', 'ing']], 8, 5, 5, 2, 3, True], ['a Berlin', 'only', 'eBooking'])], [('regression: slack separator', [[['the'], ['in', 'ter', 'na', 'tion', 'al'], ['on', 'ly'], ['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy'], ['ty', 'po', 'gra', 'phy']], 14, 4, 4, 2, 2, False], ['the interna-', 'tional only', 'hyphenated', 'international', 'international', 'typography', 'typography']), ('regression: slack separator', [[['the'], ['ty', 'po', 'gra', 'phy'], ['ty', 'po', 'gra', 'phy'], ['set'], ['rag', 'ged'], ['set'], ['Mc', 'Don', 'ald'], ['rag', 'ged']], 8, 5, 4, 1, 2, True], ['the', 'typogra-', 'phy', 'typogra-', 'phy set', 'ragged', 'set', 'McDonald', 'ragged']), ('regression: slack separator', [[['ty', 'po', 'gra', 'phy'], ['in', 'ter', 'na', 'tion', 'al'], ['Mc', 'Don', 'ald'], ['Ber', 'lin'], ['rag', 'ged'], ['rag', 'ged'], ['on', 'ly'], ['of'], ['hy', 'phen', 'at', 'ed']], 11, 5, 5, 2, 3, False], ['typography', 'interna-', 'tional', 'McDonald', 'Berlin', 'ragged', 'ragged only', 'of hyphen-', 'ated']), ('regression: slack separator', [[['in', 'ter', 'na', 'tion', 'al'], ['a'], ['on', 'ly'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy']], 9, 5, 5, 1, 2, False], ['interna-', 'tional a', 'only', 'interna-', 'tional', 'typogra-', 'phy']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('control layout', [[['a'], ['e', 'Book', 'ing'], ['rag', 'ged'], ['e', 'Book', 'ing'], ['the'], ['of'], ['a'], ['ty', 'po', 'gra', 'phy']], 11, 4, 7, 2, 2, False], ['a eBooking', 'ragged', 'eBooking', 'the of a', 'typography']), ('control layout', [[['of'], ['on', 'ly'], ['ty', 'po', 'gra', 'phy'], ['Ber', 'lin'], ['rag', 'ged'], ['hy', 'phen', 'at', 'ed'], ['a'], ['im', 'por', 'tant'], ['a'], ['Mc', 'Don', 'ald']], 9, 1, 7, 2, 2, False], ['of only', 'typogra-', 'phy', 'Berlin', 'ragged', 'hyphenat-', 'ed a im-', 'portant a', 'McDonald'])], [('regression: slack separator', [[['ty', 'po', 'gra', 'phy'], ['ty', 'po', 'gra', 'phy'], ['e', 'Book', 'ing'], ['Mc', 'Don', 'ald'], ['the'], ['hy', 'phen', 'at', 'ed'], ['Mc', 'Don', 'ald'], ['in', 'ter', 'na', 'tion', 'al'], ['ty', 'po', 'gra', 'phy'], ['of']], 13, 3, 7, 1, 3, True], ['typography', 'typography', 'eBooking Mc-', 'Donald the', 'hyphenated', 'McDonald in-', 'ternational', 'typography of']), ('regression: slack separator', [[['the'], ['Mc', 'Don', 'ald'], ['Ber', 'lin'], ['a'], ['Ber', 'lin'], ['a'], ['a'], ['in', 'ter', 'na', 'tion', 'al'], ['hy', 'phen', 'at', 'ed'], ['ty', 'po', 'gra', 'phy']], 11, 5, 5, 2, 2, False], ['the', 'McDonald', 'Berlin a', 'Berlin a a', 'interna-', 'tional', 'hyphenated', 'typography']), ('regression: slack separator', [[['hy', 'phen', 'at', 'ed'], ['hy', 'phen', 'at', 'ed'], ['in', 'ter', 'na', 'tion', 'al'], ['hy', 'phen', 'at', 'ed']], 15, 5, 7, 2, 2, False], ['hyphenated', 'hyphenated', 'international', 'hyphenated']), ('regression: slack separator', [[['Mc', 'Don', 'ald'], ['on', 'ly'], ['Ber', 'lin'], ['hy', 'phen', 'at', 'ed'], ['e', 'Book', 'ing'], ['Ber', 'lin'], ['on', 'ly'], ['rag', 'ged'], ['rag', 'ged'], ['im', 'por', 'tant']], 8, 4, 7, 1, 2, False], ['McDonald', 'only', 'Berlin', 'hyphen-', 'ated', 'eBooking', 'Berlin', 'only', 'ragged', 'ragged', 'impor-', 'tant']), ('capitalised word protected', [[['the'], ['Mc', 'Don', 'ald']], 10, 2, 4, 2, 2, False], ['the', 'McDonald']), ('zone exactly met', [[['the'], ['ty', 'po', 'gra', 'phy']], 10, 6, 4, 2, 2, False], ['the typo-', 'graphy']), ('control layout', [[['the'], ['in', 'ter', 'na', 'tion', 'al'], ['on', 'ly'], ['ty', 'po', 'gra', 'phy'], ['set'], ['hy', 'phen', 'at', 'ed'], ['of']], 11, 3, 4, 2, 2, False], ['the inter-', 'national', 'only typo-', 'graphy set', 'hyphenated', 'of']), ('control layout', [[['a'], ['rag', 'ged'], ['of'], ['of'], ['the'], ['of'], ['on', 'ly'], ['Mc', 'Don', 'ald'], ['the']], 11, 3, 7, 2, 2, False], ['a ragged of', 'of the of', 'only', 'McDonald', 'the'])]]
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:
Slack is overstated by one on lines that already hold text.

Input [words as syllables, width, zone, min word length, min chars before, min chars after, hyphenate capitals]. Greedy fill. A word that does not fit may be hyphenated only if the slack left on the line (width minus current text minus the separating space) is at least the zone, the word has at least min-word characters, and (capitals allowed or its first letter is not uppercase). Pick the latest syllable boundary with head >= min before and tail >= min after chars whose head plus "-" fits. Return lines.

split: open-access