You are responsible for building a new freeway. However, it leads through mountainous lands and therefore needs numerous bridges and tunnels. The freeway itself should stay at a single level.
You are given a rough ASCII description of how the mountains look like on standard input, such as the following:
/\ / \ /\ / \ /\ / \/ \ / \ / \ /\ / \/ \ / \ _ / \ /\ / \ \ / \ / \ / \ /\ \ / \ / \/ \/ \ \ / \ / \/ \/
_ in the first column marks the beginning and the level of the road.
The mountain range is contiguous and if the last segment will be a bridge,
it has an even number of characters.
You may assume that a line of input is never longer than 100 characters and that there are no more than 15 lines. Every line has the same length which is possibly space-padded at the end. The road never starts within a mountain or as a bridge. The first thing following the road tile in the input is either a slope down or up.
Output is the same mountain range, except that there is now a road where previously was only its beginning. Output is given on standard output.
For this there are numerous rules:
The road must start at the place indicated in the input and remain the same level throughout. To make it easier, we have prepared a number of premade road tiles which look like this:
_______ \ / \/\/
The road must extend to the far end of the mountain range (i.e. the length of the input lines dictates how far the road goes).
Tunnels must be drilled whenever a mountain is where the road needs to go. Tunnels go straight through the mountain and leave holes at the start and the end (i.e. a tunnel replaces the slope of the mountain with a closing parenthesis at its start and an opening parenthesis at its end).
Tunnels leave, well, a tunnel in the mountain which usually has a ceiling. Our premade road tiles luckily can be used to reinforce the ceiling so that the tunnel does not collapse (the line above the tunnel has to use
_to reinforce the tunnel):
/\ / \ /____\ ___)______(__ \/ \/
The tunnel does not need to be reinforced when the mountain isn't high enough above it. It sounds weird to me as well but I have been told the premade road tiles are strong enough to hold even when spaced out in that case (no tunnel ceiling is drawn when there is a slope directly above the tunnel):
/\ /\/__\ ___)______(__ \/ \/
Bridges are needed whenever the road needs to cross a chasm. For short bridges the premade road tiles are strong enough but still need a bit support at the beginning and end of the bridge (the first downward slope under the bridge and the last upward slope are replaced by
Yso that there is a support beam for the bridge):
_____ Y Y \/
Longer bridges need additional support. A long bridge is one that has more than six unsupported road tiles in a row. Long bridges need a pillar in their center. Pillars are easily built with our prebuilt pillar beams which look like this:
|. Each pillar needs two of those and they extend down to the bottom of the chasm:
_____________________________ Y || Y \ /\ || /\ /\/ \ /\ / \||/ \ /\/ \/ \/ \/ \/
Since the input only represents a portion of the whole mountain range the freeway needs to be built through, it may end abruptly in the middle of a bridge or tunnel. The long bridge rule still applies for the final segment and you can assume that the first part beyond the given input supports the bridge again.
Following the above rules we get the following for our input:
/\ / \ /\ / \ /\ / \/ \ / \ / \ /\ /____\/______________\ /__\ _________)______________________(________)(____)____(______ Y Y Y Y Y Y Y YY \ / \ / \/ \/ \ \ / \ / \/ \/
Shortest code by character count wins. Trailing whitespace in the lines is ignored for validation.
Sample input 1
_ \ /\ \ / \ \ / \ \ /\ / \ /\ / \ /\ / \ / \ /\ /\/ \ / \ / \ / \/ \ / \/ \/\ / \/ \ / \ / \/ \ / \/ \/
Sample output 1
____________________________________________________________ Y || YY || \ || / \ || \ || / \ || \ || /\ / \ || /\ / \ /\ || / \ / \ /\ |/\/ \ / \ / \ || / \/ \ / \/ \/\ / \/ \ || / \ / \/ \||/ \/ \/
Sample input 2
/\ /\ / \/ \ / /\ / \ /\ / /\ / \ / \/ \ / / \ / \/\ / \ /\ / / \/ \ / \ / \ / _/ \ / \/ \/ \ / \ /\/ \ /\ / \/ \ / \ / \/
Sample output 2
/\ /\ / \/ \ / /\ / \ /\ / /\ / \ / \/ \ / / \ / \/\ / \ /\ / /____\/________\ /__________________\ /__\ /____ _)________________(__________________)____________________()____()_____ Y || Y \ || /\/ \ /\ || / \/ \|| / \| / \/
Trailing spaces in the output are ignored for comparison with the reference solutions.
Shortest code wins, as is customary in golf. In case of a tie, the earlier solution wins.
There are two tests scripts, containing identical test cases:
Invocation is in both cases:
<test script> <my program> [arguments],
./test ruby bridges.rb or
This task was part of a golf contest held at my university during 2011-W24. The scores and languages of our contestants were as follows:
- 304 – Perl
- 343 – C
- 375 – C
- 648 – Python
Our own solution (thanks to Ventero) was
- 262 – Ruby