Quine on Brainfuck tutorial

    On Habré already there are a lot of articles about quains and technologies of their writing. Quine lovers can get bored - there is a pattern, follow it, get the quine. Multilingual quine, even with the participation of esoteric languages, is also easy to write, and there are at least three articles about this. This is where quains entirely written in esoteric languages ​​come to the rescue, returning interest to a bored programmer.

    I’ll try to talk about the process of writing puzzles on Brainfuck.


    I will quote my article Multilingual Quains A Quay-

    like design ( hereinafter we will only talk about languages ​​where you can separate data and code ) can be divided into two parts:
    1) data segment (an important point, this part remains empty until the moment the code is completed, after which it is encoded).
    2) a code
    segment A data segment is a kind of cipher that can be decrypted in two ways, as a result of decryption, the first method will get a string
    representation of the data segment itself, when decrypting the second way, a string representation of the code segment will be obtained. Combining the results of these decryptions, we get a line that contains all the source code of the program.


    Pay attention to the selected text. There are no variables in the brainfac, there is only and exclusively executable code. On the other hand, there is a tape that can and should be used to store information.
    Therefore, for the brainfac, the quine scheme will look like this.

    1) A code that writes data to the tape
    2) A code that reads data from the tape and displays the code from p.1
    3) A code that reads data from the tape and displays the code from p.2 and p.3

    Accordingly, the code from p. 1 can be obtained by “encrypting” the code from clauses 2 and 3, when it is written.

    How to write data to tape? This begs the entry in the form of ascii-codes of characters.

    We will write a function in Python, which receives a line and returns a program in a brainfac, which writes this line to tape.
    def to_brainfuck(st): return '>'+''.join('+'*ord(c)+'>' for c in st)
    


    >>> print to_brainfuck('ku')
    >+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>
    


    Pay attention to the ">" at the beginning of the program, an empty cell at the beginning of the tape is needed in case you have to return there. An empty cell is the only way to stop the cycle at Brainfac. If the implementation of the brainfac works with an endless ribbon in both directions, the ">" at the beginning can be omitted.

    Now back to the beginning of the tape

    <[<]


    and print its contents:

    >[.>]


    The program will display “ku”.

    We already know approximately how to write the code for item 1 and item 3 of our scheme.
    Now item 2, the most difficult part.

    So, you need to write a program that, using the contents of the tape, displays the line printed by print to_brainfuck ('ku'), which is actually an analogue of the python function to_brainfuck on the brainfac.
    Only the characters that are on the tape can print the brainfac. Command "." prints the contents of the cell the carriage is on. Therefore, we need to write the characters "+" and ">" on the tape.

    >+++++++++++++++++++++++++++++++++++++++++++>
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.


    Pay attention to the ">" at the beginning - we leave an empty marker cell between what is already on the tape and any service information that we write there. Also pay attention to "." in the end. Remember the ">" at the very beginning of the program? Here we will derive it.

    back to the beginning of the tape

    [<]<[<]>


    now write a nested loop. the inner loop “pinches” one from the contents of the next cell and with each such pinching it outputs “+”, the outer loop outputs “>” and goes to the next cell

    [
        [
            [>]>                 доходим до того места на ленте, где лежит "+"
            .                      печатаем "+"
            <<[<]>-            возвращаемся к обрабатываемому символу и отнимаем единицу
        ]      
        >[>]>>                 после того, как символ кончился, доходим до места, где лежит ">"
        .                          печатаем ">"
        [<]<[<]>               возвращаемся к следующему символу
    ]
    


    so, we can write program A, filling the tape and program B, running on the filled tape and outputting the program A.
    True, program B has one drawback, after its activity nothing remains on the tape, and we need the contents of the tape for subsequent printing.

    Change program B so that each time the contents of the cell are reduced at one end of the tape, it increases the contents of the cell at the other end. I put the new code under the old one and align it so that the changes made are visible:
    [[[>]>. <<        [<]>-]>[>]>>.      [<]<[<]>]   было
    [[[>]>. [>]<+[<]< [<]>-]>[>]>>. [>]+ [<]<[<]>]   стало
    


    After executing the program, the contents of the cells with the copy are 1 more than necessary. We will not redo anything, just fix and print

    >>>[->]<<[<]>>>[.>]
    


    I don’t really like this code, it seems you can be much shorter, something like:

    >>>[-.>]
    


    only without the derivation of ascii-0 at the end, but not immediately invented.
    Putting it all together

    вносим символы "k" и "u" на ленту
    >+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>
    +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>
    вносим символы "+" и ">" на ленту, а так же выводим ">"
    >+++++++++++++++++++++++++++++++++++++++++++>
    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
    отмечаем начало области, в которую будем копировать символы
    >+
    возвращаемся на начало ленты
    [<]<[<]>
    описанный выше вложенный цикл, основная часть программы
    [[[>]>.[>]<+[<]<[<]>-]>[>]>>.[>]+[<]<[<]>]
    отнимаем по единице от каждого символа и выводим символы
    >>>[->]<<[<]>>>[.>]
    


    so, we have a program that clogs the character codes “k” and “u” onto the tape, then prints out its part that clogs them, then prints the characters themselves. Those. at the moment it is almost quine, the program prints its first part, and instead of the second it prints “ku”. And why? Because it was precisely the string “ku” that we fed at the beginning of the Python function to_brainfuck. And the text of the program should have been. But he was not there then. But now there is!
    Now the most pleasant moment. We take the part of the program that we wrote with our hands and feed the to_brainfuck functions. We combine the two parts and the quine is ready:

    >>> s=""">+++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+[<]<[<]>[[[>]>.[>]<+[<]<[<]>-]>[>]>>.[>]+[<]<[<]>]>>>[->]<<[<]>>>[.>]"""
    >>> print to_brainfuck(s)+s
    


    Quine code
    >++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>>+++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+[<]<[<]>[[[>]>.[>]<+[<]<[<]>-]>[>]>>.[>]+[<]<[<]>]>>>[->]<<[<]>>>[.>]
    



    The resulting quine is not too compact due to the selected encoding method, for each character of the “code segment” there are about 50 characters of the “data segment”. You can significantly reduce the data segment if you resort to other coding methods, of course, in this case, the part of the program that you need to write with your hands will greatly increase.

    Yes, another funny moment. Because the brainfac ignores all characters except the eight that are its commands; before encryption, it is somehow interesting to format the code and / or add some inscriptions to it. Quine from this will not cease to be quine.

    >+++++++++++++++++++++++++++++++++++++++++++>
    +                                           +
    +             Quine for Habrahabr           +
    +                                           +
    +                                           +
    +++++++++++++++++++++++++++++++++++++++++++++
    +                                           +
    +                                           +
    +                                           +
    +                                           +
    +                                           .
    >                                           +
    [                                           <
    ]                                           <
    [                                           <
    ]                                           >
    [                                           [
    [                                           >
    ]                                           >
    .                                           [
    >                                           ]
    <                                           +
    [                                           <
    ]                                           <
    [<]>-]>[>]>>.[>]+[<]<[<]>]>>>[->]<<[<]>>>[.>]
    


    Inside the square, you can add something else. I don’t quote the full text of the quine, because such games with formatting at times increase the size of the "data segment" and the quine becomes really huge. Thanks for attention.
    Has anyone read to the end? :) Update: wow! someone even likes it! Thanks!
    It seems that I managed to write an article that can claim the title of the most useless Habr tutorial (and maybe not only Habr)!

    Also popular now: