Generating BF-code with single purpose

7

2

I'd like to see you create code (in any programming language) that output working BF (Brain***k) code. Your program have a single character input on stdin. The input should be one digit ("0", "1", "2", "3", "4", "5", "6", "7", "8" or "9").

Your code should output BF-code that when interpreted with a normal BF-interpreter outputs a 5*5 ascii version of the digit. The generated BF-code should not accept any input. The resulting output from the generated BF-code should be exactly this:

Input: '0'

Output BF-code that when interpreted generates the following output:

.---.
|  /|
| / |
|/  |
'---'

Input: '1'

Output BF-code that when interpreted generates the following output:

 .
/|
 |
 |
 '

Input: '2'

Output BF-code that when interpreted generates the following output:

.---.
    |
.---'
|
'---'

Input: '3'

Output BF-code that when interpreted generates the following output:

.---.
    |
 ---|
    |
'---'

Input: '4'

Output BF-code that when interpreted generates the following output:

.   .
|   |
'---|
    |
    '

Input: '5'

Output BF-code that when interpreted generates the following output:

.---
|   
'---.
    |
'---'

Input: '6'

Output BF-code that when interpreted generates the following output:

.---
|
|---.
|   |
'---'

Input: '7'

Output BF-code that when interpreted generates the following output:

----.
   /
  /
 /
'

Input: '8'

Output BF-code that when interpreted generates the following output:

.---.
|   |
|---|
|   |
'---'

Input: '9'

Output BF-code that when interpreted generates the following output:

.---.
|   |
'---|
    |
'---'

The output from your code must be 10 different BF sourcecodes, one for each digit you input. Your program should not output all 10 BF in one run, it must ask for a digit and output BF code for that and only that ascii representation.

The BF-code requirements:

  • cell size: 8bit unsigned.
  • Overflow is undefined.
  • Array size: Max 30000 bytes (not circled)
  • It can contain other characters than [],.+-<>, but they should all be ignored. It is only - the standard BF code without any extensions that counts.
  • The EOF return 0
  • Linefeed is chr(10)

The winning answer will be the one that scores that lowest code using this formula:

your code in number of bytes * 5 + sum of the 10 generated BF codes

So keep in mind that you need to generate GOLF'd BF-code to score better.

Edit/Add (2011-08-25): You may omit or supply trailing spaces, as long as the digits displays the ASCII representation on a 80 column output, so the limit is 80 chars rows. Honor to the answers to generates exactly 5*5 output for all digits.

Plarsen

Posted 2011-08-10T18:08:57.857

Reputation: 1 740

@user unknown : Thanks for spotting that unneccesary irregularity, I have updated the shape and tried to make it as uniform to the other digits as possible – Plarsen – 2011-08-11T04:43:43.287

Am I correct that the trailing spaces in the 5 are unintentional and not necessary? My solution does not include them. – PleaseStand – 2011-08-25T00:36:31.757

@PleaseStand that is correct, you can omit or supply trailing spaces, as long as the digits displays the ASCII representation on a 80 column output, so the limit is 80 chars rows. I Will edit the question later today and include this info – Plarsen – 2011-08-25T05:13:50.970

And does the "EOF return 0" part mean I have to end my output with a null byte or does it mean , can substitute for [-]? – PleaseStand – 2011-08-25T18:33:41.357

well, the EOF part is about how the bf-interpreter reads the bf sourcecode, so I guess it is not really applicable. Your output from your code or the output from the bf-code does not need to end with 0. – Plarsen – 2011-08-26T15:55:34.510

Answers

5

JavaScript (JS 455 * 5 = 2275, BF = 3505, score = 5780)

The generated code builds a character table as it produces its ASCII art output to save thousands of plus/minus signs overall. Output is placed into the text box in a prompt window so that it can be copied and pasted into a BF interpreter. The consecutive spaces in this program must remain verbatim for it to work correctly; hopefully, posting the code here will work correctly. (Note: The JS code requires ES5 or JavaScript 1.6 support to work properly.)

M=prompt;S="3cb80kh77j31f4031m10g251p64d1064a20ionle3292032510".substr(5*M(A=[]),5).replace(/./g,function(a){return"'---'0    |0|   |0.---.0|0'---|0.---0 |0|/  |0|---|0|---.0| / |0|  /|0'---.0'0.---'0.   .0/|0----.0 '0 .0 /0 ---|0  /0   /0    '".split(i=j=0)[parseInt(a,26)]+"\n"});for(O="";i<S.length;O+=".")D=A.indexOf(C=S.charCodeAt(i)),i++&&(d=-j+(j=D<0?A.length:D),O+=Array(d>0?1+d:1-d).join(d>0?">":"<")),D<0&&(O+=Array((A[j]=C)+1).join("+"));M(0,O)

PleaseStand

Posted 2011-08-10T18:08:57.857

Reputation: 5 369

Nice one!, I tested it in Google chrome Javascript-console and it seems to work flawless – Plarsen – 2011-08-25T16:29:23.563

22

BrainFuck (Score : 13073, BF : 2299 2286, BF (generated) : 1643)

,>++++++++[<------>->>>+++++++++++>+++++++++++>+++++>++++++++>++++++++>++++++>++++++<<<<<<<<<]
>>>+++>+++++>+++>---->-->--->--<<<<........<<.>>>>>.<.<<.>>.<<....>>.<<......>>.<<......>>.<<.
..............>>.<<......>>.<<.....>.......<<.>>>.<<..>>..>..<.>...<.<<....>>.>.<.>.<<<<<<<<+<
[>-]>[>>>>>>....>>>.<<.>>...<<<.>>>.<<<..>>>.<<....>>.<<<...>>>..<<....>>.<<<.>>>.<<<....>>>.<
<....>>.<<<...>>>.<<....>>.<<<....>>>.<<...>>.<<<....>>>.<<....>>.<<.>>.<<<....>>>..<<...>>.<<
<....>>>.<<......>>.<<<...>>>...<<...>>.<<<<<<<<<->>+<]>-[+<<<->[-]+>[-]<<[>-]>[>>>>>>.....>>>
.<<.>>.<<<..>>>.<<.....>>.<<<.>>>.<<<....>>>.<<.>>.<<...>>.<<<....>>>.<<.>>.<<...>>.<<<....>>>
.<<.>>.<<.....>>.<<<<<<<<<->>+<]>-[+<<<->[-]+>[-]<<[>-]>[>>>>>>....>>>.<<.>>...<<<.>>>.<<<..>>
>.<<.>>....<<...>>.<<<....>>>.<<..>>.<<.>>...<<...>>.<<<......>>>.<<....>>.<<<....>>>.<<......
>>.<<<...>>>...<<...>>.<<<<<<<<<->>+<]>-[+<<<->[-]+>[-]<<[>-]>[>>>>>>....>>>.<<.>>...<<<.>>>.<
<<..>>>.<<.>>....<<...>>.<<<....>>>.<<.>>.<<..>>...<<.>>.<<<....>>>.<<.>>....<<...>>.<<<....>>
>.<<......>>.<<<...>>>...<<...>>.<<<<<<<<<->>+<]>-[+<<<->[-]+>[-]<<[>-]>[>>>>>>....>>>.<<<.>>>
...<<.>>.<<<..>>>.<<....>>.<<<...>>>...<<...>>.<<<....>>>.<<......>>.<<<...>>>...<<.>>.<<<....
>>>.<<.>>....<<...>>.<<<....>>>.<<.>>....<<.....>>.<<<<<<<<<->>+<]>-[+<<<->[-]+>[-]<<[>-]>[>>>
>>>....>>>.<<.>>...<<<...>>>.<<....>>.<<<....>>>.<<......>>.<<<...>>>...<<<.>>>.<<<..>>>.<<.>>
....<<...>>.<<<....>>>.<<......>>.<<<...>>>...<<...>>.<<<<<<<<<->>+<]>-[+<<<->[-]+>[-]<<[>-]>[
>>>>>>....>>>.<<.>>...<<<...>>>.<<....>>.<<<....>>>.<<....>>.<<<.>>>...<<<.>>>.<<<..>>>.<<....
>>.<<<...>>>...<<...>>.<<<....>>>.<<......>>.<<<...>>>...<<...>>.<<<<<<<<<->>+<]>-[+<<<->[-]+>
[-]<<[>-]>[>>>>>>...>>>....<<<.>>>.<<<..>>>.<<.>>...<<....>>.<<<.....>>>.<<.>>..<<....>>.<<<..
...>>>.<<.>>.<<....>>.<<<.....>>>.<<......>>.<<<<<<<<<->>+<]>-[+<<<->[-]+>[-]<<[>-]>[>>>>>>...
.>>>.<<.>>...<<<.>>>.<<<..>>>.<<....>>.<<<...>>>...<<...>>.<<<....>>>.<<....>>.<<<.>>>...<<.>>
.<<<....>>>.<<....>>.<<<...>>>...<<...>>.<<<....>>>.<<......>>.<<<...>>>...<<...>>.<<<<<<<<<->
>+<]>-[+<<<->[-]+>[-]<<[>-]>[>>>>>>....>>>.<<.>>...<<<.>>>.<<<..>>>.<<....>>.<<<...>>>...<<...
>>.<<<....>>>.<<......>>.<<<...>>>...<<.>>.<<<....>>>.<<.>>....<<...>>.<<<....>>>.<<......>>.<
<<...>>>...<<...>>.>]]]]]]]]]]

