Generate a Brainf_ck program that outputs a string of given length

11

1

Your friend is trying to break into a vault that has a peculiar locking system: it requires a certain number of gentle knocks in a particular spot. Your friend discovered the number (which is in the range 1...99999) and possesses a gadget that produces the required knocks. However, the gadget is a Brainfuck interpreter! So your friend needs to feed it a Brainfuck program, which, obviously, should be as short as possible (the gadget's I/O is slow).

Your task is to help him! Write a program or a subroutine, in any language, that accepts as input a number N, and outputs a Brainfuck program, which takes no input and outputs a string of printable ASCII characters (excluding the space character - codes in the range 33...126) of length N.

Example: for input 10, the output might be

+++++++++++++++++++++++++++++++++..........

(but I am sure it can be shortened!)

Your score will be the sum of lengths of your outputs for the following values of N (they are random numbers):

55
68
15
28
841
838
522
846
4898
9004
9363
3810
13230
67175
37231
44701

Oh, and you will be transmitting your code (the generator program) to your friend by Twitter. So make sure it's 140 characters or less!


P.S. The Brainfuck language has many variants. Let's assume the tape is infinite in both directions (or "circular and large enough"), and the cells have 32-bit int capacity (finite and able to hold numbers up to 99999). Also, no wrapping: when a cell overflows, the machine self-destructs!

anatolyg

Posted 2015-03-17T09:27:59.460

Reputation: 10 719

1

"following values of N (they are random numbers)" reminded me of http://xkcd.com/221/

– cirpis – 2015-03-17T09:40:49.343

Just for reference, the space character (character code 32) is usually included in the printable ASCII range. It doesn't really make a difference for the challenge since you've defined the range explicitly though. – Martin Ender – 2015-03-17T11:51:46.457

3Can we assume cells in brainfuck to be arbitrary width integers? If not, how and when do they wrap? – orlp – 2015-03-17T14:23:40.200

1It would be nice to assume at least being able to contain 67175 + a few. – orlp – 2015-03-17T14:49:09.410

@anatolyg I realized that later. Sorry. – Esolanging Fruit – 2016-11-28T15:07:44.373

Answers

3

Python 2, score: 1021

I've just realized that this contest is pretty old but still, since I came up with a better solution than those posted, I posted it as well.

Here's a 102 byte python script that does the job:

n=input()
s='>'
while n:
    s+='>'+'+'*(n%5+1);n/=5
print s+'[->[-<+++++>]<<]<+++++++[>+++++<-]>>[-<.>]'

The idea is to use base 5 encoding for N (best base at least for the current inputs, which do not seem very "random" by the way, looks like they were arbitrarily chosen by OP), and to write a generic Brainfuck algorithm to decode a number of arbitrary length (the number is encoded with each digit increased by one in order to detect the end of conversion). I chose to print character 35 #, character 36 $ is equivalent.

You may run the following bash script to get the score:

i=0
while read p; do
  i=$((i+`echo $p | python convert.py | wc -m`))
done
echo $i

With a more advanced program that replaces encoding with multiplication for small numbers and chooses the best base for encoding each number, I can reach 958 Brainfuck characters, but Python is far too verbose (and I'm a pretty bad/lazy golfer) in order to get the converter into 144 bytes!

rixm

Posted 2015-03-17T09:27:59.460

Reputation: 50

This is a great idea! Maybe I'll use it once to improve this answer (wrote a script in Python to get score less than 950, but I don't know any golfing language to make it short enough). – anatolyg – 2015-10-08T17:21:13.790

8

BrainF***, score: 193,313

It's not under 140 characters (it's 147, so close!!), so this can't win, but I thought it was cool.

Prints 43 plus signs, then N periods. Not very optimal.

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

If anyone can help shorten this I would love it.

mdc32

Posted 2015-03-17T09:27:59.460

Reputation: 429

