Hack Forums
[CHALLENGE] Write a program that converts a string to Brainfuck code - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: [CHALLENGE] Write a program that converts a string to Brainfuck code (/showthread.php?tid=5485361)

Pages: Pages 1 2


[CHALLENGE] Write a program that converts a string to Brainfuck code - Insurmountable - Dec 5, 2016

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! 🎉



* - Zilog80 - Dec 5, 2016

Code
----[---->+<]>++.[--->+<]>+++.----------.-[--->+<]>-.---[->++++<]>.------------.---.--[--->+<]>-.[-->+++++++<]>.++.---------.------.++.--[--->+<]>-.-[--->++<]>-.++++++++++.[->+++++<]>.



* - Insurmountable - Dec 5, 2016

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.


* - Zilog80 - Dec 5, 2016

(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)


* - PartTimeNinja - Dec 5, 2016

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


* - Insurmountable - Dec 5, 2016

(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!


* - LanMan - Dec 5, 2016

[Image: 5gCAn5a.png]

13lines, 199characters


* - Insurmountable - Dec 5, 2016

(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.


* - PartTimeNinja - Dec 5, 2016

(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;
} 



* - LanMan - Dec 5, 2016

(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 !


* - Insurmountable - Dec 5, 2016

(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
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.--------.+++.------.--------.-------------------------------------------------------------------.



* - VmX5 - Dec 5, 2016

... You posted a solution only 2 hours after you posted the thread...


* - Insurmountable - Dec 5, 2016

(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


* - LanMan - Dec 6, 2016

(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
+++++++++++++[>+++++<-]>+++++++.+++++++++++++++++++++++++++++.+++++++..+++.-------------------------------------------------------------------.------------.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.--------.+++.------.--------.-------------------------------------------------------------------.



* - Insurmountable - Dec 7, 2016

(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!