Bridges and Tunnels

Perl, 210 195 194 193 chars

update

Same ideas, but applied very differently, for the most part.

Run with perl -p0 (4 chars counted for the switches).

($o,$c,$r,$b)=/((.+\n)?(_.+\n)(.+\n)?)/;/\n/;$n=$-[0];$c=~s{/ }{/_}g;$r=~y{ /\\}{_)(};map{s{\\(   +)  \1(/|\n)}{\\$1||$1$2}g,y{\\/}{Y}}$b;s/\Q$o/$c$r$b/;1while(s/_ /__/||s/(\|.{$n}) /$1|/s) 

This also requires a change to the bash test script, so as not to over-quote the args:

-   got=$("$cmd" "$args")
+   got=$("$cmd" $args)

BTW, I really appreciate the test scripts, @Joey.

commented:

#-p : implicitly read input and print after transformations
#-0 : "slurp mode": don't break lines

# grab the roadway line, along with the lines above and below for ceiling and buttresses
# also grab the whole match in $o for replacing later
($o,$c,$r,$b)=/((.+\n)?(_.+\n)(.+\n)?)/;

# compute line length
/\n/;$n=$-[0];

# start ceilings
$c=~s{/ }{/_}g;

# build the road and tunnels
$r=~y{ /\\}{_)(};

# use map to avoid repeating $b =~
map{
  # insert the pillar tops
  s{\\(   +)  \1(/|\n)}{\\$1||$1$2}g,
  # and the buttresses
  y{\\/}{Y}
} $b;

# put those 3 (or 2) lines back into the original
s/\Q$o/$c$r$b/;

# extend ceiling tiles to the right and pillars downward
1while(s/_ /__/||s/(\|.{$n}) /$1|/s)

edits:

  • replace "{3,}" with 3 literal spaces and the + quantifier, to save another character
  • use the 1while(...) form, where I can omit the semicolon at the end of the script

original (see history for commented version)

@a=map{($r=$n),y{ /\\}{_)(}if/_/;$n++;$_}<>;if($r){while($a[$r-1]=~s{(/[ _]*) }{$1_}){}}map{s{(^|\\)( {3,})  \2(/|$)}{$1$2||$2$3}g;y{\\/}{YY}}$a[$r+1];$_=join'',@a;/\n/;$n=$-[0];while(s/(\|.{$n}) /$1|/s){}print

I deliberately didn't look at @Howard's Perl solution until mine was working, but I was able to improve my golfing by looking afterward.

In particular, the regexps for tunnel ceilings and extending pillars helped. Nicely done, Howard.

Conversely, my alternative for getting line length, using the implicit $_ for the print, leaving off the final semicolon, and removing newlines could shorten Howard's to 222 characters.


Perl, 234 characters

$_=join'',<>;$l=index($_,"\n");
($w)=/(_[^\n]*)/;$w=~y/ \\\//_()/;s/_[^\n]*/$w/e;
while(s/(\/[ _]*) (?=[^\n]*\n_)/$1_/||s/(_.{$l})[\\\/]/$1Y/s){}while(s/(\n( |Y *Y)*)Y( {3,})  \3(Y| ?\n)/\1Y\3||\3\4/||s/(?<=\|.{$l}) /|/s){}
print $_;

This is a perl regex-only solution. It passes all given test cases.

The identical but more readable version shows the steps taken to obtain the result:

$_=join'',<>;                    # read the input
$l=index($_,"\n");               # determine length of line

($w)=/(_[^\n]*)/;                # extract the line starting with _
$w=~y/ \\\//_()/;                # and build road there (i.e. replace all chars in line)
s/_[^\n]*/$w/e;                  # put road back into string

while(s/(\/[ _]*) (?=[^\n]*\n_)/$1_/||s/(_.{$l})[\\\/]/$1Y/s){}
                                 # build ceiling of tunnels
                                 # build Y supports directly below the road

while(s/(\n( |Y *Y)*)Y( {3,})  \3(Y| ?\n)/\1Y\3||\3\4/||s/(?<=\|.{$l}) /|/s){}
                                 # build center support in middle of bridges
                                 # and extend them down to the ground

print $_;                        # print result

C++, 662 622 598 chars, 4 lines

#include<iostream>
#include<string>
#define s for(
#define v g[t][f]
int t,f,r,q,m=-1,w,b,e,h,j,x,y,l,c=18;int main(){std::string g[19];s;r<19;h=c)getline(std::cin,g[r++]);s;c>=0;g[c--].push_back(0))(q=g[c].size())&&g[c][0]-95?0:r=c;s;c<q;y=0){x=g[r][++c];x==47?x=41,w=e=1:++y;x==92?x=40,w=0:++y;t=r-1;f=c;y>1?x=95,w?r&&v==32?v=95:0:++b:0;c-q?0:e=1;t++;s v=x;m<0;)g[h].find(47)<g[h--].npos?m=h:0;t=r+1;if(r<m){(y=v)==47?y=89,e=1:0;v=y-92?y:89;if(e){y=e=0;if((b/=2)>4){j=l=r+1;s;!y;t=j,f=c-b+1,v==32?v=124,++j:0)t=l,y=f=c-b,v==32?v=124,l++,y=0:0;}b=0;}}}s f=q,t=0;t<=m;std::cout<<g[t++]<<'\n')v=32;}

This should be it, I've tested this out with the powershell script so it should be ok...

Edit 1

Replaced all char constants with numbers, removed successive calls to std::cout + a few other minor changes.

Edit 2

last edit, just to get it less than 600. Took out g[t][f] as a #define and moved a few things around.