Brainfuck is a programming language. And it’s what it says in its name Brainfuck.
Only 8 instructions:
Cmd Effect --- ------ + Increases element under pointer - Decrases element under pointer > Increases pointer < Decreases pointer [ Starts loop, flag under pointer ] Indicates end of loop . Outputs ASCII code under pointer , Reads char and stores ASCII under ptr
Now take a look at the Hello World (You can run it in this JavaScript Interpreter:
+++++ +++++ initialize counter (cell #0) to 10 [ use loop to set the next four cells to 70/100/30/10 > +++++ ++ add 7 to cell #1 > +++++ +++++ add 10 to cell #2 > +++ add 3 to cell #3 > + add 1 to cell #4 <<<< - decrement counter (cell #0) ] > ++ . print 'H' > + . print 'e' +++++ ++ . print 'l' . print 'l' +++ . print 'o' > ++ . print ' ' << +++++ +++++ +++++ . print 'W' > . print 'o' +++ . print 'r' ----- - . print 'l' ----- --- . print 'd' > + . print '!' > . print '\n'
Any idea? I’m not smart enough so I have to read the explanation.
Anyway, I decided to try to write one (and it’s the only one, I have ever written) to print out:
* ** *** **** *****
My first version:
Initialization +++++ +++++ [ > + > ++++ << - ] >> ++ Five iterations > +++++ [ > + >[-]>[-]<<[->+>+<<]>>[-<<+>>]<< > [ <<< . >>> - ] <<<< . >> - ]
Then, I rearranged the order of memory:
The asterisk +++++ ++ [ > +++++ + < - ] Five iterations +++++ [ >> + >[-]>[-]<<[->+>+<<]>>[-<<+>>]< [ << . >> - ] +++++ +++++ . <<< - ]
Tried to save a few characters:
Five iterations +++++ [ > + >[-]>[-]<<[->+>+<<]>>[-<<+>>] The asterisk > +++++ ++ [ < +++++ + > - ] << [ > . < - ] +++++ +++++ . << - ]
Finally, I cheated:
+++++ +++++ [ > ++++ > + << - ] > ++ . > . < .. > . < ... > . < .... > . < ..... > .
The character counts:
++++++++++[>+>++++<<-]>>++>+++++[>+>[-]>[-]<<[->+>+<<]>>[-<<+>>]<<>[<<<.>>>-]<<<<.>>-] 86 chars +++++++[>++++++<-]+++++[>>+>[-]>[-]<<[->+>+<<]>>[-<<+>>]<[<<.>>-]++++++++++.<<<-] 81 chars +++++[>+>[-]>[-]<<[->+>+<<]>>[-<<+>>]>+++++++[<++++++>-]<<[>.<-]++++++++++.<<-] 79 chars ++++++++++[>++++>+<<-]>++.>.<..>.<...>.<....>.<.....>. 54 chars
I believe someone can do better than I.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.