"""
brainf. interpreter in python

(Program Start)	
    char array[infinitely large size] = {0};
    char *ptr=array;

/-----------------------------------\    
|   Bf command |    C command       | 
|-----------------------------------|
|      >	   |    ++ptr;          | 
|      <	   |    --ptr;          |
|      +	   |    ++*ptr;         |
|      -	   |    --*ptr;         |
|      .	   |    putchar(*ptr);  |
|      ,	   |    *ptr=getchar(); |
|      [	   |    while (*ptr) {  |
|      ]	   |    }               |
\-----------------------------------/
"""

import sys


class UnmatchedBrackets(Exception):
    def __init__(self, *args):
        super().__init__(*args)

class ArgumentConflict(Exception):
    def __init__(self, *args):
        super().__init__(*args)


class InvalidCharacter(Exception):
    def __init__(self, *args):
        super().__init__(*args)


class Parser:
    def __init__(self, pointer=0, tape=None, tape_infinite_expansion=False, tape_length=None,
                 allow_other_chars=True, cyclic_cell=False, tape_bit=8):
        """
        Main brainf parser
        :param pointer: initial pointer position on the tape
        :param tape: you can pass a custom tape (list with numbers >= 0)
        :param tape_infinite_expansion: if True, tape will will expand infinitely to the right 
        :param allow_other_chars: If false, will raise an error if it encounters other chars
        :param cyclic_cell: If true, 255 + 1 -> 0 and 0 - 1 -> 255 (8-bit)
        """
        self.pointer = pointer
        self.infinite_expansion = tape_infinite_expansion
        if tape_length and tape_infinite_expansion:
            raise ArgumentConflict("Cannot specify tape length if infinite expansion is enabled")

        self.tape_length = tape_length if tape_length else 3*(10**4)
        self.allow_other_chars = allow_other_chars  # TODO: implement that
        self.cyclic_cell = cyclic_cell
        self.max_tape_value = 2 ** tape_bit - 1  # 0 counts as a number, so for 8-bit 256 numbers: [0; 255]

        self.commands = {
            '>': self.increment_pointer,
            '<': self.decrement_pointer,
            '+': self.increment_value,
            '-': self.decrement_value,
            ',': self.read_input,
            '.': self.write_output,
            '[': None,  # we don't run any of these, but they are  #
            ']': None,  # required for checking character validity #
        }

        if tape:
            self.tape = tape
        else:
            self.tape = [0 for _ in range(self.tape_length)]
            
    def increment_pointer(self):
        """equivalent of >"""
        if not self.infinite_expansion:
            if self.pointer > len(self.tape):
                raise ValueError('Segmentation fault')
        self.pointer += 1
    
    def decrement_pointer(self):
        """equivalent of <"""
        if self.pointer < 0:
            raise ValueError('Segmentation fault')
        self.pointer -= 1
    
    def increment_value(self):
        """equivalent of +"""
        if self.cyclic_cell:
            self.tape[self.pointer] = 0
        else:
            if self.tape[self.pointer] == self.max_tape_value:
                raise ValueError(f'Cannot store values > {self.max_tape_value}')
            else:
                self.tape[self.pointer] += 1
    
    def decrement_value(self):
        """equivalent of -"""
        if self.cyclic_cell:
            self.tape[self.pointer] = self.max_tape_value
        else:
            if self.tape[self.pointer] == 0:
                raise ValueError('Cannot store negative values')
            else:
                self.tape[self.pointer] -= 1

    def read_input(self):
        """equivalent of ,"""
        c = ord(sys.stdin.read(1))
        if c != 26:
            self.tape[self.pointer] = c

    def write_output(self):
        """equivalent of ."""
        sys.stdout.write(chr(self.tape[self.pointer]))

    def run(self, code):
        """execute code"""
        if self.allow_other_chars:
            code = ''.join([i for i in code if i in self.commands])
        else:
            if any([c not in self.commands for c in code]):
                raise InvalidCharacter('Invalid character found in code')
        opening_brackets = []
        code_index = 0
        while code_index < len(code):
            # TODO: check the validity of the input (matched brackets)
            if code[code_index] == '[':
                opening_brackets.append(code_index)
            elif code[code_index] == ']':
                if not opening_brackets:
                    raise UnmatchedBrackets('Closing bracket has no opening match')
                curr_opening_bracket = opening_brackets[-1]
                if self.tape[self.pointer] == 0:
                    opening_brackets.pop()
                else:
                    code_index = curr_opening_bracket  # it will be incremented at the end of the loop anyways
            else:
                self.commands[code[code_index]]()
            code_index += 1

if __name__ == '__main__':
    p = Parser(allow_other_chars=False)
    p.run('ABC')

3334ffb7238e637c489bbb8cb5b0765ee24936b3
<mask token>


class InvalidCharacter(Exception):
    <mask token>


class Parser:

    def __init__(self, pointer=0, tape=None, tape_infinite_expansion=False,
        tape_length=None, allow_other_chars=True, cyclic_cell=False, tape_bit=8
        ):
        """
        Main brainf parser
        :param pointer: initial pointer position on the tape
        :param tape: you can pass a custom tape (list with numbers >= 0)
        :param tape_infinite_expansion: if True, tape will will expand infinitely to the right 
        :param allow_other_chars: If false, will raise an error if it encounters other chars
        :param cyclic_cell: If true, 255 + 1 -> 0 and 0 - 1 -> 255 (8-bit)
        """
        self.pointer = pointer
        self.infinite_expansion = tape_infinite_expansion
        if tape_length and tape_infinite_expansion:
            raise ArgumentConflict(
                'Cannot specify tape length if infinite expansion is enabled')
        self.tape_length = tape_length if tape_length else 3 * 10 ** 4
        self.allow_other_chars = allow_other_chars
        self.cyclic_cell = cyclic_cell
        self.max_tape_value = 2 ** tape_bit - 1
        self.commands = {'>': self.increment_pointer, '<': self.
            decrement_pointer, '+': self.increment_value, '-': self.
            decrement_value, ',': self.read_input, '.': self.write_output,
            '[': None, ']': None}
        if tape:
            self.tape = tape
        else:
            self.tape = [(0) for _ in range(self.tape_length)]

    def increment_pointer(self):
        """equivalent of >"""
        if not self.infinite_expansion:
            if self.pointer > len(self.tape):
                raise ValueError('Segmentation fault')
        self.pointer += 1

    def decrement_pointer(self):
        """equivalent of <"""
        if self.pointer < 0:
            raise ValueError('Segmentation fault')
        self.pointer -= 1

    def increment_value(self):
        """equivalent of +"""
        if self.cyclic_cell:
            self.tape[self.pointer] = 0
        elif self.tape[self.pointer] == self.max_tape_value:
            raise ValueError(f'Cannot store values > {self.max_tape_value}')
        else:
            self.tape[self.pointer] += 1

    def decrement_value(self):
        """equivalent of -"""
        if self.cyclic_cell:
            self.tape[self.pointer] = self.max_tape_value
        elif self.tape[self.pointer] == 0:
            raise ValueError('Cannot store negative values')
        else:
            self.tape[self.pointer] -= 1

    def read_input(self):
        """equivalent of ,"""
        c = ord(sys.stdin.read(1))
        if c != 26:
            self.tape[self.pointer] = c

    def write_output(self):
        """equivalent of ."""
        sys.stdout.write(chr(self.tape[self.pointer]))

    def run(self, code):
        """execute code"""
        if self.allow_other_chars:
            code = ''.join([i for i in code if i in self.commands])
        elif any([(c not in self.commands) for c in code]):
            raise InvalidCharacter('Invalid character found in code')
        opening_brackets = []
        code_index = 0
        while code_index < len(code):
            if code[code_index] == '[':
                opening_brackets.append(code_index)
            elif code[code_index] == ']':
                if not opening_brackets:
                    raise UnmatchedBrackets(
                        'Closing bracket has no opening match')
                curr_opening_bracket = opening_brackets[-1]
                if self.tape[self.pointer] == 0:
                    opening_brackets.pop()
                else:
                    code_index = curr_opening_bracket
            else:
                self.commands[code[code_index]]()
            code_index += 1


