
def line_break(text, width):
    words = text.split()
    count = len(words)
    slack = [[0] * count for i in range(count)] # count x count array
    for i in range(count):
        slack[i][i] = width - len(words[i]) # line i is broken at i: slack is rest of line
        for j in range(i + 1, count):
            # line i is broken at j: slack is previous - whitespace - word len
            slack[i][j] = slack[i][j - 1] - len(words[j]) - 1

    minima = [0] + [10 ** 20] * count
    breaks = [0] * count

    # compute ideal breaking point
    for j in range(count):
        i = j
        while i >= 0:
            if slack[i][j] < 0:
                # terrible; line is too wide
                cost = 10**10
            else:
                cost = minima[i] + slack[i][j] ** 2

            if minima[j + 1] > cost:
                minima[j + 1] = cost
                breaks[j] = i

            i -= 1

    lines = []
    j = count
    while j > 0:
        i = breaks[j - 1]
        lines.append(' '.join(words[i:j]))
        j = i

    lines.reverse()

    return '\n'.join(lines)


def rewrap(text):
    text = text.strip()
    paragraphs = [par.strip() for par in text.split('\n\n')]
    new_pars = []
    for par in paragraphs:
        raw_text = par.replace('\n', ' ')
        new_text = line_break(raw_text, width=56)
        new_pars.append(new_text)
    return '\n\n'.join(new_pars)

new_txt = rewrap("""
Als ik een video over de logistic map zie, dan zwelt mijn penis op. Eerst
is het een gebruikelijke erectie, maar al snel hebben we het over een halve
meter, dan een meter, en voor je het weet is de lengte van mijn penis mijn
lichaamslengte gepasseerd. Als het in het filmpje over de
Feigenbaumconstante gaat begint mijn gigantische joekeloekes de hele ruimte
te vullen. Ik verlies bewustzijn omdat al het bloed in mijn lichaam naar de
monsterpenis gaat. Terwijl ik flauwval kan ik alleen maar denken aan hoe

The quick brown fox jumps over the lazy dog""")

print(new_txt)
