There's an interesting, if now rather old, programming challenge on the Programming Puzzles & Code Golf StackExchange website. The task is to create the weirdest obfuscated "Hello World!" program using the language of your choice, but with the restriction that you cannot use any strings.
I don't have enough reputation on that website to post an answer to this question, so I thought I'd do it here instead. The code below shows my answer using C#, and it contains some of the nastiest code that I've ever written. Don't blame me if, once you've seen and understood this code, you find that you can never un-see it. Indeed, one colleague who mangled his brain on it told me "I don't think I can be your friend anymore." ☻
______________________________________________________________________________________________
using System; using System.Collections.Generic; namespace àáâãäå { class àáâãäå { [STAThread] static void Main() { int àáâãäå=83198361;int àáâãäå=12;int àáâãäå=1; àáâãäå=(àáâãäå+àáâãäå+àáâãäå)-(àáâãäå=àáâãäå)-(àáâãäå=àáâãäå); àáâãäå^=(àáâãäå^=àáâãäå^(àáâãäå^=àáâãäå^=àáâãäå))^(àáâãäå^=àáâãäå^=àáâãäå); Dictionary<int, Action>àáâãäå=new Dictionary<int, Action>() { {0,àáâãäå}, {1,àáâãäå}, {2,àáâãäå}, {3,àáâãäå}, {4,àáâãäå}, {5,àáâãäå}, {6,àáâãäå}, {7,àáâãäå}, {8,àáâãäå} }; Random àáãâäå=new Random(àáâãäå); for (;àáâãäå<=àáâãäå;àáâãäå++) { àáâãäå[àáãâäå.Next(0,àáâãäå)](); } Console.WriteLine(); } static void àáâãäå(object[] àáâãäå) { object àáâãäå = àáâãäå[0]; typeof(C\u006fnso\u006ce).\u0047etMet\u0068o\u0064s()[93].Invoke(àáâãäå,àáâãäå); } static void àáâãäå() { object[] àáâãäå={'\u0048'};àáâãäå(àáâãäå); } static void àáâãäå() { object[] àáâãäå={(char)101};àáâãäå(àáâãäå); } static void àáâãäå() { object[] àáâãäå ={'\x6c'};àáâãäå(àáâãäå); } static void àáâãäå() { object[] àáâãäå={'\u006f'};àáâãäå(àáâãäå); } static void àáâãäå() { object[] àáâãäå={(C\u0068ar)(26+6)};àáâãäå(àáâãäå); } static void àáâãäå() { object[] àáâãäå={(C\u0068a\u0072)(86+1)};àáâãäå(àáâãäå); } static void àáâãäå() { object[] àáâãäå={(char)(101+13)};àáâãäå(àáâãäå); } static void àáâãäå() { object[] àáâãäå={'\x64'};àáâãäå(àáâãäå); } static void àáâãäå() { object[] àáâãäå={'\x21'};àáâãäå(àáâãäå); } } }
______________________________________________________________________________________________
Explanation of the code:
- Yes, it really does compile and work.
- If you want to experiment with the code locally, just copy/paste it into a new Console application within Visual Studio.
- This answer is an evil mixture of:
-
- Michael Kaplan's "Normalization as Obfuscation" blog entry. Thanks to the wonders of Unicode normalization, the namespace, class, procedure names, and variables nearly all look like they have the same name. Visual Studio handles it just fine, and hopefully your browser will also display it properly.
- The random number idea from my Squirbling with FizzBuzz blog entry. But with a little twist that makes it much faster to converge on the right seed for the System.Random generator. It took my code less than 3 minutes on a single core to find a seed that generates the right indexes for the 12 characters that make up the "Hello World!" text.
- The idea of using Unicode escape sequences in C# identifiers. Naughty, but nice.
- Swapping variable contents without using a temporary variable - the two lines after the inital declarations.
- This code will likely only work with .NET Framework 4.5, because of that obfuscated Console.GetMethods lookup. It uses a constant index into the list of Console.Write overloads to find the right overload for printing Char[]. When I check version 4.0, I can see that it has a different index for this overload, and future versions will probably also change that index. That will stop this code compiling.
- Can this code be made even more evil? I think so...
- Yes, this definitely made me squirble - but this time the rubber bands held.