<mask token>

<mask token>


class UnmatchedBrackets(Exception):
    <mask token>


class ArgumentConflict(Exception):

    def __init__(self, *args):
        super().__init__(*args)


class InvalidCharacter(Exception):

    def __init__(self, *args):
        super().__init__(*args)


class Parser:

    def __init__(self, pointer=0, tape=None, tape_infinite_expansion=False,
        tape_length=None, allow_other_chars=True, cyclic_cell=False, tape_bit=8
        ):
        """
        Main brainf parser
        :param pointer: initial pointer position on the tape
        :param tape: you can pass a custom tape (list with numbers >= 0)
        :param tape_infinite_expansion: if True, tape will will expand infinitely to the right 
        :param allow_other_chars: If false, will raise an error if it encounters other chars
        :param cyclic_cell: If true, 255 + 1 -> 0 and 0 - 1 -> 255 (8-bit)
        """
        self.pointer = pointer
        self.infinite_expansion = tape_infinite_expansion
        if tape_length and tape_infinite_expansion:
            raise ArgumentConflict(
                'Cannot specify tape length if infinite expansion is enabled')
        self.tape_length = tape_length if tape_length else 3 * 10 ** 4
        self.allow_other_chars = allow_other_chars
        self.cyclic_cell = cyclic_cell
        self.max_tape_value = 2 ** tape_bit - 1
        self.commands = {'>': self.increment_pointer, '<': self.
            decrement_pointer, '+': self.increment_value, '-': self.
            decrement_value, ',': self.read_input, '.': self.write_output,
            '[': None, ']': None}
        if tape:
            self.tape = tape
        else:
            self.tape = [(0) for _ in range(self.tape_length)]

    def increment_pointer(self):
        """equivalent of >"""
        if not self.infinite_expansion:
            if self.pointer > len(self.tape):
                raise ValueError('Segmentation fault')
        self.pointer += 1

    def decrement_pointer(self):
        """equivalent of <"""
        if self.pointer < 0:
            raise ValueError('Segmentation fault')
        self.pointer -= 1

    def increment_value(self):
        """equivalent of +"""
        if self.cyclic_cell:
            self.tape[self.pointer] = 0
        elif self.tape[self.pointer] == self.max_tape_value:
            raise ValueError(f'Cannot store values > {self.max_tape_value}')
        else:
            self.tape[self.pointer] += 1

    def decrement_value(self):
        """equivalent of -"""
        if self.cyclic_cell:
            self.tape[self.pointer] = self.max_tape_value
        elif self.tape[self.pointer] == 0:
            raise ValueError('Cannot store negative values')
        else:
            self.tape[self.pointer] -= 1

    def read_input(self):
        """equivalent of ,"""
        c = ord(sys.stdin.read(1))
        if c != 26:
            self.tape[self.pointer] = c

    def write_output(self):
        """equivalent of ."""
        sys.stdout.write(chr(self.tape[self.pointer]))

    def run(self, code):
        """execute code"""
        if self.allow_other_chars:
            code = ''.join([i for i in code if i in self.commands])
        elif any([(c not in self.commands) for c in code]):
            raise InvalidCharacter('Invalid character found in code')
        opening_brackets = []
        code_index = 0
        while code_index < len(code):
            if code[code_index] == '[':
                opening_brackets.append(code_index)
            elif code[code_index] == ']':
                if not opening_brackets:
                    raise UnmatchedBrackets(
                        'Closing bracket has no opening match')
                curr_opening_bracket = opening_brackets[-1]
                if self.tape[self.pointer] == 0:
                    opening_brackets.pop()
                else:
                    code_index = curr_opening_bracket
            else:
                self.commands[code[code_index]]()
            code_index += 1


<mask token>

<mask token>


class UnmatchedBrackets(Exception):

    def __init__(self, *args):
        super().__init__(*args)


class ArgumentConflict(Exception):

    def __init__(self, *args):
        super().__init__(*args)


