Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

[CHALLENGE] Write a program that converts a string to Brainfuck code

What's up HF, today I have a challenge for you... I know you probably think it's impossible just because you saw the words "Brainfuck". Well guess what? It's actually pretty easy.

Your challenge is to create a function (in any language you like) that takes a string as a parameter and prints out Brainfuck code that will compute that string.

Use this to test your code: https://copy.sh/brainfuck/
How Brainfuck works: https://www.youtube.com/watch?v=-3C200nCwpk

First person to post a working solution gets the prize. Bonus points for having less lines of code (and don't try to shorten code by compressing everything to one line; you must have new lines after semi colons and at least three lines for if-statements, etc.)

Good luck! 🎉
Code
----[---->+<]>++.[--->+<]>+++.----------.-[--->+<]>-.---[->++++<]>.------------.---.--[--->+<]>-.[-->+++++++<]>.++.---------.------.++.--[--->+<]>-.-[--->++<]>-.++++++++++.[->+++++<]>.
Since I guess nobody can figure it out, I'll post my solution in Go.

[Image: tCVBb6X.png]


(Dec 5, 2016 04:30 PM)Zilog80 Wrote:  
Code
----[---->+<]>++.[--->+<]>+++.----------.-[--->+<]>-.---[->++++<]>.------------.---.--[--->+<]>-.[-->+++++++<]>.++.---------.------.++.--[--->+<]>-.-[--->++<]>-.++++++++++.[->+++++<]>.

Lol, I don't need to buy your program, I've already made my own. It's a challenge.
(Dec 5, 2016 04:32 PM)Insurmountable Wrote:  Lol, I don't need to buy your program, I've already made my own. It's a challenge.

Lol...the fucking smartphone grammar Dictionary wrote price when I wanted to write "prize" (as you said in #1)
PHP Code

<?php

function toBrainfuck(string $string) {
    
$result = '';
    
$letters = str_split($string);

    foreach (
$letters as $letter) {
        
$ascii = ord($letter);
        
$result .= str_repeat('+', $ascii) . '.';
        
$result .= str_repeat('-', $ascii);
    }

    return 
$result;
} 

Obviously not the smarted way to do things, but you never said how it should work :p
(Dec 5, 2016 04:47 PM)Zilog80 Wrote:  Lol...the fucking smartphone grammar Dictionary wrote price when I wanted to write "prize" (as you said in #1)

If you want the prize, show your code!
[Image: 5gCAn5a.png]

13lines, 199characters
(Dec 5, 2016 05:01 PM)PartTimeNinja Wrote:  
PHP Code

<?php

function toBrainfuck(string $string) {
    
$result = '';
    
$letters = str_split($string);

    foreach (
$letters as $letter) {
        
$ascii = ord($letter);
        
$result .= str_repeat('+', $ascii) . '.';
        
$result .= str_repeat('-', $ascii);
    }

    return 
$result;
} 

Obviously not the smarted way to do things, but you never said how it should work :p

Nice job, it's a helpful that PHP has a built in function for that.


(Dec 5, 2016 05:19 PM)LanMan Wrote:  [Image: 5gCAn5a.png]

13lines, 199characters

Ooh, good to see a solution in vanilla C. Nicely done.
(Dec 5, 2016 05:20 PM)Insurmountable Wrote:  Nice job, it's a helpful that PHP has a built in function for that.

Indeed.

Made it a bit shorter cause I can.

PHP Code

<?php

function toBrainfuck($string) {
    
$result = '';
    foreach (
str_split($string) as $letter) {
        
$ascii = ord($letter);
        
$result .= str_repeat('+', $ascii) . '.>';
    }
    return 
$result;
} 
(Dec 5, 2016 05:20 PM)Insurmountable Wrote:  Ooh, good to see a solution in vanilla C. Nicely done.
Ye but the result is far from optimised haha 😁
And nice challenge, i didn't know about brainfuck !

Helloworld:
Spoiler (Click to View)
Next challenge should be the shortest brainfuck string result for a defined word/phrase !
(Dec 5, 2016 05:28 PM)LanMan Wrote:  Ye but the result is far from optimised haha 😁
And nice challenge, i didn't know about brainfuck !

Helloworld:
Spoiler (Click to View)
Next challenge should be the shortest brainfuck string result for a defined word/phrase !

Good idea! Have your program convert this phrase, try to get the shortest result:
Quote:Hello, world!


Code:
Code
func v2_convert(s string) {
    ascii := []byte(s)
    for i := 0; i < len(ascii); i++ {
        if i == 0 {
            for j := 0; j < int(ascii[i]); j++ {
                fmt.Print("+")
            }
        } else if ascii[i-1] > ascii[i] {
            for j := 0; j < int(ascii[i-1])-int(ascii[i]); j++ {
                fmt.Print("-")
            }
        } else if ascii[i-1] < ascii[i] {
            for j := 0; j < int(ascii[i])-int(ascii[i-1]); j++ {
                fmt.Print("+")
            }
        }
        fmt.Print(".")
    }
}

Result: 383 chars
Code
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.--------.+++.------.--------.-------------------------------------------------------------------.
... You posted a solution only 2 hours after you posted the thread...
(Dec 5, 2016 09:09 PM)VmX5 Wrote:  ... You posted a solution only 2 hours after you posted the thread...

I made the solution 15 minutes after learning brainfuck
(Dec 5, 2016 07:20 PM)Insurmountable Wrote:  Good idea! Have your program convert this phrase, try to get the shortest result:

Result: 383 chars
Code
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.--------.+++.------.--------.-------------------------------------------------------------------.

Code:
Spoiler (Click to View)

Result: 343chars !
Code
+++++++++++++[>+++++<-]>+++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.--------.+++.------.--------.-------------------------------------------------------------------.
(Dec 6, 2016 04:10 PM)LanMan Wrote:  Code:
Spoiler (Click to View)

Result: 343chars !
Code
+++++++++++++[>+++++<-]>+++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.--------.+++.------.--------.-------------------------------------------------------------------.

Come on now, that's cheating! You have to generate everything programmatically!


Possibly Related Threads...
Thread / Author Replies Views Last Post
by MaskinNov 28, 2023 06:57 PM 11 330 Nov 28, 2023 06:57 PM
Last Post: Hackers
by luucaFeb 26, 2021 08:39 AM 2 144 Feb 26, 2021 08:39 AM
Last Post: gametime
[Help] by rosen2000Apr 18, 2020 03:29 PM 5 159 Apr 18, 2020 03:29 PM
Last Post: Epidemic