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: , ,


Comments (7) -

Saran C
11/16/2016 7:03:36 AM #

Thanks for sharing your knowledge.


11/25/2016 5:11:40 PM #

Great post. Very innovative. Finding very hard to fully understanding but will through it again

Reply United States
12/29/2016 8:01:37 PM #

Just a little msg to compliment your labour, intriguing tips outlined in this article  My web-site - free Cop games; - ,


1/4/2017 10:33:11 AM #

Very interesting


1/4/2017 10:33:46 AM #

Inspiring. Go to:


kashmir websites
1/13/2017 10:22:10 AM #

Very nice and informative post it help me lots in solving my problems


Tabatha United States
3/7/2017 2:59:33 AM #

I read this article fully regarding the resemblance of latest and preceding technologies, it's remarkable article.  Look into my page ...  Tabatha - http://?????.??/index.php?option=com_k2&view=item&id=29:about-the-company&limitstart=2400


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