Problem p03017 (python)

Fixed submission:
N, A, B, C, D = map(int, input().split())
S = input()

A -= 1
B -= 1
C -= 1
D -= 1

if '##' in S[A:C] or '##' in S[B:D]:
    print("No")
    exit()
if C > D:
    if  '...' not in S[B-1:D+2]:
        print("No")
        exit()
print("Yes")

Buggy submission (verdict: Runtime Error):
N, A, B, C, D = map(int, input().split())
S = list(input())

A -= 1
B -= 1
C -= 1
D -= 1

if S[A:C] in '##' or S[B:D] in '##':
    print("No")
    exit()
if C > D:
    if S[B:D] in '...':
        print("Yes")
        exit()
    else:
        print("No")
        exit()
print("Yes")

Buggy submission (verdict: Runtime Error):
N, A, B, C, D = map(int, input().split())
S = list(input())

A -= 1
B -= 1
C -= 1
D -= 1

if S[C] == '#' or S[D] == '#':
    print("No")
    exit()

if C < B:
    stone = 0
    road = 0
    for i in range(A + 1, C):
        if S[i] == '#':
            stone += 1
            road = 0
            if stone > 1:
                print("No")
                exit()
        else:
            road += 1
            stone = 0
    stone = 0
    road = 0
    for i in range(B + 1, D):
        if S[i] == '#':
            stone += 1
            road = 0
            if stone > 1:
                print("No")
                exit()
        else:
            road += 1
            stone = 0
    print("Yes")
    exit()

stone = 0
road = 0
c = False
l = [-1]
ans = False

for i in range(B - 1, D + 2):
    if S[i] == '#':
        stone += 1
        road = 0
        if c:
            l.append(i - 1)
            c = False
        if stone > 1:
            print("No")
            exit()
    else:
        road += 1
        stone = 0
        if road > 2:
            c = True
if C < D:
    S = S[:B] + ['.'] + S[B + 1:]
    stone = 0
    road = 0
    for i in range(A + 1, C):
        if S[i] == '#':
            stone += 1
            road = 0
            if stone > 1:
                print("No")
                exit()
        else:
            road += 1
            stone = 0
    ans = True
else:
    stone = 0
    road = 0
    check = True
    # B放置でゴールできるか
    S = S[:B] + ['#'] + S[B + 1:]
    stone = 0
    road = 0
    for i in range(A + 1, C):
        if S[i] == '#':
            stone += 1
            road = 0
            if stone > 1:
                check = False
        else:
            road += 1
            stone = 0
    S = S[:B] + ['.'] + S[B + 1:]
    if check:
        ans = True
    # Bがゴールしているとゴールできるか
    stone = 0
    road = 0
    check = True
    S = S[:D] + ['#'] + S[D + 1:]
    stone = 0
    road = 0
    for i in range(A + 1, C):
        if S[i] == '#':
            stone += 1
            road = 0
            if stone > 1:
                check = False
        else:
            road += 1
            stone = 0
    if check:
        ans = True
    S = S[:D] + ['.'] + S[D + 1:]

if not ans:
    stone = 0
    road = 0
    if l[-1] >= 0:
        for i in range(A + 1, C):
            if S[i] == '#':
                stone += 1
                road = 0
                if stone > 1:
                    print("No")
                    exit()
            else:
                road += 1
                stone = 0
        ans = True

if ans:
    print("Yes")
else:
    print("No")

Buggy submission (verdict: Runtime Error):
N, A, B, C, D = map(int, input().split())
S = list(input())

A -= 1
B -= 1
C -= 1
D -= 1

if S[C] == '#' or S[D] == '#':
    print("No")
    exit()

stone = 0
road = 0
c = False
l = []

for i in range(B + 1, D):
    if S[i] == '#':
        stone += 1
        road = 0
        if c:
            l.append(i - 1)
            c = False
        if stone > 1:
            print("No")
            exit()
    else:
        road += 1
        stone = 0
        if road > 2:
            c = True

stone = 0
road = 0

count = 0

# B放置でゴールできるか
if S[B - 1] == '#' or S[B + 1] == '#':
    count += 1
# Bがゴールしているとゴールできるか
if D < C:
    if S[D - 1] == '#' or S[D + 1] == '#':
        count += 1

if count > 1:
    if len(l) == 0:
        print("No")
        exit()

# Bが避難しているときBはAを通したあとゴールできるか
if C < D:
    if l[-1] < C:
        if S[C - 1] == '#' or S[C + 1] == '#':
            count += 1

if count > 2:
    print("No")
    exit()

print("Yes")