32
\$\begingroup\$

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.

Input

You are given a rough ASCII description of how the mountains look like on standard input, such as the following:

                         /\                                
                        /  \                               
                   /\  /    \                              
            /\    /  \/      \                             
           /  \  /            \                  /\        
          /    \/              \                /  \       
_        /                      \        /\    /    \      
 \      /                        \      /  \  /      \  /\ 
  \    /                          \    /    \/        \/  \
   \  /                            \  /                    
    \/                              \/                     

The _ 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

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:

  1. 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: _:

    _______
     \    /
      \/\/
    
  2. 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).

  3. 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).

  4. 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):

          /\
         /  \
        /____\
    ___)______(__
     \/        \/
    
  5. 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):

           /\
        /\/__\
    ___)______(__
     \/        \/
    
  6. 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 Y so that there is a support beam for the bridge):

    _____
     Y  Y
      \/
    
  7. 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
      \        /\ || /\      /\/ 
       \  /\  /  \||/  \  /\/    
        \/  \/    \/    \/       
    
  8. 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                                  
                    \      ||    /\/                                   
                     \  /\ ||   /                                      
                      \/  \||  /                                       
                           \| /                                        
                            \/                                         

Note

Trailing spaces in the output are ignored for comparison with the reference solutions.

Winning condition

Shortest code wins, as is customary in golf. In case of a tie, the earlier solution wins.

Test cases

There are two tests scripts, containing identical test cases:

Invocation is in both cases: <test script> <my program> [arguments], e.g. ./test ruby bridges.rb or ./test.ps1 ./bridges.exe.

Another note

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
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Note: I updated the test scripts where some test cases contained an unintentional empty line at the top. Should be fixed now. \$\endgroup\$
    – Joey
    Jul 16, 2011 at 16:26

7 Answers 7

8
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ For the bash test script give thanks to Ventero. I just took what he created and bashed at it until it worked somewhat ;-) \$\endgroup\$
    – Joey
    Jul 19, 2011 at 8:50
6
\$\begingroup\$

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
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Yikes. That looks rather evil in all parts ;-). Why do Perl people always have to solve everything with regexes? :-þ \$\endgroup\$
    – Joey
    Jul 15, 2011 at 21:07
  • \$\begingroup\$ Because regices are the weapons of a true Perl hacker :ð \$\endgroup\$
    – FUZxxl
    Jul 15, 2011 at 21:49
  • 2
    \$\begingroup\$ A perl hacker (I called my self such in years bygone) thinks quite the other way around, Joey: if a problem can be solved with regexes, why bother with anything else? This really is an ideal problem for regular expressions, I think: it's that or implement a backtracking state machine yourself. \$\endgroup\$
    – boothby
    Jul 15, 2011 at 21:53
  • \$\begingroup\$ That wasn't meant entirely serious. My own reference solution initially used lots of regexes as well. \$\endgroup\$
    – Joey
    Jul 16, 2011 at 8:42
4
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ Yep, I had a few test cases wrong, but that's my own fault. Looks good now :-) \$\endgroup\$
    – Joey
    Jul 16, 2011 at 16:26
  • \$\begingroup\$ Would it help to #define std::? \$\endgroup\$
    – FUZxxl
    Jul 17, 2011 at 11:59
  • \$\begingroup\$ @FUZxxl, I don't think so, I only use "std::" 4 times so that's 20 characters, if I used a define i would need 2 chars at each of those places(eg "a cout" using a as std::) and the define would take more than 12 chars, "using namespace std" isn't worth it either. \$\endgroup\$ Jul 17, 2011 at 15:42
3
\$\begingroup\$

Ruby, 382 356 353 characters

I thought to give a non-regex solution a shot.

m=$<.read;n,r,s,$e,t=(/\n/=~m)+1,/^_/=~m,m.size,(u,d=47,92),0;def j y;$e.member? y;end;(n-1).times{|i|x=r+i;c=m[x];j(c)||(m[x]=95;a,b=x-n,x+n;t<41||a<0||m[a]>32||m[a]=95;b>s||!j(m[b])||(m[b]<d||(p=(/\/|\n/=~m[b..s])/2+b;p-b<4||(w=[f=false,f];p.step(s,n){|p|2.times{|i|(w[i]||=j m[i=p+i])||m[i]='|'}}));m[b]='Y'));c!=u||t=m[x]=41;c!=d||t=m[x]=40};print m

