13
\$\begingroup\$

The goal is to write a program (anything except explicit brainfuck) which prints Barney Stinson's best rule:

New is always better.

when interpreted normally, but

Legen... wait for it... dary!

When processed with a Brainfuck interpreter.

May the most popular bro win. You have 72 hours to be legendary.

\$\endgroup\$
  • \$\begingroup\$ Not sure code-golf is the best tag for this challenge. IMHO popularity-contest would be better. \$\endgroup\$ – Florent Feb 25 '14 at 10:10
  • 3
    \$\begingroup\$ Whose Barney? The purple dinosaur? Fred's BFF? \$\endgroup\$ – user12183 Feb 25 '14 at 10:14
  • 1
    \$\begingroup\$ @LowerClassOverflowian How I met Your Mother character, see edit \$\endgroup\$ – Thomas Ayoub Feb 25 '14 at 10:15
  • 4
    \$\begingroup\$ Can someone please create the tag "bro-contest"? \$\endgroup\$ – Pierre Arlaud Feb 25 '14 at 10:53
  • 1
    \$\begingroup\$ @Florent if the challenge is intended to be won by the shortest answer, I don't think you should retag it to a popularity contest. Samoth, in case you don't know, in a popularity contest the winner is chosen by voters - the answer with the highest vote wins. If you want your contest to be won by the answer with shortest code, please retag your question as a code-golf, and if you want it to be a popularity contest, please edit your question. \$\endgroup\$ – ace Feb 25 '14 at 11:11
11
\$\begingroup\$

C#

So I decided to get a little more... creative with my entry. The Brainfuck code is embedded into the C# as the various operators (not in comments or in hardcoded strings), while the C# code itself operates on a similar principal to the way Brainfuck prints characters (meaning there are no hardcoded strings, and C# generates each individual character as an integer then casts it to a char when printing).

I'm sure I could have made this more elegant, but it took me long enough to finish as it is.

Everything was tested on this JavaScript Brainfuck interpreter by Cal Henderson and with Visual Studo 2012's C# compiler (targeted at .NET framework v4.0).

using System;

