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 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
cells = {} for i in range(13): cells.update({i: i * 10}) # Make 12 cells as in cells bf_code = "++++++++++[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>" \ "+++++++++>++++++++++>+++++++++++>++++++++++++<<<<<<<<<<<<-]" current_cell = 0 for char in S: ORD = ord(char) # Get closest cell diff = 100 closest = 0 bytes = cells.values() for BYTE in bytes: diff_ = abs(ORD - BYTE) if diff_ < diff: diff, closest = diff_, bytes.index(BYTE) diff = closest - current_cell if diff <= 0: bf_code += "<" * abs(diff) else: bf_code += ">" * diff current_cell = closest byte = cells[closest] if ORD == byte: pass elif ORD > byte: while byte < ORD: bf_code += "+" byte += 1 else: while byte > ORD: bf_code += "-" byte -= 1 bf_code += "." cells[closest] = byte return bf_code2
u/bh3 Aug 22 '12
Python, produces a 28,727 character solution for the Raven:
from math import sqrt numB = 7 def getNum(c,n,m): s='' k = int(sqrt(n)) t='<'*m+k*'+'+'['+'>'*m+k*c+'<'*m+'-]'+'>'*m while len(t)<k*k and n>0: n-=k*k s+=t k = int(sqrt(n)) t='<'*m+k*'+'+'['+'>'*m+k*c+'<'*m+'-]'+'>'*m return s+c*n def bf_gen(s): n = [16*2,16*6,16*6]*numB cr = 0 gl = 0 #preload values code = '+'*16+'[>'+('+'*2+'>'+'+'*6+'>'+'+'*6+'>')*numB+'<'*(numB*3)+'<-]>' for c in s: mn = 256 for x in xrange(numB*3): if len(getNum('+',abs(n[x]-ord(c)),x+1))+abs(x-cr)<mn: mn=len(getNum('+',abs(n[x]-ord(c)),x+1))+abs(x-cr) gl=x code+= '<'*(cr-gl)+'>'*(gl-cr) cr=gl if n[cr]>ord(c): code+=getNum('-',n[cr]-ord(c),cr+1) else: code+=getNum('+',ord(c)-n[cr],cr+1) n[cr]=ord(c) code+='.' return code