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)
                    p.Add(f[l++]);

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

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

Tags: , ,

Coding4Fun

Comments (6) -

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

Thanks for sharing your knowledge.

Reply

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

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

Reply

http://gipperich-gipprich-wiki.com/ 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;  http://gipperich-gipprich-wiki.com/ - gipperich-gipprich-wiki.com/index.php ,

Reply

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

Very interesting

Reply

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

Inspiring. Go to: https://www.techniquehow.com/

Reply

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

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

Reply

Add comment

Marcel Veldhuizen

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

Metal enthusiast; I love going to concerts :)

 

RecentPosts

Recent Tweets

Note: For Customization and Configuration, CheckOut Recent Tweets Documentation