The biggest space saver here is to use conditional || operator instead of if - elsif - end. So instead of writing if(a) lorem_ipsum end I've written !a||lopem_ipsum. Larger blocks can included using parenthesis !a||(block). If the condition includes && operators then they must negated by using parenthesis and a ! or by using De Morgan's laws.

And here's the same with more words

# read the STDIN to string
mountains = $<.read
line_length,road_level,size,$slopes,in_tunnel =
        (/\n/ =~ mountains) + 1, # Fint the first new line
        /^_/ =~ mountains, # Find the index of the road
        mountains.size, 
        (slope_up,slope_down=47,92),
        0

def is_slope y;$slopes.member? y;end

# For each index in the road line
(line_length - 1).times { |i|
  curindex = road_level + i
  curchar = mountains[curindex]

  # If not a slope then
  (is_slope(curchar))|| (

    # mark road as underscore
    mountains[curindex] = 95
    above, below = curindex - line_length, curindex + line_length

    # add roof to tunnel if in tunnel and the mountain
    # is high enough
    in_tunnel<41||above<0||mountains[above]>32||mountains[above]=95

    # If there's a slope character below the road
    below>size||!is_slope(mountains[below])||(

      # if there's a downward slop - start a bridge
      mountains[below]<slope_down||(

          # If bridge is longer than 6 characters
          # Add support to the middle
          support_pos = (/\/|\n/=~mountains[below..size])/2+below
          support_pos-below<4||(
            # Init collision detection
            collision_detected=[f=false,f]
            # Build supports as long as 
            # There is mountains below
            support_pos.step(size,line_length) { |support_pos|

               # Add two parallel suppports
               2.times { |i|

               # Add support if the bottom of the 
               # gap has not been reached
               (collision_detected[i]||=is_slope(mountains[i=support_pos+i]))||mountains[i] = '|'
               }
            }
          )
      )

      # Add support to the beginning and to the end
      # of the bridge
      mountains[below] = 'Y'
    )
  )

  # Add tunnel entrance and exit
  curchar!=slope_up||in_tunnel=mountains[curindex]=41
  curchar!=slope_down||in_tunnel=mountains[curindex]=40
}

print mountains
\$\endgroup\$
1
  • \$\begingroup\$ The tests are ok - I had just screwed up the test script while pasting it ... awkward. \$\endgroup\$ Jul 22, 2011 at 6:00
2
\$\begingroup\$

Scala, 462 characters

object B extends App{var r="""(?s)((?:.*\n)?)((?:.*?\n)?)(_.*?\n)(.*?\n)(.*)""".r
var r(a,b,c,d,e)=io.Source.stdin.mkString
var s=a+b.replaceAll("(?<=/ {0,99}) (?=.*\\\\)","_")+c.replaceAll("\\\\","(").replaceAll("/",")").replaceAll(" ","_")+d.replaceAll("\\\\( {3,99})  \\1/","Y$1||$1Y").replaceAll("\\\\( {3,99})  \\1\\n","Y$1||$1\n").replaceAll("\\\\( {0,9})/","Y$1Y")+e
for(i<-1 to 15)s=s.replaceAll("(?s)(?<=\\|.{"+s.split("\n")(0).size+"}) ","|")
print(s)}

Not particularly competitive, but I've learnt a lot about regular expressions today. :-)

\$\endgroup\$
2
\$\begingroup\$

Erlang, 1182 characters