class InvalidCharacter(Exception):

    def __init__(self, *args):
        super().__init__(*args)


class Parser:

    def __init__(self, pointer=0, tape=None, tape_infinite_expansion=False,
        tape_length=None, allow_other_chars=True, cyclic_cell=False, tape_bit=8
        ):
        """
        Main brainf parser
        :param pointer: initial pointer position on the tape
        :param tape: you can pass a custom tape (list with numbers >= 0)
        :param tape_infinite_expansion: if True, tape will will expand infinitely to the right 
        :param allow_other_chars: If false, will raise an error if it encounters other chars
        :param cyclic_cell: If true, 255 + 1 -> 0 and 0 - 1 -> 255 (8-bit)
        """
        self.pointer = pointer
        self.infinite_expansion = tape_infinite_expansion
        if tape_length and tape_infinite_expansion:
            raise ArgumentConflict(
                'Cannot specify tape length if infinite expansion is enabled')
        self.tape_length = tape_length if tape_length else 3 * 10 ** 4
        self.allow_other_chars = allow_other_chars
        self.cyclic_cell = cyclic_cell
        self.max_tape_value = 2 ** tape_bit - 1
        self.commands = {'>': self.increment_pointer, '<': self.
            decrement_pointer, '+': self.increment_value, '-': self.
            decrement_value, ',': self.read_input, '.': self.write_output,
            '[': None, ']': None}
        if tape:
            self.tape = tape
        else:
            self.tape = [(0) for _ in range(self.tape_length)]

    def increment_pointer(self):
        """equivalent of >"""
        if not self.infinite_expansion:
            if self.pointer > len(self.tape):
                raise ValueError('Segmentation fault')
        self.pointer += 1

    def decrement_pointer(self):
        """equivalent of <"""
        if self.pointer < 0:
            raise ValueError('Segmentation fault')
        self.pointer -= 1

    def increment_value(self):
        """equivalent of +"""
        if self.cyclic_cell:
            self.tape[self.pointer] = 0
        elif self.tape[self.pointer] == self.max_tape_value:
            raise ValueError(f'Cannot store values > {self.max_tape_value}')
        else:
            self.tape[self.pointer] += 1

    def decrement_value(self):
        """equivalent of -"""
        if self.cyclic_cell:
            self.tape[self.pointer] = self.max_tape_value
        elif self.tape[self.pointer] == 0:
            raise ValueError('Cannot store negative values')
        else:
            self.tape[self.pointer] -= 1

    def read_input(self):
        """equivalent of ,"""
        c = ord(sys.stdin.read(1))
        if c != 26:
            self.tape[self.pointer] = c

    def write_output(self):
        """equivalent of ."""
        sys.stdout.write(chr(self.tape[self.pointer]))

    def run(self, code):
        """execute code"""
        if self.allow_other_chars:
            code = ''.join([i for i in code if i in self.commands])
        elif any([(c not in self.commands) for c in code]):
            raise InvalidCharacter('Invalid character found in code')
        opening_brackets = []
        code_index = 0
        while code_index < len(code):
            if code[code_index] == '[':
                opening_brackets.append(code_index)
            elif code[code_index] == ']':
                if not opening_brackets:
                    raise UnmatchedBrackets(
                        'Closing bracket has no opening match')
                curr_opening_bracket = opening_brackets[-1]
                if self.tape[self.pointer] == 0:
                    opening_brackets.pop()
                else:
                    code_index = curr_opening_bracket
            else:
                self.commands[code[code_index]]()
            code_index += 1


if __name__ == '__main__':
    p = Parser(allow_other_chars=False)
    p.run('ABC')

<mask token>
import sys


class UnmatchedBrackets(Exception):

    def __init__(self, *args):
        super().__init__(*args)


class ArgumentConflict(Exception):

    def __init__(self, *args):
        super().__init__(*args)


class InvalidCharacter(Exception):

    def __init__(self, *args):
        super().__init__(*args)


