Hide a Brainf*** greeting in other code
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):
[] > +++++ +++
[ - < +++++ ++++ > ]
< ++++ .
+++++
> ++
[ - < +++++ +++++ > ]
< . ++ . -- . +++++ ++++ .
----
> +++++ +
[ - < ----- ----- > ]
< ... >>
+++++ +
[ - < +++++ > ]
< ++ . >>
+++++ +++++ ++
[ - < +++++ +++++ > ] <
- . >
+++++ +
[ - < ---- > ] <
++ . +++++ +++ .
> ++
[ - < +++++ > ]
< + . < . >>
+++
[ - < ----- > ] <
+ . +++++ ++++ . +++ .
< . > ----- ---- . +++++ +++++ + .
<< ... > . >>
++++
[ - < ---- > ] < .
--- . >
++++
[ - < ++++ > ]
< + .
> ++
[ - < +++ > ]
< + . < + .
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.
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/