Code Golf

by Marcel Veldhuizen 18. February 2013 14:13

Every now and then I just want to do something silly with code, like some code golf. Just for a moment to forget about best practices and write the shortest possible program to solve a problem.

Today I happened to run into one of those projects on my hard disk, which happened to be in C# (it's not exactly the tersest language there is, so it may be a little unusual). It's an exercise from an old Google Code Jam. The problem to be solved and some solutions can be found in this StackOverflow question, including my own. It measures 398 characters after removing insignificant whitespace, the longest solution between a host of different languages.

Today I realized that there was a slightly shorter solution than the one below, but perhaps you can come up with something even better?

namespace System
    class P
        static void Main(string[]a)
            int c = 0, n, m, d, l = 1;
            var f = IO.File.ReadAllLines(a[0]);

            while (c < int.Parse(f[0]))
                var o = f[l++].Split(' ');
                n = int.Parse(o[0]);
                m = int.Parse(o[1]);
                var p = new Collections.Generic.HashSet<string>();

                while (n-- > 0)

                while (m-- > 0)
                    for (o = f[l++].Split('/'), d = 0; d++ < o.Length; )
                        if (p.Add(string.Join("/", o, 0, d)))

                Console.Write("Case #{0}: {1}\n", ++c, n);

Tags: , ,


Add comment

Marcel Veldhuizen

Software Architect by profession, mostly focused on Microsoft .NET technology.

Metal enthusiast; I love going to concerts :)



Recent Tweets

Note: For Customization and Configuration, CheckOut Recent Tweets Documentation