Vasu Balakrishnan’s Blog

Project Euler #22

leave a comment »


Problem: What is the total of all the name scores in the file of first names?

Answer: 871198282

Solution: It was quite easy to implement with LINQ. This goes to show the power of LINQ and the readability of a Functional Program.

var lContent = ReadFile("Problem22.txt");
var lWords = lContent
    .Replace("\"", "")
    .Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

var lNumWords = lWords
    .OrderBy(pArg => pArg)
    .Select((pString, pIndex) =>
            (pIndex + 1) * pString.Sum(pCharArg => pCharArg.ToOrdinal())

var lAnswer = lNumWords.Sum();

Time: 59 ms


Written by nbvasu

May 15, 2009 at 2:40 pm

Posted in C#, Euler, General

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: