Code Golf: 2014 In ColdFusion
Over the weekend, I happened upon this Stack Exchange entry asking people to try and output the value "2014" without actually using any numbers. Apparently there is this thing called "Code Golf" in which developers try to do carry-out some task with the smallest amount of code possible. This is new to me, but it sounds exciting. I figured I'd give it a try. Well, not the "least amount of characters" aspect - just the trying to output 2014 without numbers.
All of the following output 2014:
<cfscript>
// Inspired by this entry on Stack Exchange (via Hacker News):
// http://codegolf.stackexchange.com/questions/17005/produce-the-number-2014-without-any-numbers-in-your-source-code
// --
// So, now that it's 2014, it's time for a code question involving
// the number 2014. Your task is to make a program that prints the
// number 2014 without using any of the characters 0123456789 in
// your code. The shortest code (counting in bytes) to do so in any
// language in which numbers are valid tokens wins.
// Simply logs all of the arguments to output.
// --
// NOTE: This funciton uses a "1", however, since its just a
// utility function for writeOutput(), I'm not going ot count it.
function print( arg1, arg2, arg3, arg4 ) {
for ( var i = 1 ; i <= arrayLen( arguments ) ; i++ ) {
writeOutput( arguments[ i ] );
}
writeOutput( "<br />" );
}
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Outputting the current year is definitely the most low-hanging
// fruit approach to the problem.
print( year( now() ) );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// While we can't use numbers, we certainly can generate numbers
// based on other values.
print(
len( "xx" ),
len( "" ),
len( "x" ),
len( "xxxx" )
);
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// If we know what the Base64-encoded version of 2014 is, we
// can reverse engineer the value.
print( toString( toBinary( "MjAxNA==" ) ) );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Similarly, if we know the encrypted value, we can just decrypt
// it, assuming the encrypted value contains no numbers!
print( decrypt( "$##RL.+P", "you sexy beast" ) );
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Each character can be represented by a decimal value. As such,
// we can use the "char" versions of said numbers to produce
// the numerical equivalents for some math.
print(
( asc( "<" ) - asc( "(" ) ),
( asc( "." ) - asc( " " ) )
);
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// Another way to generate numbers is to find the location of
// substrings within larger strings.
print(
find( "sexy", "How you doing, you sexy lady?!" ),
find( "troll", "Ewwwwww, you troll! Get away from me!" )
);
// ------------------------------------------------------ //
// ------------------------------------------------------ //
// By using mathematical operations, we can force ColdFusion to
// cast boolean values into ones and zeros.
print(
( true + true ),
( true * false ),
( false + true ),
( true + true + true + true )
);
</cfscript>
I thought maybe there would be some interesting bit-shifting I could do. But 1) I'm not that good at bits and, 2) all of the bit-shift functions in ColdFusion require actual numbers as part of their arguments.
Want to use code from this post? Check out the license.
Reader Comments
I take it #Year(Now())# is cheating then.
@Ross,
I don't think so - no numbers, no cheating :D
I'm sure you'll be happy to learn, today's xkcd cartoon is on regex golf:
http://www.xkcd.com/1313/
Here's another one:
<cfset Code = "Golf" />
<cfoutput>
#Find('o', Code)##Find('x', Code)##Find('G', Code)##Find('f', Code)#
</cfoutput>
The shortest I can think of is just:
asc('-')
Hey Ben, it's been a while.
<cfscript>
i = val('a');
writeOutput((++i + i++ & --i - i-- & ++i & ++i + i++ ));
</cfscript>
@Yaron,
That one hurt my head :)
Nice job, @Ben! I don't think that's the smallest amount of code with all of your comments, though. ;) Just kidding. I've always liked all your comments. I can't come up with anything better or shorter.