I guess with Brainfuck it would be enough to make a "subroutine" that receives its input on the tape - no need to read from "standard input device". – anatolyg – 2015-03-18T21:06:15.837

@anatolyg That makes it much easier - probably about 80 or 90 characters. Should I change it? – mdc32 – 2015-03-18T23:20:47.543

5

J, Total score = 1481

(For my previous entry and explanation check revision history.)

f10=.('>++++++++++<';'')rplc~;@([:(<@('+++++[>+++++++<-]>>+',;@((<'[>++++++++++')#~#)),[<@(']',~'<-','<.>'#~],[,])"0 #-i.@# )10#.inv])

This function generates nested BF loops based on the base10 digits of the input number. Checking all reasonable bases and choosing the smallest BF code would improve the score with a small amount.

BF programs for the test set:

   f10 every 55 68 15 28 841 838 522 846 4898 9004 9363 3810 13230 67175 37231 44701
+++++[>+++++++<-]>>+[>++++++++++[-<<.....>>]<-<.....>]                                                                                     
+++++[>+++++++<-]>>+[>++++++++++[-<<......>>]<-<........>]                                                                                 
+++++[>+++++++<-]>>+[>++++++++++[-<<.>>]<-<.....>]                                                                                         
+++++[>+++++++<-]>>+[>++++++++++[-<<..>>]<-<........>]                                                                                     
+++++[>+++++++<-]>>+[>++++++++++[>++++++++++[-<<<........>>>]<-<<....>>]<-<.>]                                                             
+++++[>+++++++<-]>>+[>++++++++++[>++++++++++[-<<<........>>>]<-<<...>>]<-<........>]                                                       
+++++[>+++++++<-]>>+[>++++++++++[>++++++++++[-<<<.....>>>]<-<<..>>]<-<..>]                                                                 
+++++[>+++++++<-]>>+[>++++++++++[>++++++++++[-<<<........>>>]<-<<....>>]<-<......>]                                                        
+++++[>+++++++<-]>>+[>++++++++++[>++++++++++[>++++++++++[-<<<<....>>>>]<-<<<........>>>]<-<<.........>>]<-<........>]                      
+++++[>+++++++<-]>>+[>++++++++++[>++++++++++[>++++++++++[-<<<<.........>>>>]<-<<<>>>]<-<<>>]<-<....>]                                      
+++++[>+++++++<-]>>+[>++++++++++[>++++++++++[>++++++++++[-<<<<.........>>>>]<-<<<...>>>]<-<<......>>]<-<...>]                              
+++++[>+++++++<-]>>+[>++++++++++[>++++++++++[>++++++++++[-<<<<...>>>>]<-<<<........>>>]<-<<.>>]<-<>]                                       
+++++[>+++++++<-]>>+[>++++++++++[>++++++++++[>++++++++++[>++++++++++[-<<<<<.>>>>>]<-<<<<...>>>>]<-<<<..>>>]<-<<...>>]<-<>]                 
+++++[>+++++++<-]>>+[>++++++++++[>++++++++++[>++++++++++[>++++++++++[-<<<<<......>>>>>]<-<<<<.......>>>>]<-<<<.>>>]<-<<.......>>]<-<.....>]
+++++[>+++++++<-]>>+[>++++++++++[>++++++++++[>++++++++++[>++++++++++[-<<<<<...>>>>>]<-<<<<.......>>>>]<-<<<..>>>]<-<<...>>]<-<.>]          
+++++[>+++++++<-]>>+[>++++++++++[>++++++++++[>++++++++++[>++++++++++[-<<<<<....>>>>>]<-<<<<....>>>>]<-<<<.......>>>]<-<<>>]<-<.>]          

Computing score on the test set:

   +/#@> f10 each 55 68 15 28 841 838 522 846 4898 9004 9363 3810 13230 67175 37231 44701
1481

randomra

Posted 2015-03-17T09:27:59.460

Reputation: 19 909

3

Pyth, 1702

Reconstruct numbers using factors of N + x.

+holN+]++">>"*"+"Q"<<"mjk(">>"j">"m*"+"kP+Qd"<[[->[->+>+<<]>>[-<<+>>]<<<]>[-]>[-<<+>>]<<<]>"*"-"d"<<")50"++++++[>++++++<-]>>[<.>-]"

orlp

Posted 2015-03-17T09:27:59.460

Reputation: 37 067

For 2 this outputs ++. now which doesn't print anything in BF. – randomra – 2015-03-17T17:00:55.217

@randomra Good catch, this happened while updating, I'll fix it, give me a few. – orlp – 2015-03-17T17:09:17.967

@randomra Should be fixed, made the score slightly higher (of course). – orlp – 2015-03-17T17:38:11.897

3

CJam, 52 74 108 bytes, total = 1304 1244 1210

ri5b_,1>{(_3<{\(@5*+}*\+}*W%)\{T+_2>:T5*-_0>"-+"=\z*}%\T+'+*a+W%{"[->+++++<]>"\}*">x[>x<-]<[->>.<<]"'x/'+6**

A test script (slow in the online interpreter):

q~]
{
_[0:T;
5b_,1>{(_3<{\(@5*+}*\+}*W%)\{T+_2>:T5*-_0>"-+"=\z*}%\T+'+*a+W%{"[->+++++<]>"\}*">x[>x<-]<[->>.<<]"'x/'+6**
]s
_[L:RL@0\
"-+><.]"['('){+\_{)}0?@\}{@\+\_{)}0?}{R1$c+:R;}]:`"]a"{{_aa+1$4G#%{:~~1}{;0}?}g}`+a+er:~:~
];R,@=!"Error."N+*o
}%s,

jimmy23013

Posted 2015-03-17T09:27:59.460

Reputation: 34 042

I didn't see the part about self-destruction. But it will never overflow anyway. – jimmy23013 – 2015-03-18T10:14:14.560

How does it work? – anatolyg – 2015-03-18T10:38:26.190

@anatolyg The first version simply generate the number in base 5. Later versions added a special case for the first two digits and also used decrement. – jimmy23013 – 2015-03-18T11:10:16.030

@user23013 Oh, sorry, haven't seen the spec changes. (Updated my answer accordingly.) – randomra – 2015-03-18T11:23:42.953

2

Befunge-98, N + 41, total = 193281

&>'+\:v
v^-1,\_
' >1-:v
>v^,+'_
,'    :
>ff3++^
>2f*+v
^>/9+:,
>'>,61v
, v*6+<
^/2,:<@
v >+2+,
>'<,']^

I know it's bad, but I felt like writing some Befunge today. The best part of Befunge is that the programs are even less understandible than the actual golf languages, especially when they reuse code :D

Uses a similar algorithm to Martin Büttner's CJam answer:

(N +'s)>+++++++++++++++++++++++++++++++++<[->.<]

PurkkaKoodari

Posted 2015-03-17T09:27:59.460

Reputation: 16 699

1

CJam, 40 + N, Total: 193265

'+33*'>'+l~*"[<.>-]"

Just to get this started, here is the baseline solution. It generates the following code:

+++++++++++++++++++++++++++++++++>_[<.>-]

where _ is N copies of +.

Run the generator here.

Martin Ender

Posted 2015-03-17T09:27:59.460

Reputation: 184 808

1

Befunge-93 - 24 + N, total = 193009

&>">>]-<]-<++++>[++++>[+++"v
v  ,,,,,,,,,,,,,,,,,,,,,,, <
>:v
,v_@
"1
.-
"
^<

This uses a prefix of +++[>++++[>++++<-]<-]>> to set the first tape index to '0' with 24 characters. The Befunge program is very basic and outputs that along with N '.' characters.

object

Posted 2015-03-17T09:27:59.460

Reputation: 11

Now that I see this I don't know why I was thinking my loop would be any better... – Martin Ender – 2015-03-17T17:02:47.800