21. Text wrapping
The printing house had never slept easily, but that winter it seemed to live on less rest than ever. Ink fumes clung to its rafters; the press arms pounded through the night like restless hearts. Beneath the din, Karl muttered curses over crooked margins, and Liina stood near the compositor’s table, her manuscripts spread like wounded birds across the wood.
„They keep breaking at odd places,“ Karl growled, stabbing a thumb at a proof sheet. „A line too long, a word spilling over. We’re not printing German treatises here—we’ve no paper for waste!“
Liina’s voice, though soft, carried steel. „A poem doesn’t bend to your margins, Karl. It has its own rhythm.“
„Rhythm won’t fit into twelve centimeters of column!“ he barked, wiping his hands on his apron. „If you want your verses read, they must fit the page. Find me someone who can measure breath and line alike.“
At that, Mihkel stepped from the corner, dusted with fine soot from the press. „Perhaps I can,“ he said quietly.
Liina looked up, brow furrowed, torn between relief and resistance. „You would wrap words in numbers now?“
„Not in numbers,“ Mihkel replied, a faint smile touching his lips. „In measure. Measure is not a prison—it is what lets words breathe evenly.“
That night, in the lamplit backroom where paper scraps lay like snowdrifts, Mihkel unrolled a strip of tape. He marked the wrapping length in quiet unary strokes, then copied one of Liina’s poems beside it.
The Logic Mill stirred once more. Its gears clicked with a new rhythm—not arithmetic now, but cadence. Each turn of the crank decided where breath would pause, where meaning would fall into silence before beginning again.
Liina watched in stillness, her shawl drawn close. The faint metallic heartbeat of the Mill filled the room, as if the machine itself were learning to read aloud.
Outside, the snow fell thick and soundless. Inside, words began to find their place.
On the input tape, you’ll get the line wrapping length as a unary number and the text (separated by :).
Your task is to wrap the text so that each line is at most the given length.
You must use greedy left-to-right wrapping (i.e., keep appending the next word if it still fits on the line).
Mark line breaks using +.
The words are never broken; line breaks can only occur between words.
Each line consists of English letters (a-z), - is used as a word delimiter.
There are no consecutive hyphens and no leading/trailing hyphens.
The line length includes the in-line hyphens (-) but not line breaks (+).
The wrapping length is at least as long as the longest word in the text, but no longer than the whole text.
Examples
If the input tape is |||||||:hello-world-how-are-you, your output tape should be hello+world+how-are+you (each line is max 7 characters).
If the input tape is |||||:hello-world-how-are-you, your output tape should be hello+world+how+are+you (each line is max 5 characters).
If the input tape is |||||||:hey-you, your output tape should be hey-you (whole text fits into 1 line of 7 characters).