class Parser:

    def __init__(self, pointer=0, tape=None, tape_infinite_expansion=False,
        tape_length=None, allow_other_chars=True, cyclic_cell=False, tape_bit=8
        ):
        """
        Main brainf parser
        :param pointer: initial pointer position on the tape
        :param tape: you can pass a custom tape (list with numbers >= 0)
        :param tape_infinite_expansion: if True, tape will will expand infinitely to the right 
        :param allow_other_chars: If false, will raise an error if it encounters other chars
        :param cyclic_cell: If true, 255 + 1 -> 0 and 0 - 1 -> 255 (8-bit)
        """
        self.pointer = pointer
        self.infinite_expansion = tape_infinite_expansion
        if tape_length and tape_infinite_expansion:
            raise ArgumentConflict(
                'Cannot specify tape length if infinite expansion is enabled')
        self.tape_length = tape_length if tape_length else 3 * 10 ** 4
        self.allow_other_chars = allow_other_chars
        self.cyclic_cell = cyclic_cell
        self.max_tape_value = 2 ** tape_bit - 1
        self.commands = {'>': self.increment_pointer, '<': self.
            decrement_pointer, '+': self.increment_value, '-': self.
            decrement_value, ',': self.read_input, '.': self.write_output,
            '[': None, ']': None}
        if tape:
            self.tape = tape
        else:
            self.tape = [(0) for _ in range(self.tape_length)]

    def increment_pointer(self):
        """equivalent of >"""
        if not self.infinite_expansion:
            if self.pointer > len(self.tape):
                raise ValueError('Segmentation fault')
        self.pointer += 1

    def decrement_pointer(self):
        """equivalent of <"""
        if self.pointer < 0:
            raise ValueError('Segmentation fault')
        self.pointer -= 1

    def increment_value(self):
        """equivalent of +"""
        if self.cyclic_cell:
            self.tape[self.pointer] = 0
        elif self.tape[self.pointer] == self.max_tape_value:
            raise ValueError(f'Cannot store values > {self.max_tape_value}')
        else:
            self.tape[self.pointer] += 1

    def decrement_value(self):
        """equivalent of -"""
        if self.cyclic_cell:
            self.tape[self.pointer] = self.max_tape_value
        elif self.tape[self.pointer] == 0:
            raise ValueError('Cannot store negative values')
        else:
            self.tape[self.pointer] -= 1

    def read_input(self):
        """equivalent of ,"""
        c = ord(sys.stdin.read(1))
        if c != 26:
            self.tape[self.pointer] = c

    def write_output(self):
        """equivalent of ."""
        sys.stdout.write(chr(self.tape[self.pointer]))

    def run(self, code):
        """execute code"""
        if self.allow_other_chars:
            code = ''.join([i for i in code if i in self.commands])
        elif any([(c not in self.commands) for c in code]):
            raise InvalidCharacter('Invalid character found in code')
        opening_brackets = []
        code_index = 0
        while code_index < len(code):
            if code[code_index] == '[':
                opening_brackets.append(code_index)
            elif code[code_index] == ']':
                if not opening_brackets:
                    raise UnmatchedBrackets(
                        'Closing bracket has no opening match')
                curr_opening_bracket = opening_brackets[-1]
                if self.tape[self.pointer] == 0:
                    opening_brackets.pop()
                else:
                    code_index = curr_opening_bracket
            else:
                self.commands[code[code_index]]()
            code_index += 1


if __name__ == '__main__':
    p = Parser(allow_other_chars=False)
    p.run('ABC')

"""
brainf. interpreter in python

(Program Start)	
    char array[infinitely large size] = {0};
    char *ptr=array;

/-----------------------------------\    
|   Bf command |    C command       | 
|-----------------------------------|
|      >	   |    ++ptr;          | 
|      <	   |    --ptr;          |
|      +	   |    ++*ptr;         |
|      -	   |    --*ptr;         |
|      .	   |    putchar(*ptr);  |
|      ,	   |    *ptr=getchar(); |
|      [	   |    while (*ptr) {  |
|      ]	   |    }               |
\-----------------------------------/
"""

import sys


class UnmatchedBrackets(Exception):
    def __init__(self, *args):
        super().__init__(*args)

