using System.Collections.Generic;
using System.Linq;

namespace Core
{
    public static class JangleWordScorer
    {
        private static readonly IDictionary<char, int> _scores = new Dictionary<char, int>(26);

        static JangleWordScorer()
        {
            foreach (var ch in "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
                switch (ch)
                {
                    case 'Z':
                    case 'J':
                    case 'Q':
                    case 'K':
                    case 'X':
                        _scores.Add(ch, 3);
                        break;
                    case 'H':
                    case 'B':
                    case 'Y':
                    case 'M':
                    case 'C':
                    case 'F':
                    case 'W':
                        _scores.Add(ch, 2);
                        break;
                    default:
                        _scores.Add(ch, 1);
                        break;
                }
        }

        public static int Score(string str)
        {
            return
                (from c in str.Canonicalize().ToCharArray()
                    let score = _scores[c]
                    select score).Sum();
        }

        public static IDictionary<int, IEnumerable<string>> GroupByScore(IEnumerable<string> st)
        {
            return
                (from s in st
                    let score = Score(s)
                    group s by score
                    into g
                    orderby g.Key descending
                    let vals = g.Select(v => v)
                    select new {g.Key, vals}).ToDictionary(d => d.Key, d => d.vals);
        }
    }
}