namespace Polyglot
{
    internal static class Program
    {
        private static void Main()
        {
            var a = new short[50];

            short _1 = 72;
            short _2 = 0;
            short _3 = 0;
            short _4 = 0;
            short _5 = 0;
            short _6 = 0;
            short _7 = 97;
            short _8 = 0;
            short _9 = 0;
            short _10 = 0;
            short _11 = 0;
            short _12 = 0;
            short _13 = 0;
            short _14 = 0;
            short _15 = 0;
            short _16 = 46;
            short _19 = 0;
            short _20 = 0;
            short _21 = 0;

            if( 0 >= 0 )
            {
                ++_1;
                ++_1;
                ++_1;
                ++_1;
            }

            a[ -1 < 0 ? 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 : 0 + 1 > 1 ? 0 : 0 ] = 9001;

            if( 7 < 42 )
            {
                ++_1;
                ++_1;
            }

            Console.Write( (char)_1 );

            _2 = 101;
            _1 += 1 + 1 + 1 + 1 + 1;

            _3 = 42 > 7 ? 110 + 9 : 1 + 1;

            a[ -1 < 0 ? 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 : 0 > 0 ? 0 : 0 ] = 9001;

            if( 1 < 2 )
                _4 = 32;

            Console.Write( (char)_2 );

            ++_1;
            _5 = 105;

            Console.Write( (char)_3 );

            --_1;

            _6 = 115;

            Console.Write( (char)_4 );

            ++_1;
            ++_1;
            ++_1;
            _1 += 1 + 1 + 5;

            Console.Write( (char)_5 );

            --_1;
            --_1;

            if( 42 > 41 )
            {
                ++_1;
                ++_1;
                ++_1;
            }

            a[ -1 < 0 ? 0 : 10 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 > 5 ? 0 : 0 ] = 9001;

            if( 42 < 9001 )
                Console.Write( (char)_6 );

            Console.Write( (char)_4 );
            Console.Write( (char)_7 );

            if( 12 > 11 && 11 > 10 )
            {
                _8 = 108;
                _9 = _3;
            }
            else
            {
                ++_1;
                ++_1;
                ++_1;
            }

            a[ -1 < 0 ? 1 + 1 + 1 + 1 + 1 + 1 : 1 > 2 ? 0 : 0 ] = 9001;

            if( _4 < _1 )
            {
                ++_1;
                Console.Write( (char)_8 );
            }
            else if( _4 > _19 && _2 > _20 )
            {
                ++_21;
                ++_21;
                ++_21;
                ++_21;
                ++_21;
                _21 += (short)( _21 + 36 );
            }

            a[ -7 < 9 ? 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 : 4 > 8 ? 9 : 9 ] = (short)( 12 < 10 ? -_4 : _9 );
            Console.Write( (char)_9 );

            if( _4 > _9 )
            {
                ++_1;
                ++_1;
                ++_1;
            }

            a[ -9 < 7 ? 10 - 1 - 1 - 1 - 1 : 6 > 7 ? 0 : 0 ] = _21;

            if( 1 < 0 )
                return;
            else
            {
                ++_1;
                Console.Write( (char)_7 );
            }

            _10 += 5 + 4 + 1 + 2 + 8 + 9 + 1 + 91;
            Console.Write( (char)_10 );

            if( 10 > _4 ) ++_21; else a[ -0 < 0 ? 5 + 6 + 1 + 2 + 3 + 9 : 1 > 2 ? 0 : 9 ] = 50;

            if( _21 <= _4 )
                _11 = 58 + 57;

            Console.Write( (char)_11 );

            if( _2 <= _8 )
                Console.Write( (char)_4 );
            else if( 1 >= 2 )
                return;
            else if( 42 >= _4 )
                _1 += ++_21;
            else
            {
                a[ -99 < --_1 ? --_1 - _1 : 44 > 12 ? 9 : 7 ] = (short)( _2 < _4 ? _21 : 6 );
                throw new Exception();
            }

            switch( _4 )
            {
                case 32:
                    var x = (char)( (short)( _4 + 66 ) );
                    Console.Write( x );
                    break;
                default: break;
            }

            _12 += (short)( ++_12 + ( ++_1 ) + 1 + 1 + 1 );
            Console.Write( (char)_12 );

            _13 += (short)( 39 + 38 + 39 );
            Console.Write( (char)_13 );

            if( _12 < _13 )
                Console.Write( (char)_13 );

            if( _13 >= _4 )
            {
                _14 = (short)( 500 - ( - ( 50 ) ) - ( --_1 ) - 90 - ( -4 ) - 267 );
                Console.Write( (char)_14 );
            }

            switch( _1 )
            {
                case 52:
                    _15 += (short)( ++_1 + ( ++_21 ) );
                    break;
                default:
                    _15 += (short)( 15 + ( ++_1 ) + 2 );
                    break;
            }

            Console.Write( (char)_15 );

            if( _16 <= 3521 && _21 < _4 )
                Console.WriteLine( (char)_16 );

            _16 = (short)( Int16.Parse( "54" ) % 2 );
            _20 = (short)( Int16.Parse( "99" ) / ( _1 > _4 ? 3 : 0 ) );
            _1 = (short)( 02.23 );

            if( _16 > 9 || _20 >= 52 )
                _1 += (short)( ( ++_1 ) + _21 );

            a[ -0 < 0 ? -52 - ( --_20 ) : 1 > 0 ? 1 : 2 ] = (short)( _12 < _19 ? Int16.Parse( "19" ) : 44 );
            _12 -= (short)f( --_19 / 19.467d );

            if( _12 > _14 )
                _19 += (short)( _19 + 1 + _3 + 5 );

            a[ -904 < 409 ? 4 + ( ++_4 ) + 4 : 49 > 50 ? 49 : 50 ] = (short)( 50 < 99 ? _4 + 669.2452 : 0 );

            if( 44 > ++_4 )
                a[ -9 < 6 ? 6 + ( ++_4 ) : 9 > 2 ? 44 : 8 ] = 3;
        }

        private static double f( double x )
        {
            return x < 12 ? x + 13.22 : x < 6 ? x + 90.45 : 5555; 
        }
    }
}

This is the original Brainfuck code I wrote for this challenge:

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

When running the C# code through a Brainfuck interpreter, you end up with the following commands (notice the addition of the square brackets at the beginning, these are from the array declaration and don't do anything, since the cell under the memory pointer in the array will already be 0):

[] > +++++ +++
[ - < +++++ ++++ > ]
< ++++ .
+++++
> ++
[ - < +++++ +++++ > ]
< . ++ . -- . +++++ ++++ .
----
> +++++ +
[ - < ----- ----- > ]
< ... >>
+++++ +
[ - < +++++ > ]
< ++ . >>
+++++ +++++ ++
[ - < +++++ +++++ > ] <
- . >
+++++ +
[ - < ---- > ] <
++ . +++++ +++ .
> ++
[ - < +++++ > ]
< + . < . >>
+++
[ - < ----- > ] <
+ . +++++ ++++ . +++ .
< . > ----- ---- . +++++ +++++ + .
<< ... > . >>
++++
[ - < ---- > ] < .
--- . >
++++
[ - < ++++ > ]
< + .
> ++
[ - < +++ > ]
< + . < + .
\$\endgroup\$
6
\$\begingroup\$

Smalltalk

"adding adding all the time"
"1+1+1+1+1+1+1+1+1+1+1" "is not fine"
"more to come / going far" 
"+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1"
"all that work for a single char"