class ArgumentConflict(Exception):
    def __init__(self, *args):
        super().__init__(*args)


class InvalidCharacter(Exception):
    def __init__(self, *args):
        super().__init__(*args)


class Parser:
    def __init__(self, pointer=0, tape=None, tape_infinite_expansion=False, tape_length=None,
                 allow_other_chars=True, cyclic_cell=False, tape_bit=8):
        """
        Main brainf parser
        :param pointer: initial pointer position on the tape
        :param tape: you can pass a custom tape (list with numbers >= 0)
        :param tape_infinite_expansion: if True, tape will will expand infinitely to the right 
        :param allow_other_chars: If false, will raise an error if it encounters other chars
        :param cyclic_cell: If true, 255 + 1 -> 0 and 0 - 1 -> 255 (8-bit)
        """
        self.pointer = pointer
        self.infinite_expansion = tape_infinite_expansion
        if tape_length and tape_infinite_expansion:
            raise ArgumentConflict("Cannot specify tape length if infinite expansion is enabled")

        self.tape_length = tape_length if tape_length else 3*(10**4)
        self.allow_other_chars = allow_other_chars  # TODO: implement that
        self.cyclic_cell = cyclic_cell
        self.max_tape_value = 2 ** tape_bit - 1  # 0 counts as a number, so for 8-bit 256 numbers: [0; 255]

        self.commands = {
            '>': self.increment_pointer,
            '<': self.decrement_pointer,
            '+': self.increment_value,
            '-': self.decrement_value,
            ',': self.read_input,
            '.': self.write_output,
            '[': None,  # we don't run any of these, but they are  #
            ']': None,  # required for checking character validity #
        }

        if tape:
            self.tape = tape
        else:
            self.tape = [0 for _ in range(self.tape_length)]
            
    def increment_pointer(self):
        """equivalent of >"""
        if not self.infinite_expansion:
            if self.pointer > len(self.tape):
                raise ValueError('Segmentation fault')
        self.pointer += 1
    
    def decrement_pointer(self):
        """equivalent of <"""
        if self.pointer < 0:
            raise ValueError('Segmentation fault')
        self.pointer -= 1
    
    def increment_value(self):
        """equivalent of +"""
        if self.cyclic_cell:
            self.tape[self.pointer] = 0
        else:
            if self.tape[self.pointer] == self.max_tape_value:
                raise ValueError(f'Cannot store values > {self.max_tape_value}')
            else:
                self.tape[self.pointer] += 1
    
    def decrement_value(self):
        """equivalent of -"""
        if self.cyclic_cell:
            self.tape[self.pointer] = self.max_tape_value
        else:
            if self.tape[self.pointer] == 0:
                raise ValueError('Cannot store negative values')
            else:
                self.tape[self.pointer] -= 1

    def read_input(self):
        """equivalent of ,"""
        c = ord(sys.stdin.read(1))
        if c != 26:
            self.tape[self.pointer] = c

    def write_output(self):
        """equivalent of ."""
        sys.stdout.write(chr(self.tape[self.pointer]))

    def run(self, code):
        """execute code"""
        if self.allow_other_chars:
            code = ''.join([i for i in code if i in self.commands])
        else:
            if any([c not in self.commands for c in code]):
                raise InvalidCharacter('Invalid character found in code')
        opening_brackets = []
        code_index = 0
        while code_index < len(code):
            # TODO: check the validity of the input (matched brackets)
            if code[code_index] == '[':
                opening_brackets.append(code_index)
            elif code[code_index] == ']':
                if not opening_brackets:
                    raise UnmatchedBrackets('Closing bracket has no opening match')
                curr_opening_bracket = opening_brackets[-1]
                if self.tape[self.pointer] == 0:
                    opening_brackets.pop()
                else:
                    code_index = curr_opening_bracket  # it will be incremented at the end of the loop anyways
            else:
                self.commands[code[code_index]]()
            code_index += 1

if __name__ == '__main__':
    p = Parser(allow_other_chars=False)
    p.run('ABC')
