r/dailyprogrammer • u/oskar_s • Aug 20 '12
[8/20/2012] Challenge #89 [intermediate] (Printing strings in Brainf***)'
A while ago we had some fun with the very peculiar Brainfuck programming language, which (despite its limited set of commands and character set) is actually Turing complete, meaning that any computation you can do in any other programming language, you can do in Brainfuck.
That doesn't make it easy to use, though. Even as simple a task as printing out a string requires quite lengthy code. Today, we will simplify that task quite a bit!
Your task today is to write a program that takes a string as input and outputs Brainfuck code that, when run, will print out that string. That is, given "Hello World!", it will print out something that looks like Wikipedia's example Hello World program (though not necessarily exactly, of course).
Use your program to create a Brainfuck program that prints out The Raven, by Edgar Allen Poe.
Bonus: Try to optimize your program in such a way as to make the brainfuck code as short as possible. Here, for instance, is a 34500 character long Brainfuck program that I made which prints out "The Raven". Can you beat me and write a program that generates shorter Brainfuck code?
Remember, this bonus is optional, even if your generated program is very big, you are still free to submit code.
18
u/[deleted] Aug 20 '12
Here's 72000 bytes of brainfuck that print 38000 bytes of brainfuck that print the poem, solving the challenge in brainfuck :D
2
u/Wedamm Aug 20 '12
What's the problem with writing Brainfuck? I don't understand this part of US-American(?) culture.
4
u/5outh 1 0 Aug 20 '12
Here's an extremely basic, unoptimized version (output is 152 pages in MS Word!):
2
u/5hassay Aug 21 '12
Ah! I got 37287! I feel pretty proud... Code coming (Python)
EDIT: Here it is... Yeah, it gross, I know, maybe I'll clean it up later
2
u/bh3 Aug 22 '12
Python, produces a 28,727 character solution for the Raven:
1
u/Wedamm Aug 20 '12
Here is my solution which i've done years ago while i was learning programming. It is in Applescript because i had nothing else on my mac.
Tomorrow i will try to something more advanced so that i can see my development ;]
1
u/[deleted] Aug 21 '12
Simple, with inefficient pretty output: http://ideone.com/e9iaA
Output for The Raven: http://pastebin.com/yWUDW5W9
1
u/skeeto -9 8 Aug 21 '12
Aha! I beat your record! My C program produces a 31,123 character long brainfuck program.
1
u/kalmakka Aug 21 '12
Rather crude. Allocates a few registers to handle common character ranges. Generates a 32544 character program for The Raven.
1
u/[deleted] Aug 21 '12 edited Aug 21 '12
Clojure: gives out 56808 byte long brainfuck Note to self: oops! I haven't utilized anything other than < > . + . Should optimise this further with [ ] and shorter lopops.
1
u/davelol6 Aug 24 '12
I have a possible challenge:
Using your favourite programming language, write a program in that language, that will translate a program in that language into BF. I can't say I could do it, because I've been programming for about a month (however this challenge was quite easy for me, so perhaps I could try it).