(FYI : over 90% of above code is generated by JS code to generate part of BrainFuck code generating part of BrainFuck code generating ASCII art.)

This program gets one ASCII character ('0'~'9') and print the BF code generating the ASCII art.

Generated BF codes :

#0
++++++++[->+>++++>++++++>++++++>+++++++++++++++>++++++>+++++<<<<<<<]>++>>-->--->++++>->-<<<<.>...<.<<.>>>>.<<<..>>>>.<.<<<<.>>>>.<<<.>>>>.<<<<.>>>.<<<<.>>>>.>.<<<<..>>>.<<<<.>>>>>>.<<<...>>>.
#1
++++++++[->+>++++>++++++>++++++>+++++++++++++++>++++++>+++++<<<<<<<]>++>>-->--->++++>->-<<<<<.>.<<.>>>>>.<.<<<<.>.>>>.<<<<.>.>>>.<<<<.>.>>>>>.
#2
++++++++[->+>++++>++++++>++++++>+++++++++++++++>++++++>+++++<<<<<<<]>++>>-->--->++++>->-<<<<.>...<.<<.>....>>>.<<<<.>>.>...>>>.<<<<<<.>>>>.<<<<.>>>>>>.<<<...>>>.
#3
++++++++[->+>++++>++++++>++++++>+++++++++++++++>++++++>+++++<<<<<<<]>++>>-->--->++++>->-<<<<.>...<.<<.>....>>>.<<<<.>.>>...>.<<<<.>....>>>.<<<<.>>>>>>.<<<...>>>.
#4
++++++++[->+>++++>++++++>++++++>+++++++++++++++>++++++>+++++<<<<<<<]>++>>-->--->++++>->-<<<<.<...>.<<.>>>>.<<<...>>>.<<<<.>>>>>>.<<<...>.<<<<.>....>>>.<<<<.>....>>>>>.
#5
++++++++[->+>++++>++++++>++++++>+++++++++++++++>++++++>+++++<<<<<<<]>++>>-->--->++++>->-<<<<.>...<<<.>>>>.<<<<.>>>>>>.<<<...<.<<.>....>>>.<<<<.>>>>>>.<<<...>>>.
#6
++++++++[->+>++++>++++++>++++++>+++++++++++++++>++++++>+++++<<<<<<<]>++>>-->--->++++>->-<<<<.>...<<<.>>>>.<<<<.>>>>.<...<.<<.>>>>.<<<...>>>.<<<<.>>>>>>.<<<...>>>.
#7
++++++++[->+>++++>++++++>++++++>+++++++++++++++>++++++>+++++<<<<<<<]>++>>-->--->++++>->-<<<....<.<<.>...>>>>.<<<<<.>..>>>>.<<<<<.>.>>>>.<<<<<.>>>>>>.
#8
++++++++[->+>++++>++++++>++++++>+++++++++++++++>++++++>+++++<<<<<<<]>++>>-->--->++++>->-<<<<.>...<.<<.>>>>.<<<...>>>.<<<<.>>>>.<...>.<<<<.>>>>.<<<...>>>.<<<<.>>>>>>.<<<...>>>.
#9
++++++++[->+>++++>++++++>++++++>+++++++++++++++>++++++>+++++<<<<<<<]>++>>-->--->++++>->-<<<<.>...<.<<.>>>>.<<<...>>>.<<<<.>>>>>>.<<<...>.<<<<.>....>>>.<<<<.>>>>>>.<<<...>>>.

Example output for 7 :

----.
   /
  /
 /
'

JiminP

Posted 2011-08-10T18:08:57.857

Reputation: 3 264

1Very nice and intuitive aproach. To bad bf-code is hard on the bytecount. – Plarsen – 2011-09-28T05:16:18.547