"happy me / a loop to start" 
"[>++>+++>++++>+++++<<<<-]"
"was it code or just a fart?"
"adding adding / thats not fine"
"+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1"

">>+" 'New is always better.' "shall I " print
">+.++.--.+++++++++.----------.<<----...<+++++++"
"the garbage here gives us no hint"
".>>+++++++++++.>---.++++++++" 

"hurray a change to stop the pain" 
".>---------.<<<<.>>" 
"the author now has in his brain"
"-----------------.>>-----.+++.<<<<.>>>.>++."
"We all agree the time is here"
"<<<...<.>>--.>--------.>--.+++++++.<<<<."
"to finish this and have a beer"

type into a workspace and press "doIt", or send to BF. BF stolen from the other posters - thanks.

\$\endgroup\$
4
\$\begingroup\$

Javascript / Brainfuck

Ungolfed version as it is not a code-golf contest :

var outputString='';
'+++++++@++++[>++@/+@@>@/+@+@++>++++++@/+>+@++@/+@@+++++@/>@+@+++++@/+++@+<@/<<<<-]>>>@-.>++.+@/+@@.--.>.<-@/.@<@<++...<-.@/>>>>+++++@++++.<-@/-@-.@+@/+@+@+++++@/.>-@--@/.<<<<.>>>@---.>---@/-@@-@/.@+@++.<<<@/<@.@>>>+++@/.@@>@/+@+@.<<<@/...<@.>>>--@/---.---.>--.+++++++.<<<<+.<'
  .split('/')
  .forEach(function(e) {
    var matched=e.match(/[^@]*@/g);
    if (matched) {
      var asciiCode=0;
      for(var i=0;i<matched.length;i++) {
        asciiCode=matched[i-0].length-1+10*asciiCode;
      }
      outputString+=String.fromCharCode(asciiCode);
    }
  });
  alert(outputString);

That was the occasion for me to learn BF and that was fun :)
The difficulty was to never use , as BF interprets it as user input and be sure that byte is 0 before using [] for accessing elements of arrays.
The JS encoding is pretty simple, each strings before / encode a character and each string length before @ defines ASCII code in decimal. (+++++++@++++[>++@ = 78 = N)

You can test Brainfuck here : http://copy.sh/brainfuck/

\$\endgroup\$
3
\$\begingroup\$

Cubically (TIO) / BF (TIO)

+5+1/1+5+3@6:2/1+5+5+1@6+2@6:5/1+3@6:5+1/1+5+5+1@6:5+2/1+5+5+2@6:5/1+3@6:5+2/1+5+5@6:5+5+2@6:2/1+5+53[@6>:5+2/1+5+5@6>:4/1+5+5+3@6+>:5+2/1+55+2@6:5/1+3@6>:5+3/1+55@6>:2/1+551@6<<<<<-]>:5+3./1+552-@66>+.:2/1+5+51.@6--.>:5+1/1+5+5+2@6:1/1+5@6+++++.>----...>+++++++.<<+++++++++.<----.++++++++.>---.>>.<<<---.>-----.+++.>>.<<<+++.>++.>...>.<<<-----.---.>--.+++++++.>>+.

(Updated 8/4/17 to account for language changes in Cubically)

Basically this just uses as many characters from the BF program as it can in the Cubically program, then sticks the rest of the BF program on the end of it. No forced termination with &/E in the Cubically program is necessary.

\$\endgroup\$
2
\$\begingroup\$

Python / BrainF**k, 362 chars

BrainF**k code taken from Clément Renaud's deleted answer.

Edit - Initial null character no longer printed.

t='++++++++++_+++++++|^+++++++|+[>++>+++%>++++>+++++<<<<-||]+++++++++++|*+++++++++++++@+>>+.>+||.++.|--.+++++++++.--|^--------.<<----..._<+++++|++.>|>+++++&++++++.>---.+=+++++++|.>---------.<<^<<.>>--||-----|--~-----|---.>>-----~.+++.<<<<.>|>>.*>++.<<<...<.>>--._>|--------|.>-^-.+++++++.<<<<!!!'
print ''.join((chr(93+len(x))if x else' ')for x in t.split('|'))

Uses a very simple trick - | characters were added to the BF program, and the distances between them encode the string printed in Python. The Python code is mostly ignored by BF.

\$\endgroup\$
  • \$\begingroup\$ Is it ok to print the '\0' at the beginning? \$\endgroup\$ – V-X Feb 25 '14 at 16:40
  • \$\begingroup\$ @V-X, Perhaps it isn't. The BF code is copied, and I noticed that .join added a ., but didn't see any harm (because \0 is invisible). The + is and .split are OK - I removed the characters from the original. \$\endgroup\$ – ugoren Feb 25 '14 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.