BRAINF_K - Brainf_ck
brainf*ck is the ungodly creation of Urban Mller, whose goal was apparently to create a Turing-complete language for which he could write the smallest compiler ever. http://en.wikipedia.org defines it as "a computer programming language designed to challenge and amuse programmers, and is not suitable for practical use. Its name has been variously euphemized, as in brainf*ck."
A brainf*ck program has an implicit byte pointer, called "the pointer", which is free to move around within an array of 32768 bytes, initially all set to zero. The pointer itself is initialized to point to the beginning of this array.
The brainf*ck programming language consists of seven commands, each of which is represented as a single character. Note: "Industry standard" brainf*ck actually has eight commands, but for the purposes of this problem one command was intentionally omitted.
COMMAND | OPERATION |
> | Increment the pointer. |
Incrementing a pointer value of 32767 | |
results in a pointer value of 0. | |
< | Decrement the pointer. |
Decrementing a pointer value of 0 | |
results in a pointer value of 32767. | |
+ | Increment the byte at the pointer. |
Incrementing the byte value 255 results | |
in the byte value 0. | |
- | Decrement the byte at the pointer. |
Decrementing the byte value 0 results | |
in the byte value 255. | |
. | Output the character whose ASCII |
value is the byte at the pointer | |
[ | Jump forward past the matching ] if the |
byte at the pointer is zero. | |
] | Jump backward to the matching [ |
unless the byte at the pointer is zero. |
For this problem, you will write a program that reads in, parses and executes a brainf*ck program.
Input
Input contains exactly one program. The program consists of one or more lines of brainf*ck commands. Your program should ignore any illegal characters (I.E. any character not in the set: <>+-.[]), If a percent sign (%) is encountered during parsing, the remainder of the line should be discarded. This constitutes a comment. The maximum number of commands in a brainf*ck program is 128000.
Output
Your program should output the output generated by the brainf*ck program. The only possible parsing error that can occur is if there is an unmatched [ or ] in the brainf*ck program. If your program encounters such an error, it should simply print "COMPILE ERROR" instead of executing the program. All brainf*ck programs will use no more than the specified 32768 bytes of memory.
Score
Score is the length of your source.
Example
Input: ++++++++[>+++++++++ % hello-world. <-]>.<+++++[>++++++<-]>-.+++++++.. +++.<++++++++[>>++++<<-]>>.<<++++[> ------<-]>.<++++[>++++++<-]>.+++. ------.--------.>+. Output: Hello World! Input: []] Output: COMPILE ERRORA super hard test case was removed.
hide comments
Rafail Loizou:
2023-08-15 15:22:16
Is '\r' considered as well? |
|
Rafail Loizou:
2023-08-14 20:29:34
I mean it would be nice if you could show some feedback. I'm getting TLE and I don't know why. Should I check for infinite loops? I don't get why I get TLE. |
|
Daniel Carvalho:
2015-03-01 15:21:30
What I meant by my previous question: what should I do when the pointer to the first memory cell is decremented (or last is increased)? |
|
Daniel Carvalho:
2015-03-01 15:17:14
Should the contents of the cell be output when there is a '.', and the cell contains non-readable characters? In a brainfuck program, where does the memory start? Is it in the middle of the memory block? |
|
Alexandre Henrique Afonso Campos:
2014-01-22 11:58:47
Case 14 must be The Bible in extended version, with comments, in 63 languages. |
|
Mitch Schwartz:
2013-06-16 18:47:06
Hmm, my 0.57s AC C code gets 1.87s for the super hard test case on ideone, while my TLE Perl code upon translation (roughly speaking) into C gets 0.04s with 4.7.2 but >15s TLE with 4.3.2 on ideone. |
|
Aditya Pande:
2012-12-18 15:40:31
thanks but now i am getting TLE when submitting same solution in C
|
|
(Tjandra Satria Gunawan)(曾毅昆):
2012-12-18 15:36:30
@Aditya Pande: did you try with other C type, C++ 4.3.2 or C99, because sometimes every compiler give different result... |
|
Aditya Pande:
2012-12-18 13:41:45
why am i getting NZEC error in C++. someone please help
|
|
Suyash Garg:
2012-12-14 14:22:01
how to decrease the code? My code is 600B |
Added by: | Bin Jin |
Date: | 2007-08-20 |
Time limit: | 1.735s |
Source limit: | 512B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All |
Resource: | ACM/ICPC Greater New York Regional Contest 2005 |