Skip to main content
2 of 4
Include sleeker code
VisualMelon
  • 3.9k
  • 18
  • 39

C# - 388 433 bytes

Quick job making good use of LINQ.

using C=System.Console;using System.Linq;class P{static void Main(){var D=("\n"+C.In.ReadToEnd()).Split('\n');int i=D.Length,I,J,k,M=D.Max(m=>m.Length),d,c=0;var E=D.Select(l=>l.PadRight(M+1).ToCharArray()).ToList();for(;i-->0;)for(J=M;J-->0;)if(E[I=i][J]==43)for(d=0,c++;(k=E[I+=(d-2)%2][J+=(d-1)%2])>32;)if(k<44){E[I][J]=' ';d=d%2<1?E[I-1][J]<99?3:1:E[I][J+1]!=45?0:2;}C.WriteLine(c);}}

First it reads in the input, and makes it nice and rectangular, with a " " border so we don't have to do any bounds checking. Then it looks back through the rectangle, so it always hits a right-bottom corner. When it hits one of these, it directs itself up, following any +s it meets, and clearing them as it goes (with a space). It stops following when it meets a space. Tested on the five given examples.

using C=System.Console;
using System.Linq;

class P
{
    // this is far too LINQy
    static void Main()
    {
        // 43  + 00101011
        // 45  - 00101101
        // 124 | 01111100
        
        // read in
        var D=("\n"+C.In.ReadToEnd()).Split('\n'); // say no to verticle bounds checking
        
        int i=D.Length,I,J,k,
        M=D.Max(m=>m.Length), // M is max length of a line
        d, // d is direction of travel (1 == vertical?, 2 == down/right?)
        c=0; // c is loop count
        
        // max all the lines the same length
        var E=D.Select(l=>l.PadRight(M+1).ToCharArray()).ToList(); // say no to horizontal bounds checking
        
        // consume +s
        for(;i-->0;) // for each line (bottom to top)
            for(J=M;J-->0;) // for each char (right to left)
                if(E[I=i][J]==43) // right-most lower-most +
                    for(d=0,c++; // go up, increment counter
                        (k=E[I+=(d-2)%2][J+=(d-1)%2])>32 // move I and J, then check we havn't hit a space
                        ;)
                        if(k<44) // +
                        {
                            E[I][J]=' '; // toodles
                            
                            d=
                                d%2<1? // currently horizontal, must go vertical
                                    E[I-1][J]<99?3 // can't go up, must go down
                                    :1 // can go up, go up
                                : // currently verticle, must go horizontal
                                    E[I][J+1]!=45?0 // can't go right, must go left
                                    :2 // can go right, go right
                                ;
                        }
        
        // output result
        C.WriteLine(c);
    }
}
VisualMelon
  • 3.9k
  • 18
  • 39