-module(b).
-export([t/0]).
t()->r([]).
r(R)->case io:get_line("")of eof->{_,Q}=lists:foldl(fun([$ |L],{$_,Q})->{$ ,[[$X|L]|Q]};([C|_]=L,{_,Q})->{C,[L|Q]}end,{$ ,[]},R),{_,P}=lists:foldl(fun s/2,{s,[]},lists:flatten(Q)),io:put_chars(lists:reverse(P));L->r([L|R])end.
s($X,{s,R})->{{r,s},[$ |R]};s($/,{{r,s},R})->{{r,r},[$/|R]};s($\\,{{r,r},R})->{{r,s},[$\\|R]};s($\n,{{r,_},R})->{u,[$\n|R]};s(_,{{r,r}=S,R})->{S,[$_|R]};s($_,{_,R})->{u,[$_|R]};s($\n,{u,R})->{{g,i,1,1,[]},[$\n|R]};s($\\,{u=S,R})->{S,[$(|R]};s($/,{u=S,R})->{S,[$)|R]};s(_,{u=S,R})->{S,[$_|R]};s($\\,{{g,i,X,_L,T},R})->{{g,o,X+1,X,[$Y|T]},R};s($/,{{g,o,X,L,T},R})->a(o,X,L),{{g,i,X+1,X,[$Y|T]},R};s($\n,{{g,S,X,L,T},R})->a(S,X,L),{{d,1},[$\n,f(T,X)|R]};s(C,{{g,S,X,L,T},R})->{{g,S,X+1,L,[C|T]},R};s($\n,{{d,_},R})->{{d,1},[$\n|R]};s($ ,{{d,X},R})->{{d,X+1},[case get(X)of y->$|;_->$ end|R]};s($/,{{d,X},R})->put(X,n),{{d,X+1},[$/|R]};s($\\,{{d,X},R})->put(X,n),{{d,X+1},[$\\|R]};s($\n=N,{S,R})->{S,[N|R]};s(X,{S,R})->{S,[X|R]}.
a(o,X,L)when X-L-1>6->Pos=round((X-L-1)/2+L),put(Pos,y),put(Pos+1,y);a(_,_,_)->v.
f(L,M)->{_,R}=lists:foldl(fun(C,{X,R})->case get(M-X)of y->{X+1,[$||R]};_->{X+1,[C|R]}end end,{1,[]},L),R.

Not competitive at all. To be honest, I was simply interested in producing some nice ascii art, not so much in the golfing. Note that this does not use regular expressions, instead, I wrote the state machine myself. This will also need the overquoting fix in the shell script, mentioned in the perl solution above.

\$\endgroup\$
1
\$\begingroup\$

Powershell, 300 295 287 bytes

-8 bytes fix peeked from DCharness'es answer.

param($s)filter e{$r,$e=$_-split'#';$e|%{$r=$r-replace($_-split';')};$r}
$x=$s-match'(.+
)?(_.+)(
.+)?'
$o,$c,$r,$b=$Matches[0..3]
for($s=$s|% *ce $o(("$c#/ ;/_"|e)+("$r# ;_#/;)#\\;("|e)+($b+'#\\(   +)  \1;\$1||$1#/|\\;Y'|e));$s-ne($x="$s#_ ;__#(?s)(?<=\|.{$($r|% Le*)}) ;|"|e)){$s=$x}$s

Ungolfed test script:

$f = {

param($s)
$x=$s-match'(.+
)?(_.+)(
.+)?'
$o,$c,$r,$b=$Matches[0..3]
$c=$c-replace'/ ','/_'                  # we assume that road does not start as a tunnel
$r=$r-replace' ','_'
$r=$r-replace'/',')'
$r=$r-replace'\\','('
$b=$b-replace'\\(   +)  \1','\$1||$1'   # we assume that road does not start as a bridge under a chasm
$b=$b-replace'/|\\','Y'
$s=$s.replace($o,"$c$r$b")              # string method replace (not operator) does not handle regexp
for(;$s-ne($x=$s-replace'_ ','__'-replace"(?s)(?<=\|.{$($r.Length)}) ",'|')){$s=$x}
$s

}

$pass = 0
$count = 0

