19
\$\begingroup\$

Background

A few months ago, the adventure of your life was just about to start. Now, in this precise moment (yeah, now), after months of suffering and hard work, you and a group of friends are standing on the top of the world. Yes, you are right, you are on the summit of Sagarmāthā.

However, things aren't going as well as you would like to. A dense fog has surrounded you and an incredibly bad-looking storm is coming as fast as it can. You didn't fix any rope on the way up, and your footprints have been covered in snow. If you want to survive (at least for today), you need to get out of there as fast as you can, but you MUST first find a way to know which face of the mountain is the one you should descend.

Luckily, you brought with you your sat phone which you modified before the trip so you are able to program and execute programs in it.

Challenge

You have been able to download to your phone the map of the mountain in an ASCII-old-fashioned-unreadable-on-the-top-of-the-world way. Your task is to decide which face of the mountain presents the easiest descent so you can increase your chances of surviving. In order to do so, you have the brilliant idea to code a program on your phone that will tell which is the easiest way down. (Disclaimer: These activities have been done by professionals. No programmer was hurt during this narration. Please, do not try this at home.)

Maps are only made of the characters / and \ (plus spaces and newlines). In any map, the summit of the mountain is always represented by

 /\ 
 \/ 

and from each of the sides (1,2,3 or 4) of the summit you will always find a "possible" way down the mountain.

1 /\ 2
3 \/ 4

The routes are always presented in the next way:

                      \
  Steep-> /          /
           /        / <-Flat
            /      \
      Flat-> \    \
              /  \ <-Steep
               /\
               \/

where each new character is either one place to the left/right of its predecessor. The meaning of each character is:

  • If the slash/backlash is parallel to its summit side -> counts as a 'steep' part.
  • If the slash/backslash is perpendicular to its summit side -> counts as a 'flat' part.

*For further reference see graphic above.

Note: The sides can have different lengths and the characters which constitute the summit also count as part of their side. In case of a draw, you can choose any of them.

Standard loopholes are disallowed.

Input

A string representing the map of the mountain or a plain text file containing the same info.

Either

C:\....\file.txt

or

                  \
      /          /
       /        /
        /      \
         \    \
          /  \
           /\
           \/
          \  /
         \    \
        \      \
       \        \
      /          /

as a string are valid inputs.

Output

As output, you should produce either a file in plain text or by stdout an ASCII profile representation of the side with the smallest average steepness using _ for flat parts and / for steep parts along with the average steepness of the side (number of "/")/(total chars).

Output example for map above:

       /
   ___/
  /
  AS:0.5

The format is not important as long as you have the profile and average steepness.

Scoring

What? Do you want a better reward than saving your and your friend's lives and being the first programmer who has ever programmed on the top of the world? Okay... this is code golf, so shortest program in bytes wins.

Test cases

Input:

                  \
      /          /
       /        /
        /      \
         \    \
          /  \
           /\
           \/
          \  /
         \    \
        \      \
       \        \
      /          /

Output:

       /
   ___/
  /
  AS=0.5

Input:

                  /
      \          /
       /        /
        \      /
         \    /
          /  /
           /\
           \/
          \  /
         \    \
        \      \
       \        \
      /          /
     / 
    / 

Output:

______/
AS=0.143 (1/7)

Input:

           /        \
            \      \
             /    /
              /  \
               /\
               \/
              \  /
             \    /
            \      /
           /        \

Output:

        /        
       /
      /       
    _/
    AS=0.8
\$\endgroup\$
  • \$\begingroup\$ Based on the examples, it looks like the profile shows the path in bottom to top direction if you read it from left to right? Seems somewhat unusual, since we're moving top to bottom, but no problem if it's clearly defined this way. \$\endgroup\$ – Reto Koradi Oct 6 '15 at 15:24
  • 6
    \$\begingroup\$ @RetoKoradi you are right. I don't know why I did it this way... You know, at that heigth its hard to keep your thoughts in order... \$\endgroup\$ – Ioannes Oct 6 '15 at 15:32
  • \$\begingroup\$ Should the output also include the number of the slope that is the least steep (1,2,3 or 4)? As it stands, you know one of them is definetly the winner but not which one. \$\endgroup\$ – Vic Oct 6 '15 at 16:23
  • 1
    \$\begingroup\$ *No programmer was hurt during this narration. * I care. +1 \$\endgroup\$ – edc65 Oct 6 '15 at 18:53
  • 3
    \$\begingroup\$ I like that you used Sagarmāthā :) \$\endgroup\$ – Beta Decay Oct 6 '15 at 20:14
4
\$\begingroup\$

JavaScript (ES6), 303

Test running the snippet blow in an EcmaScript compliant browser - surely Firefox, probably Chrome. Using template strings, arrow functions.

// Golfed, no indentenation, all newlines are significant

f=s=>(s=`
${s}
`.split`
`,s.map((r,i)=>~(q=r.search(/\/\\/))&&(y=i,x=q),x=y=0),z=[],[0,2,0,2].map((d,i)=>{t=x+i%2,u=y+i/2|0,b=s[u][t];for(p=[''],n=l=0;(c=s[u][t])>' ';++l,t+=d-1,u+=(i&2)-1)c==b?p.push(p[n++].replace(/./g,' ',w='/')):w='_',p=p.map((r,i)=>(i<n?' ':w)+r);z=z[0]<(p[0]=n/l)?z:p}),z.join`
`)

// Less golfed

U=s=>(
  s=(`\n${s}\n`).split`\n`,
  x = y = 0,
  s.map((r,i)=>~(q=r.search(/\/\\/))&&(y=i,x=q)),
  z=[],
  [0,2,0,2].map((d,i) => {
    t = x+i%2,
    u = y+i/2|0,
    b = s[u][t];
    for(p=[''], n=l=0; (c=s[u][t])>' '; ++l, t += d-1, u +=(i&2)-1)
      c == b
        ? p.push(p[n++].replace(/./g,' ',w='/'))
        : w='_',
      p = p.map((r,i) => (i<n?' ':w)+r);
    z = z[0]<(p[0]=n/l)?z:p
  }),
  z.join`\n`
)

// TEST
// redirect console into the snippet body
console.log=x=>O.innerHTML+=x+'\n'

maps=[ // as javascript string literals, each baskslasch has to be repeated
`                  \\
      /          /
       /        /
        /      \\
         \\    \\
          /  \\
           /\\
           \\/
          \\  /
         \\    \\
        \\      \\
       \\        \\
      /          /`,
`                  /
      \\          /
       /        /
        \\      /
         \\    /
          /  /
           /\\
           \\/
          \\  /
         \\    \\
        \\      \\
       \\        \\
      /          /
     / 
    / `,
`           /        \\
            \\      \\
             /    /
              /  \\
               /\\
               \\/
              \\  /
             \\    /
            \\      /
           /        \\`]

maps.forEach(m=>console.log(m + '\n'+ f(m) +'\n'))
<pre id=O></pre>

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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