48
2
Background
In typography, rivers are visual gaps in a block of text, which occur due to coincidental alignment of spaces. These are particularly annoying since your brain seems to pick them up more easily in peripheral vision, which constantly distracts your eyes.
As an example, take the following block of text, lines broken such that the line width does not exceed 82 characters:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eismod tempor
incididunt ut labore et dolore maga aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet,
consectetur adipisicing elit, sed do eismod tempor incididunt ut labore et dolore
maga aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.
There is a river spanning six lines in the bottom right part, which I've highlighted in the following block:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eismod tempor
incididunt ut labore et dolore maga aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
officia deserunt mollit anim id est laborum. Lorem█ipsum dolor sit amet,
consectetur adipisicing elit, sed do eismod tempor█incididunt ut labore et dolore
maga aliqua. Ut enim ad minim veniam, quis nostrud█exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute█irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla█pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui█officia deserunt mollit anim id
est laborum.
We can mitigate this by choosing a slightly different column width. E.g. if we layout the same text using lines no longer than 78 characters, there is no river longer than two lines:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eismod tempor
incididunt ut labore et dolore maga aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor
sit amet, consectetur adipisicing elit, sed do eismod tempor incididunt ut
labore et dolore maga aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis
aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
Note that for the purpose of this question we're only considering monospaced fonts, such that rivers are simply vertical columns of spaces. The length of a river is the number of lines it spans.
Aside: If you're interesting in river detection in proportional fonts, there are some interesting posts around the network.
The Challenge
You're given a string of printable ASCII characters (code point 0x20 to 0x7E) - i.e. a single line. Print this text, with a line width between 70 and 90 characters (inclusive), such that the maximum length of any river in the text is minimised. If there are multiple text widths with the same (minimal) maximum river length, choose the narrower width. The above example with 78 characters is the correct output for that text.
To break lines, you should replace space characters (0x20) with line breaks, such that the resulting lines have as many characters as possible, but not more than the chosen text width. Note that the resulting line break itself is not part of that count. As an example, in the last block above, Lorem[...]tempor
contains 78 characters, which is also the text's width.
You may assume that the input will not contain consecutive spaces, and won't have leading or trailing spaces. You may also assume that no word (consecutive substring of non-spaces) will contain more than 70 characters.
You may write a program or function, taking input via STDIN, command-line argument or function argument and printing the result to STDOUT.
This is code golf, so the shortest answer (in bytes) wins.
I think in your 78 and 82 column wrap examples, the last and second-to-last lines are incorrect. In the 82 example, the last break should be between id and est, and in the 78 example it should be between in and culpa. Or am I doing something wrong? – Cristian Lupascu – 2014-11-05T13:10:45.987
@Optimizer The tie break is the text length, not the river length. – FryAmTheEggman – 2014-11-05T17:32:15.923
I guess it doesn't count as an official river, but in the example 78 characters max length, there seems to be a pretty long diagonal river in the top-ish left-ish area – markasoftware – 2014-11-06T03:33:56.097
Do we consider cases like this as continues rivers ?
– Optimizer – 2014-11-06T08:25:51.150Great challenge! Hm, next one could be about having (not purely vertical ) rivers shaping subliminal letters ;) – Tobias Kienzler – 2014-11-06T09:51:00.600
@Optimizer I don't think any of the five existing solutions considers that, and I've defined rivers as a vertical column of space characters, so no. – Martin Ender – 2014-11-06T10:38:31.473
I doubt that they are specially treating it as not a river ... I am not, but I have a fix for that – Optimizer – 2014-11-06T10:39:19.660
@Optimizer I'll leave it up to you (and any other participant). It was an oversight in the spec. – Martin Ender – 2014-11-06T10:41:32.740
That DSP.SE post has some awesome answers! – wchargin – 2014-11-07T05:31:15.317
Can I have trailing white spaces on each lines in the output ? – Optimizer – 2014-11-07T12:14:45.460
@Optimizer Sorry, no, I think the spec for breaking lines is pretty precise about replacing those spaces with line breaks. – Martin Ender – 2014-11-07T12:15:58.757