@(
# Test 2
,(2,@'
    /\                  /\                  /\        
   /  \                /  \                /  \       
  /____\              /____\              /____\      
_)______(____________)______(____________)______(_____
         Y    ||    Y        Y    ||    Y        Y    
          \   ||   /          \   ||   /          \   
           \  ||  /            \  ||  /            \  
            \ || /              \ || /              \ 
             \||/                \||/                \
              \/                  \/                  
'@,@'
    /\                  /\                  /\        
   /  \                /  \                /  \       
  /    \              /    \              /    \      
_/      \            /      \            /      \     
         \          /        \          /        \    
          \        /          \        /          \   
           \      /            \      /            \  
            \    /              \    /              \ 
             \  /                \  /                \
              \/                  \/                  
'@)

# Test 1
,(1,@'
____________________________________________________________
 Y             ||             YY             ||             
  \            ||            /  \            ||             
   \           ||           /    \           ||             
    \          ||    /\    /      \          ||  /\        /
     \    /\   ||   /  \  /        \      /\ |/\/  \      / 
      \  /  \  ||  /    \/          \    /  \/      \/\  /  
       \/    \ || /                  \  /              \/   
              \||/                    \/                    
               \/                                           
'@,@'
_                                                           
 \                            /\                            
  \                          /  \                           
   \                        /    \                          
    \                /\    /      \              /\        /
     \    /\        /  \  /        \      /\  /\/  \      / 
      \  /  \      /    \/          \    /  \/      \/\  /  
       \/    \    /                  \  /              \/   
              \  /                    \/                    
               \/                                           
'@)

# Test 3
,(3,@'
    /\                 
   /  \                
  /____\/\/\/\/\/\/\/\ 
_)____________________(
'@,@'
    /\                 
   /  \                
  /    \/\/\/\/\/\/\/\ 
_/                    \
'@)

# Test 4
,(4,@'
___________________________________________________
 Y           ||           YY          ||          Y
  \          ||          /  \         |/\        / 
   \         ||         /    \        /  \      /  
    \    /\  ||    /\  /      \      /    \    /   
     \  /  \ ||   /  \/        \    /      \  /    
      \/    \||  /              \  /        \/     
             \| /                \/                
              \/                                   
'@,@'
_                                                  
 \                        /\                      /
  \                      /  \          /\        / 
   \                    /    \        /  \      /  
    \    /\        /\  /      \      /    \    /   
     \  /  \      /  \/        \    /      \  /    
      \/    \    /              \  /        \/     
             \  /                \/                
              \/                                   
'@)

# Test 5
,(5,@'
                                                      /
                                                     / 
                                                    /__
___________________________________________________)___
 Y           ||           YY          ||          Y    
  \          ||          /  \         |/\        /     
   \         ||         /    \        /  \      /      
    \    /\  ||    /\  /      \      /    \    /       
     \  /  \ ||   /  \/        \    /      \  /        
      \/    \||  /              \  /        \/         
             \| /                \/                    
              \/                                       
'@,@'
                                                      /
                                                     / 
                                                    /  
_                                                  /   
 \                        /\                      /    
  \                      /  \          /\        /     
   \                    /    \        /  \      /      
    \    /\        /\  /      \      /    \    /       
     \  /  \      /  \/        \    /      \  /        
      \/    \    /              \  /        \/         
             \  /                \/                    
              \/                                       
'@)

# Test 6
,(6,@'
_______________________________)
 YYYYYYYYYYYYYYYYYYYYYYYYYYYYYY 
'@,@'
_                              /
 \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ 
'@)

# Test 7
,(7,@'
_________________________________)
 Y              ||              Y 
  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/  
'@,@'
_                                /
 \                              / 
  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/  
'@)

# Test 8
,(8,@'
      /\    /\    /\    /\    /\    /\ 
_)(__)__(__)__(__)__(__)__(__)__(__)__(
   YY    YY    YY    YY    YY    YY    
'@,@'
      /\    /\    /\    /\    /\    /\ 
_/\  /  \  /  \  /  \  /  \  /  \  /  \
   \/    \/    \/    \/    \/    \/    
'@)

# Test 9
,(9,@'
  /\/\/\/\/\/\/\/\ 
_)________________(
'@,@'
  /\/\/\/\/\/\/\/\ 
_/                \
'@)

# Test 10
,(10,@'
      /\                                                            /\                              
     /  \                                                          /  \                             
    /    \                                                        /    \                            
   /      \                                                      /      \                           
  /________\                                                    /________\/\/\/\/\                  
_)__________(__________________________________________________)__________________(______)(_________
             Y                       ||                       Y                    Y    Y  Y   ||   
              \                      ||    /\                /                      \  /    \  ||  /
               \    /\               ||   /  \              /                        \/      \ || / 
                \  /  \              ||  /    \            /                                  \||/  
                 \/    \             || /      \          /                                    \/   
                        \        /\  ||/        \        /                                          
                         \      /  \ |/          \      /                                           
                          \    /    \/            \    /                                            
                           \/\/                    \/\/                                             
'@,@'
      /\                                                            /\                              
     /  \                                                          /  \                             
    /    \                                                        /    \                            
   /      \                                                      /      \                           
  /        \                                                    /        \/\/\/\/\                  
_/          \                                                  /                  \      /\         
             \                                                /                    \    /  \        
              \                            /\                /                      \  /    \      /
               \    /\                    /  \              /                        \/      \    / 
                \  /  \                  /    \            /                                  \  /  
                 \/    \                /      \          /                                    \/   
                        \        /\    /        \        /                                          
                         \      /  \  /          \      /                                           
                          \    /    \/            \    /                                            
                           \/\/                    \/\/                                             
'@)

# Test 11
,(11,@'
__________________________________________________
 Y     ||     YY     ||     YY         ||         
  \    ||    /  \    ||    /  \        /\         
   \   ||   /    \   ||   /    \      /  \    /\  
    \  ||  /      \  ||  /      \    /    \  /  \/
     \ || /        \ || /        \  /      \/     
      \||/          \||/          \/              
       \/            \/                           
'@,@'
_                                                 
 \            /\            /\                    
  \          /  \          /  \        /\         
   \        /    \        /    \      /  \    /\  
    \      /      \      /      \    /    \  /  \/
     \    /        \    /        \  /      \/     
      \  /          \  /          \/              
       \/            \/                           
'@)

# Test 12
,(12,@'
                                           /\  /\                      
                                          /  \/  \                    /
           /\                            /        \  /\              / 
    /\    /  \                          /          \/  \            /  
   /  \  /    \/\                      /                \    /\    /   
  /____\/________\                    /__________________\  /__\  /____
_)________________(__________________)____________________()____()_____
                   Y       ||       Y                                  
                    \      ||    /\/                                   
                     \  /\ ||   /                                      
                      \/  \||  /                                       
                           \| /                                        
                            \/                                         
'@,@'
                                           /\  /\                      
                                          /  \/  \                    /
           /\                            /        \  /\              / 
    /\    /  \                          /          \/  \            /  
   /  \  /    \/\                      /                \    /\    /   
  /    \/        \                    /                  \  /  \  /    
_/                \                  /                    \/    \/     
                   \                /                                  
                    \            /\/                                   
                     \  /\      /                                      
                      \/  \    /                                       
                           \  /                                        
                            \/                                         
'@)

# Test 13
,(13,@'
_____________)(____________)(_____________________
 Y    ||    Y  Y    ||    Y  Y   ||   YY    ||    
  \   ||   /    \   ||   /    \  ||  /  \   |/\  /
   \  ||  /      \  ||  /      \ || /    \  /  \/ 
    \ || /        \ || /        \||/      \/      
     \||/          \||/          \/               
      \/            \/                            
'@,@'
_            /\            /\                     
 \          /  \          /  \        /\          
  \        /    \        /    \      /  \    /\  /
   \      /      \      /      \    /    \  /  \/ 
    \    /        \    /        \  /      \/      
     \  /          \  /          \/               
      \/            \/                            
'@)

# Test 14
,(14,@'
            /\            /\                    
___________)__(__________)__(________)(_________
 Y   ||   Y    Y   ||   Y    Y      Y  Y    YY  
  \  ||  /      \  ||  /      \    /    \  /  \/
   \ || /        \ || /        \  /      \/     
    \||/          \||/          \/              
     \/            \/                           
'@,@'
            /\            /\                    
_          /  \          /  \        /\         
 \        /    \        /    \      /  \    /\  
  \      /      \      /      \    /    \  /  \/
   \    /        \    /        \  /      \/     
    \  /          \  /          \/              
     \/            \/                           
'@)

# Test 15
,(15,@'
           /\            /\                    
          /__\          /__\        /\         
_________)____(________)____(______)__(____)(__
 Y      Y      Y      Y      Y    Y    Y  Y  YY
  \    /        \    /        \  /      \/     
   \  /          \  /          \/              
    \/            \/                           
'@,@'
           /\            /\                    
          /  \          /  \        /\         
_        /    \        /    \      /  \    /\  
 \      /      \      /      \    /    \  /  \/
  \    /        \    /        \  /      \/     
   \  /          \  /          \/              
    \/            \/                           
'@)

# Test 16
,(16,@'
          /\            /\                    
         /  \          /  \        /\         
        /____\        /____\      /__\    /\  
_______)______(______)______(____)____(__)__()
 Y    Y        Y    Y        Y  Y      YY     
  \  /          \  /          \/              
   \/            \/                           
'@,@'
          /\            /\                    
         /  \          /  \        /\         
        /    \        /    \      /  \    /\  
_      /      \      /      \    /    \  /  \/
 \    /        \    /        \  /      \/     
  \  /          \  /          \/              
   \/            \/                           
'@)

# Test 17
,(17,@'
         /\            /\                    
        /  \          /  \        /\         
       /    \        /    \      /  \    /\  
      /______\      /______\    /____\  /__\/
_____)________(____)________(__)______()_____
 Y  Y          Y  Y          YY              
  \/            \/                           
'@,@'
         /\            /\                    
        /  \          /  \        /\         
       /    \        /    \      /  \    /\  
      /      \      /      \    /    \  /  \/
_    /        \    /        \  /      \/     
 \  /          \  /          \/              
  \/            \/                           
'@)

# Test 18
,(18,@'
        /\            /\                    
       /  \          /  \        /\         
      /    \        /    \      /  \    /\  
     /      \      /      \    /    \  /  \/
    /________\    /________\  /______\/_____
___)__________(__)__________()______________
 YY            YY                           
'@,@'
        /\            /\                    
       /  \          /  \        /\         
      /    \        /    \      /  \    /\  
     /      \      /      \    /    \  /  \/
    /        \    /        \  /      \/     
_  /          \  /          \/              
 \/            \/                           
'@)

# Test 19
,(19,@'
       /\            /\                    
      /  \          /  \        /\         
     /    \        /    \      /  \    /\  
    /      \      /      \    /    \  /  \/
   /        \    /        \  /      \/     
  /__________\  /__________\/______________
_)____________()___________________________
'@,@'
       /\            /\                    
      /  \          /  \        /\         
     /    \        /    \      /  \    /\  
    /      \      /      \    /    \  /  \/
   /        \    /        \  /      \/     
  /          \  /          \/              
_/            \/                           
'@)

# Test 20
,(20,@'
__________________________________________________
 Y     ||     YY     ||     YY         ||         
  \    ||    /  \    ||    /  \        /\         
   \   ||   /    \   ||   /    \      /  \    /\  
    \  ||  /      \  ||  /      \    /    \  /  \ 
     \ || /        \ || /        \  /      \/    \
      \||/          \||/          \/              
       \/            \/                           
'@,@'
_                                                 
 \            /\            /\                    
  \          /  \          /  \        /\         
   \        /    \        /    \      /  \    /\  
    \      /      \      /      \    /    \  /  \ 
     \    /        \    /        \  /      \/    \
      \  /          \  /          \/              
       \/            \/                           
'@)

# Test 21
,(21,@'
      /\            /\                    
     /  \          /  \        /\         
    /    \        /    \      /  \    /\  
   /      \      /      \    /    \  /  \/
  /________\    /________\  /______\/_____
_)__________(__)__________()______________
             YY                           
'@,@'
      /\            /\                    
     /  \          /  \        /\         
    /    \        /    \      /  \    /\  
   /      \      /      \    /    \  /  \/
  /        \    /        \  /      \/     
_/          \  /          \/              
             \/                           
'@)

# Test 22
,(22,@'
     /\            /\                    
    /  \          /  \        /\         
   /    \        /    \      /  \    /\  
  /______\      /______\    /____\  /__\/
_)________(____)________(__)______()_____
           Y  Y          YY              
            \/                           
'@,@'
     /\            /\                    
    /  \          /  \        /\         
   /    \        /    \      /  \    /\  
  /      \      /      \    /    \  /  \/
_/        \    /        \  /      \/     
           \  /          \/              
            \/                           
'@)

# Test 23
,(23,@'
                         /\                                 
                        /  \                                
                   /\  /    \                               
            /\    /  \/      \                              
           /  \  /            \                  /\         
          /____\/______________\                /__\        
_________)______________________(________)(____)____(_______
 Y      Y                        Y      Y  Y  Y      Y  YY  
  \    /                          \    /    \/        \/  \/
   \  /                            \  /                     
    \/                              \/                      
'@,@'
                         /\                                 
                        /  \                                
                   /\  /    \                               
            /\    /  \/      \                              
           /  \  /            \                  /\         
          /    \/              \                /  \        
_        /                      \        /\    /    \       
 \      /                        \      /  \  /      \  /\  
  \    /                          \    /    \/        \/  \/
   \  /                            \  /                     
    \/                              \/                      
'@)

# Test 24
,(24,@'
    /\            /\                    
   /  \          /  \        /\         
  /____\        /____\      /__\    /\  
_)______(______)______(____)____(__)__()
         Y    Y        Y  Y      YY     
          \  /          \/              
           \/                           
'@,@'
    /\            /\                    
   /  \          /  \        /\         
  /    \        /    \      /  \    /\  
_/      \      /      \    /    \  /  \/
         \    /        \  /      \/     
          \  /          \/              
           \/                           
'@)

# Test 25
,(25,@'
   /\            /\                    
  /__\          /__\        /\         
_)____(________)____(______)__(____)(__
       Y      Y      Y    Y    Y  Y  YY
        \    /        \  /      \/     
         \  /          \/              
          \/                           
'@,@'
   /\            /\                    
  /  \          /  \        /\         
_/    \        /    \      /  \    /\  
       \      /      \    /    \  /  \/
        \    /        \  /      \/     
         \  /          \/              
          \/                           
'@)

# Test 26
,(26,@'
  /\            /\                    
_)__(__________)__(________)(_________
     Y   ||   Y    Y      Y  Y    YY  
      \  ||  /      \    /    \  /  \/
       \ || /        \  /      \/     
        \||/          \/              
         \/                           
'@,@'
  /\            /\                    
_/  \          /  \        /\         
     \        /    \      /  \    /\  
      \      /      \    /    \  /  \/
       \    /        \  /      \/     
        \  /          \/              
         \/                           
'@)

# Test 27
,(27,@'
_)(____________)(_____________________
   Y    ||    Y  Y   ||   YY    ||    
    \   ||   /    \  ||  /  \   |/\  /
     \  ||  /      \ || /    \  /  \/ 
      \ || /        \||/      \/      
       \||/          \/               
        \/                            
'@,@'
_/\            /\                     
   \          /  \        /\          
    \        /    \      /  \    /\  /
     \      /      \    /    \  /  \/ 
      \    /        \  /      \/      
       \  /          \/               
        \/                            
'@)

# Test 28
,(28,@'
____________________________________
 Y     ||     YY         ||         
  \    ||    /  \        /\         
   \   ||   /    \      /  \    /\  
    \  ||  /      \    /    \  /  \/
     \ || /        \  /      \/     
      \||/          \/              
       \/                           
'@,@'
_                                   
 \            /\                    
  \          /  \        /\         
   \        /    \      /  \    /\  
    \      /      \    /    \  /  \/
     \    /        \  /      \/     
      \  /          \/              
       \/                           
'@)

# Test 29
,(29,@'
_____________________________
 Y            ||            Y
  \        /\ || /\      /\/ 
   \  /\  /  \||/  \  /\/    
    \/  \/    \/    \/       
'@,@'
_                            
 \                          /
  \        /\    /\      /\/ 
   \  /\  /  \  /  \  /\/    
    \/  \/    \/    \/       
'@)

# Test 30
,(30,@'
_____
 Y  Y
  \/ 
'@,@'
_    
 \  /
  \/ 
'@)

# Test 31
,(31,@'
       /\    
    /\/__\   
___)______(__
 YY        YY
'@,@'
       /\    
    /\/  \   
_  /      \  
 \/        \/
'@)

# Test 32
,(32,@'
      /\     
     /  \    
    /____\   
___)______(__
 YY        YY
'@,@'
      /\     
     /  \    
    /    \   
_  /      \  
 \/        \/
'@)

# Test 33
,(33,@'
_______
 Y    Y
  \/\/ 
'@,@'
_      
 \    /
  \/\/ 
'@)

# Test 34
,(34,@'
_)()()()()()()()()()()()()()()()()()(
'@,@'
_/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
'@)

) | % {
    $num, $expected, $s = $_
    $result = &$f $s
    $passed=$result-eq$expected
    "$num : $passed"
    # $result           # uncomment this line to display results

    $pass+=$passed
    $count+=1
}

"$pass/$count passed."

Output:

2 : True
1 : True
3 : True
4 : True
5 : True
6 : True
7 : True
8 : True
9 : True
10 : True
11 : True
12 : True
13 : True
14 : True
15 : True
16 : True
17 : True
18 : True
19 : True
20 : True
21 : True
22 : True
23 : True
24 : True
25 : True
26 : True
27 : True
28 : True
29 : True
30 : True
31 : True
32 : True
33 : True
34 : True
34/34 passed.

Explanation comics:

Start here:
    /\                  /\                  /\        
   /  \                /  \                /  \       
  /    \              /    \              /    \      
_/      \            /      \            /      \     
         \          /        \          /        \    
          \        /          \        /          \   
           \      /            \      /            \  
            \    /              \    /              \ 
             \  /                \  /                \
              \/                  \/                  

Match and zoom it!
  /    \              /    \              /    \      $ceiling \
_/      \            /      \            /      \     $road    |  $outline
         \          /        \          /        \    $bottom  /

Start ceiling:
  /_   \              /_   \              /_   \      $ceiling \
_/      \            /      \            /      \     $road    |  $outline
         \          /        \          /        \    $bottom  /

Dig tunnels, build a road:
  /_   \              /_   \              /_   \      $ceiling \
_)______(____________)______(____________)______(_____$road    |  $outline
         \          /        \          /        \    $bottom  /

Attach to the road the top parts of pillars:
  /_   \              /_   \              /_   \      $ceiling \
_)______(____________)______(____________)______(_____$road    |  $outline
         \    ||    /        \    ||    /        \    $bottom  /

The road need support:
  /____\              /____\              /____\      $ceiling \
_)______(____________)______(____________)______(_____$road    |  $outline
         Y    ||    Y        Y    ||    Y        Y    $bottom  /

Put the road back into mountainous lands:
    /\                  /\                  /\        
   /  \                /  \                /  \       
  /_   \              /_   \              /_   \      $ceiling \
_)______(____________)______(____________)______(_____$road    |  $outline
         Y    ||    Y        Y    ||    Y        Y    $bottom  /
          \        /          \        /          \   
           \      /            \      /            \  
            \    /              \    /              \ 
             \  /                \  /                \
              \/                  \/                  

Build ceilings and build down pillars:
    /\                  /\                  /\        
   /  \                /  \                /  \       
  /__  \              /__  \              /__  \      
_)______(____________)______(____________)______(_____
         Y    ||    Y        Y    ||    Y        Y    
          \   ||   /          \   ||   /          \   
           \      /            \      /            \  
            \    /              \    /              \ 
             \  /                \  /                \
              \/                  \/                  

A few iterations later...
    /\                  /\                  /\        
   /  \                /  \                /  \       
  /____\              /____\              /____\      
_)______(____________)______(____________)______(_____
         Y    ||    Y        Y    ||    Y        Y    
          \   ||   /          \   ||   /          \   
           \  ||  /            \  ||  /            \  
            \ || /              \ || /              \ 
             \||/                \||/                \
              \/                  \/                  

Thanks. But our princess is in another castle!
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.