#P2027. BrainFuck Interpreter

    ID: 15309 Type: Default 1000ms 256MiB

BrainFuck Interpreter

BrainFuck Interpreter

This problem requires you to write an interpreter for the esoteric programming language bf, a shortened version of BrainFuck. The language uses only 8 commands: +, -, <, >, ., ,, [, and ].

The interpreter uses a memory tape of size \(30000\) cells. Each cell stores a signed 8-bit integer, meaning its value is always in the range \([-128,127]\). All cells are initially set to 0, and the data pointer starts at the first cell. When performing arithmetic operations, the values wrap around within this range.

Execution semantics for each command are as follows:

  • <: Decrement the pointer (move it to the left).
  • >: Increment the pointer (move it to the right).
  • +: Increment the value at the current cell, with wrap-around if it exceeds 127.
  • -: Decrement the value at the current cell, with wrap-around if it goes below -128.
  • .: Output the character corresponding to the ASCII value of the current cell. (For negative values, use the equivalent unsigned 8-bit conversion.)
  • ,: Read one byte from input and store its ASCII value in the current cell. If the input is exhausted, store -1.
  • [ and ]: Define a loop. If the value at the current cell is zero, jump forward to the command after the matching ]; otherwise, if nonzero, jump back to the command after the matching [.

Your task is to simulate the execution of a given bf program with its input and produce the corresponding output.

inputFormat

The input consists of two lines:

  1. The first line is a string representing the bf program. It contains only the characters +, -, ,, ., >, <, [, and ].
  2. The second line is the input buffer for the program (this line may be empty).

outputFormat

Output the result produced by executing the bf program.

sample

+++++[>+++++++++++.+.
34