Problem p03017 (python)

Fixed submission:
#!/usr/bin/env python
"""<https://github.com/cheran-senthil/PyRival>"""
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
else:
    _str = str
    str = lambda x=b"": x if type(x) is bytes else _str(x).encode()


def main():
    n, a, b, c, d = read_int_array()
    a -= 1
    b -= 1
    c -= 1
    d -= 1
    s = read() + b'###'
    if s[d] == '#' or s[c] == '#':
        print('No')
    else:
        if c < d:
            while b < d:
                if b + 1 == d:
                    b += 1
                elif s[b + 2] == '.':
                    b += 2
                elif s[b + 1] == '.':
                    b += 1
                else:
                    print('No')
                    return
            while a < c:
                if a + 1 == c:
                    a += 1
                elif s[a + 2] == '.':
                    a += 2
                elif s[a + 1] == '.':
                    a += 1
                else:
                    print('No')
                    return
            print("Yes")
        else:
            while a < c:
                if a + 1 == c:
                    if a + 1 == b:
                        if b + 1 == d:
                            b += 1
                        elif s[b + 2] == '.':
                            b += 2
                        elif s[b + 1] == '.':
                            b += 1
                        else:
                            print('No')
                            return
                    a += 1
                elif s[a + 1] == '.' and b != a + 1:
                    a += 1
                elif s[a + 2] == '.' and b != a + 2:
                    a += 2
                elif a + 1 == b:
                    if s[a + 2] == '.':
                        a += 2
                    elif s[a + 3] == '.':
                        b += 2
                    else:
                        print("No")
                        return
                elif a + 2 == b:
                    if s[a + 3] == '.':
                        b += 1
                        a += 2
                    elif s[a + 4] == '.':
                        b += 2
                        a += 2
                    else:
                        print("No")
                        return
                else:
                    print('No')
                    return
            if b > d:
                print("No")
                return
            while b < d:
                if b + 1 == d:
                    b += 1
                elif s[b + 2] == '.':
                    b += 2
                elif s[b + 1] == '.':
                    b += 1
                else:
                    print('No')
                    return
            print("Yes")


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        if self.buffer.tell():
            return self.buffer.read()
        return os.read(self._fd, os.fstat(self._fd).st_size)

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", b" "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", b"\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip(b"\r\n")


def read():
    return input()


def read_array(sep=None, maxsplit=-1):
    return read().split(sep, maxsplit)


def read_int():
    return int(read())


def read_int_array(sep=None, maxsplit=-1):
    return [int(a) for a in read_array(sep, maxsplit)]


# endregion

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python
"""<https://github.com/cheran-senthil/PyRival>"""
from __future__ import division, print_function

import os
import sys
from io import BytesIO, IOBase

if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
else:
    _str = str
    str = lambda x=b"": x if type(x) is bytes else _str(x).encode()


def solve(s, a, b, c, d):
    if s[d] == '#' or s[c] == '#' or c == d:
        return False
    else:
        def move_a():
            nonlocal a, b
            if s[a + 1] == 46 and b != a + 1:
                a += 1
            elif s[a + 2] == 46 and b != a + 2:
                a += 2
            elif a + 1 == b:
                if move_b():
                    a += 1
                else:
                    return False
            elif a + 2 == b:
                if move_b():
                    a += 2
                else:
                    return False
            else:
                return False
            return True

        def move_b():
            nonlocal a, b
            if s[b + 1] == 46:
                b += 1
                return True
            if s[b + 2] == 46:
                b += 2
                return True
            return False

        if c < d:
            while b < d:
                if not move_b():
                    return False
            if b != d:
                return False
            while a < c:
                if not move_a():
                    return False
            return True
        else:
            while a < c:
                if not move_a():
                    return False
            if b > d:
                return False
            while b < d:
                if not move_b():
                    return False
            return True


def solve_slow(s, a, b, c, d):
    if a == c and b == d:
        return True
    if a > c or b > d:
        return False
    if s[a + 1] == 46 and a + 1 != b:
        if solve_slow(s, a+1, b, c, d):
            return True
    if s[a + 2] == 46 and a + 2 != b:
        if solve_slow(s, a+2, b, c, d):
            return True
    if s[b + 1] == 46 and b + 1 != a:
        if solve_slow(s, a, b + 1, c, d):
            return True
    if s[b + 2] == 46 and b + 2 != a:
        if solve_slow(s, a, b + 2, c, d):
            return True
    return False


def main():
    n, a, b, c, d = read_int_array()
    a -= 1
    b -= 1
    c -= 1
    d -= 1
    s = read() + b'###'
    if solve_slow(s, a, b, c, d):
        print("YES")
    else:
        print("NO")
    # sl, fst = solve_slow(s, a, b, c, d), solve(s, a, b, c, d)
    # print(sl, fst)

def gen_str(opts, n):
    import random
    str = []
    for _ in range(n):
        str.append(opts[random.randrange(0, len(opts))])
    return ''.join(str)


def main2():
    import random
    import itertools
    for _ in range(1000000):
        while True:
            n = random.randint(4, 15)
            a = random.randint(0, n-3)
            b = random.randint(a+1, n-2)
            c = random.randint(a+1, n-1)
            d = random.randint(b+1, n-1)
            if not(a < b and a < c and b < d):
                continue
            s = gen_str('.#', n) + '###'
            if s[a] != '.' or s[b] != '.' or s[c] != '.' or s[d] != '.':
                continue
            sl, fst = solve_slow(s, a, b, c, d), solve(s, a, b, c, d)
            if sl != fst:
                print(sl, fst, s, a, b, c, d)
            else:
                print(s)
                sys.stdout.flush()
            break





# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        if self.buffer.tell():
            return self.buffer.read()
        return os.read(self._fd, os.fstat(self._fd).st_size)

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", b" "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", b"\n"))
    if kwargs.pop("flush", False):
        file.flush()


sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
input = lambda: sys.stdin.readline().rstrip(b"\r\n")


def read():
    return input()


def read_array(sep=None, maxsplit=-1):
    return read().split(sep, maxsplit)


def read_int():
    return int(read())


def read_int_array(sep=None, maxsplit=-1):
    return [int(a) for a in read_array(sep, maxsplit)]

# endregion

if __name__ == "__main__":
    main()