1

GTB (GTB, 888 * 5 = 4440, BF = 1643, score = 6083)

Yes, this does run on your calculator!

~"++++++++[->+>++++>++++++>++++++>+++++++++++++++>++++++>+++++<<<<<<<]>++>>-->--->++++>->-<<<"→_`A@A=0~"<.>...<.<<.>>>>.<<<..>>>>.<.<<<<.>>>>.<<<.>>>>.<<<<.>>>.<<<<.>>>>.>.<<<<..>>>.<<<<.>>>>>>.<<<...>>>.@A=1~"<<.>.<<.>>>>>.<.<<<<.>.>>>.<<<<.>.>>>.<<<<.>.>>>>>.@A=2~"<.>...<.<<.>....>>>.<<<<.>>.>...>>>.<<<<<<.>>>>.<<<<.>>>>>>.<<<...>>>.@A=3~"<.>...<.<<.>....>>>.<<<<.>.>>...>.<<<<.>....>>>.<<<<.>>>>>>.<<<...>>>.@A=4~"<.<...>.<<.>>>>.<<<...>>>.<<<<.>>>>>>.<<<...>.<<<<.>....>>>.<<<<.>....>>>>>.@A=5"<.>...<<<.>>>>.<<<<.>>>>>>.<<<...<.<<.>....>>>.<<<<.>>>>>>.<<<...>>>.@A=6~"<.>...<<<.>>>>.<<<<.>>>>.<...<.<<.>>>>.<<<...>>>.<<<<.>>>>>>.<<<...>>>.@A=7~"....<.<<.>...>>>>.<<<<<.>..>>>>.<<<<<.>.>>>>.<<<<<.>>>>>>.@A=8~"<.>...<.<<.>>>>.<<<...>>>.<<<<.>>>>.<...>.<<<<.>>>>.<<<...>>>.<<<<.>>>>>>.<<<...>>>.@A=9~"<.>...<.<<.>>>>.<<<...>>>.<<<<.>>>>>>.<<<...>.<<<<.>....>>>.<<<<.>>>>>>.<<<...>>>.

Timtech

Posted 2011-08-10T18:08:57.857

Reputation: 12 038

Excellent work! :) – Plarsen – 2014-01-08T19:31:16.210

Thanks, I also updated the score (didn't see the language *5) – Timtech – 2014-01-08T21:34:17.233

1

Spoon (Spoon, 7430 * 5 = 37150, BF = 1643, score = 38793)

00101100101111111100100011000000000000000000010000010010010111111111110101111111111101011111010111111110101111111101011111101011111101101101101101101101101101100110100100101110101111101011101000000000000001000000001000000000001000000001101101101100101000101000101000101000101000101000101000101001101100101001001001001001000101001100101001101100101001001000101001101100101000101000101000101001001000101001101100101000101000101000101000101000101001001000101001101100101000101000101000101000101000101001001000101001101100101000101000101000101000101000101000101000101000101000101000101000101000101000101000101001001000101001101100101000101000101000101000101000101001001000101001101100101000101000101000101000101001000101000101000101000101000101000101000101001101100101001001001000101001101100101000101001001000101000101001000101000101001100101001000101000101000101001100101001101100101000101000101000101001001000101001000101001100101001000101001101101101101101101101110110010001000000110100010001001001001001001000101000101000101000101001001001000101001101100101001001000101000101000101001101101100101001001001000101001101101100101000101001001001000101001101100101000101000101000101001001000101001101101100101000101000101001001001000101000101001101100101000101000101000101001001000101001101101100101001001001000101001101101100101000101000101000101001001001000101001101100101000101000101000101001001000101001101101100101000101000101001001001000101001101100101000101000101000101001001000101001101101100101000101000101000101001001001000101001101100101000101000101001001000101001101101100101000101000101000101001001001000101001101100101000101000101000101001001000101001101100101001001000101001101101100101000101000101000101001001001000101000101001101100101000101000101001001000101001101101100101000101000101000101001001001000101001101100101000101000101000101000101000101001001000101001101101100101000101000101001001001000101000101000101001101100101000101000101001001000101001101101101101101101101101100001001010110011010000001001011011011000010001000000011101000100000001101101100100010000001101000100010010010010010010001010001010001010001010001010010010010001010011011001010010010001010011011011001010001010010010010001010011011001010001010001010001010001010010010001010011011011001010010010010001010011011011001010001010001010001010010010010001010011011001010010010001010011011001010001010001010010010001010011011011001010001010001010001010010010010001010011011001010010010001010011011001010001010001010010010001010011011011001010001010001010001010010010010001010011011001010010010001010011011001010001010001010001010001010010010001010011011011011011011011011011000010010101100110100000010010110110110000100010000000111010001000000011011011001000100000011010001000100100100100100100010100010100010100010100100100100010100110110010100100100010100010100010100110110110010100100100100010100110110110010100010100100100100010100110110010100100100010100010100010100010100110110010100010100010100100100010100110110110010100010100010100010100100100100010100110110010100010100100100010100110110010100100100010100010100010100110110010100010100010100100100010100110110110010100010100010100010100010100010100100100100010100110110010100010100010100010100100100010100110110110010100010100010100010100100100100010100110110010100010100010100010100010100010100100100010100110110110010100010100010100100100100010100010100010100110110010100010100010100100100010100110110110110110110110110110000100101011001101000000100101101101100001000100000001110100010000000110110110010001000000110100010001001001001001001000101000101000101000101001001001000101001101100101001001000101000101000101001101101100101001001001000101001101101100101000101001001001000101001101100101001001000101000101000101000101001101100101000101000101001001000101001101101100101000101000101000101001001001000101001101100101001001000101001101100101000101001001000101000101000101001101100101001001000101001101101100101000101000101000101001001001000101001101100101001001000101000101000101000101001101100101000101000101001001000101001101101100101000101000101000101001001001000101001101100101000101000101000101000101000101001001000101001101101100101000101000101001001001000101000101000101001101100101000101000101001001000101001101101101101101101101101100001001010110011010000001001011011011000010001000000011101000100000001101101100100010000001101000100010010010010010010001010001010001010001010010010010001010011011011001010010010010001010001010001010011011001010010010001010011011011001010001010010010010001010011011001010001010001010001010010010001010011011011001010001010001010010010010001010001010001010011011001010001010001010010010001010011011011001010001010001010001010010010010001010011011001010001010001010001010001010001010010010001010011011011001010001010001010010010010001010001010001010011011001010010010001010011011011001010001010001010001010010010010001010011011001010010010001010001010001010001010011011001010001010001010010010001010011011011001010001010001010001010010010010001010011011001010010010001010001010001010001010011011001010001010001010001010001010010010001010011011011011011011011011011000010010101100110100000010010110110110000100010000000111010001000000011011011001000100000011010001000100100100100100100010100010100010100010100100100100010100110110010100100100010100010100010100110110110010100010100010100100100100010100110110010100010100010100010100100100010100110110110010100010100010100010100100100100010100110110010100010100010100010100010100010100100100010100110110110010100010100010100100100100010100010100010100110110110010100100100100010100110110110010100010100100100100010100110110010100100100010100010100010100010100110110010100010100010100100100010100110110110010100010100010100010100100100100010100110110010100010100010100010100010100010100100100010100110110110010100010100010100100100100010100010100010100110110010100010100010100100100010100110110110110110110110110110000100101011001101000000100101101101100001000100000001110100010000000110110110010001000000110100010001001001001001001000101000101000101000101001001001000101001101100101001001000101000101000101001101101100101000101000101001001001000101001101100101000101000101000101001001000101001101101100101000101000101000101001001001000101001101100101000101000101000101001001000101001101101100101001001001000101000101000101001101101100101001001001000101001101101100101000101001001001000101001101100101000101000101000101001001000101001101101100101000101000101001001001000101000101000101001101100101000101000101001001000101001101101100101000101000101000101001001001000101001101100101000101000101000101000101000101001001000101001101101100101000101000101001001001000101000101000101001101100101000101000101001001000101001101101101101101101101101100001001010110011010000001001011011011000010001000000101001000000101100010000100100101001010010000011001001000000000101101100100000001011000001010010000101100001000000101000000000001000000000110000000000110000100001011000101000000000110001100000011000100011001100110000000000000001001000000110000010001001100011010001101101011001100000000100001000000100000011000000001000000110010011011001100001000000010110100000000001010000000000000000000000010010100101000000001011000000001011010000100010100000001010001000000101001000100000000000000110010010000110000010000000001000000101000110010000010000000000000000101000001101101000000000001001000000011011010000011

Timtech

Posted 2011-08-10T18:08:57.857

Reputation: 12 038