コード例 #1
0
        protected override void Solve(out string answer)
        {
            string dataFile    = "ProblemData\\p022_names.txt";
            var    data        = File.OpenText(dataFile);
            var    namesString = data.ReadToEnd();

            data.Close();

            //Extract names into an array of strings
            var names = namesString.Split(',').Select(t => new NameObject {
                Name = t.Trim('"')
            }).ToArray();

            var sortedNames = names.OrderBy(t => t.Name).ToArray();

            //Short test of alphabetical value.
            var test = new NameObject {
                Name = "COLIN"
            };
            var testValue = test.AlphabeticalValue;

            //Calculate the sum of all scores.
            var totalValue = 0;
            var currentPos = 1;

            foreach (var name in sortedNames)
            {
                totalValue += name.AlphabeticalValue * currentPos;
                currentPos++;
            }

            answer = string.Format("Total sum of all the name scores in the file is {0}.", totalValue);
        }
コード例 #2
0
        internal static int GetAlphabeticalValue(this NameObject me)
        {
            var value = me.Name.Select(c => c - 'A' + 1).Sum();

            return(value);
        }