﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pasteles
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " - Inicio" + Environment.NewLine);
            int tastyCakes  = GetNumberOfCakes("CDFCDFCDF"); // no Masita (*)
                tastyCakes += GetNumberOfCakes("MDFCDFCDF"); // no Confite
                tastyCakes += GetNumberOfCakes("CMFCDFCDF"); // no Dulce
                tastyCakes += GetNumberOfCakes("CDMCDFCDF"); // no Fruta
            Console.WriteLine(Environment.NewLine + "Pasteles Ricos: " + tastyCakes + Environment.NewLine);
            Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " - Fin");
            Console.ReadLine();
        }
        
        static int GetNumberOfCakes(string ingredients)
        {
            string str = ingredients;
            List<String> allCakes = new List<string>();
            String currentCake;
            int[] inds = new int[9];

            Console.Write(DateTime.Now.ToString("HH:mm:ss") + " Analizando : " + ingredients);

            for (inds[0] = 0; inds[0] < 9; inds[0]++)
            {
                for (inds[1] = 0; inds[1] < 9; inds[1]++)
                {
                    if (HasDuplicates(inds, 1)) continue;
                    for (inds[2] = 0; inds[2] < 9; inds[2]++)
                    {
                        if (HasDuplicates(inds, 2)) continue;
                        for (inds[3] = 0; inds[3] < 9; inds[3]++)
                        {
                            if (HasDuplicates(inds, 3)) continue;
                            for (inds[4] = 0; inds[4] < 9; inds[4]++)
                            {
                                if (HasDuplicates(inds, 4)) continue;
                                for (inds[5] = 0; inds[5] < 9; inds[5]++)
                                {
                                    if (HasDuplicates(inds, 5)) continue;
                                    for (inds[6] = 0; inds[6] < 9; inds[6]++)
                                    {
                                        if (HasDuplicates(inds, 6)) continue;
                                        for (inds[7] = 0; inds[7] < 9; inds[7]++)
                                        {
                                            if (HasDuplicates(inds, 7)) continue;
                                            for (inds[8] = 0; inds[8] < 9; inds[8]++)
                                            {
                                                if (HasDuplicates(inds, 8)) continue;

                                                currentCake = "";
                                                foreach (int i in inds)
                                                    currentCake += str[i];

                                                if (!allCakes.Contains(currentCake))
                                                    allCakes.Add(currentCake);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            int tastyCount = (from s in allCakes where IsTasty(s) select s).Count();

            Console.WriteLine("  -> Pasteles: {0} - Ricos: {1} ", allCakes.Count, tastyCount);

            return tastyCount;
        }

        static bool HasDuplicates(int[] indexes, int count)
        {
            for (int i = 0; i < count; i++)
                if (indexes[i] == indexes[count])
                    return true;
            return false;
        }

        static bool IsTasty(string s)
        {
            return AreThreeInRow(s[0], s[1], s[2]) || AreThreeInRow(s[3], s[4], s[5]) || AreThreeInRow(s[6], s[7], s[8])
                || AreThreeInRow(s[0], s[3], s[6]) || AreThreeInRow(s[1], s[4], s[7]) || AreThreeInRow(s[2], s[5], s[8]);
        }

        static bool AreThreeInRow(char a, char b, char c)
        {
            List<char> chars = new List<char>() { a, b, c };
            chars.Sort();
            String s = "" + chars[0] + chars[1] + chars[2];
            return (s == "CCM") || (s == "DDM") || (s == "FFM") || (s == "CCC") || (s == "DDD") || (s == "FFF");
        }
    }
}
