Two roads diverged in a yellow wood (part 3)

D, 348 321 312 302 bytes

import std.stdio,std.algorithm,std.regex,std.range;void h(R,U)(R s,U r){U[2]c;foreach(L;s.map!(a=>a.array.split)){U l=L.length-1;r>1?r=l^r-2:0;l?c[]+=l*L.map!sum.array[]:c;}write(c[r]>c[!r]?"right":"left");}void main(){auto s=stdin.byLineCopy.array;!s.join.match(`\d \d`)?h(s.transposed,2UL):h(s,3UL);}

Ungolfed

import std.stdio,std.algorithm,std.regex,std.range;

void h(R,U)(R s, U reverse) {
    U[2] counts;
    /* Now that all paths are up/down, every line we need to count up has
       precisely two space-delimited parts to sum up. */
    foreach (line; s.map!(a=>a.array.split)) {
        U len = line.length - 1;
        reverse > 1 ? reverse = len ^ reverse - 2 : 0;
        len ? counts[] += len * line.map!sum.array[] : counts;
    }

    /* Switch left/right as needed based on our orientation */
    write(counts[reverse] > counts[!reverse] ? "right" : "left");
}

void main() {
    /* Treat the input as a matrix of integers */
    auto s = stdin.byLineCopy.array;

    /* If moving left/right intead of up/down, transpose */
    !s.join.match(`\d \d`)?h(s.transposed,2UL):h(s,3UL);
}

Try it online!


Python 2, 304 bytes

Try it online

A=input()
S=str(A)[2:-2].replace("', '",'')
i=S.index('#')
r=len(A[0])
w=1
a=i/r
if' '<S[i-r]:w=-w;A=A[::w];a=len(A)+~a
if' '<S[i+1]:A=zip(*A[::-1]);a=i%r
if' '<S[i-1]:A=zip(*A)[::-1];a=len(A)-i%r-1
s=0
for c in' '.join(map(''.join,A[0:a])).split():s+=sum(map(int,c));s=-s
print['left','right'][::w][s>0]

This program deduces direction of roads and rotates it facing up to use my solution from part 2 of this challenge.