public string ValidateAndGetSolution(int numColors, List <CustomerPreference> candidate,
                                             IEnumerable <IEnumerable <CustomerPreference> > customerPreferences)
        {
            // colors is the working array, is created with numColors cells initialized to null
            var colors = new CustomerPreference[numColors];

            for (var i = 0; i < numColors; i++)
            {
                colors[i] = null;
            }

            bool found = false;

            for (var i = 0; i < candidate.Count; i++)
            {
                var preference = candidate[i];
                if (
                    colors[preference.Color - 1] != null &&
                    (colors[preference.Color - 1].IsReadOnly || preference.IsReadOnly) &&
                    colors[preference.Color - 1].Finish != preference.Finish)
                {
                    found = false; // stop iteration
                    break;
                }

                colors[preference.Color - 1] = preference;
                found = true;
            }

            if (!found)
            {
                return(string.Empty);
            }

            var result = new string[numColors];

            for (int i = 0; i < colors.Length; i++)
            {
                var c = colors[i];

                if (c != null)
                {
                    result[i] = c.Finish;
                }
                else
                {
                    result[i] = "G";
                }
            }

            // verifies that the current solution is satisfying all customers
            if (!IsSatisfyingAllPreferences(result, customerPreferences))
            {
                return(string.Empty);
            }

            return(string.Join(" ", result));
        }
        int SortColor(CustomerPreference a, CustomerPreference b)
        {
            if (a.Finish == "G" && b.Finish == "M")
            {
                return(-1);
            }
            if (a.Finish == "M" && b.Finish == "G")
            {
                return(1);
            }

            return(a.Color - b.Color);
        }
        public ParsedFileObject ParseFile(string filePath)
        {
            try
            {
                ParsedFileObject parsedFileObject;

                FileInfo fileInfo = new FileInfo(filePath);
                string   fileLine;
                Regex    regexNumber    = new Regex(@"^\d$");
                int      numberOfColors = 0;
                char[]   result         = new char[0];


                StreamReader file =
                    new StreamReader(filePath);
                Regex         linePattern   = new Regex(@"^[GM1-9 ]+$");
                List <string> customerLines = new List <string>();

                string colorLine = string.Empty;

                while ((fileLine = file.ReadLine()) != null)
                {
                    Console.WriteLine(fileLine);

                    if (regexNumber.IsMatch(fileLine))
                    {
                        colorLine = fileLine.Trim();
                    }

                    else if (!string.IsNullOrEmpty(fileLine) && linePattern.IsMatch(fileLine))
                    {
                        fileLine = fileLine.Trim();
                        if (!string.IsNullOrEmpty(fileLine))
                        {
                            customerLines.Add(fileLine);
                        }
                    }
                }

                file.Close();

                int.TryParse(colorLine, out numberOfColors);

                parsedFileObject = new ParsedFileObject();
                parsedFileObject.NumberColors = numberOfColors;

                List <CustomerPreference> customerPreferences;

                foreach (var line in customerLines)
                {
                    customerPreferences = new List <CustomerPreference>();
                    CustomerPreference customerPreference;

                    var           splitLine       = Regex.Split(line, @"\s+");
                    List <string> nonEmptyOptions = new List <string>();
                    foreach (var item in splitLine)
                    {
                        if (!string.IsNullOrEmpty(item[0].ToString()))
                        {
                            nonEmptyOptions.Add(item);
                        }
                    }

                    customerPreference = new CustomerPreference();

                    foreach (var item in nonEmptyOptions)
                    {
                        if (item == "G" || item == "M")
                        {
                            customerPreference.Finish     = item;
                            customerPreference.IsReadOnly = false;
                            customerPreferences.Add(customerPreference);
                            customerPreference = new CustomerPreference();
                        }
                        else
                        {
                            customerPreference.Color = int.Parse(item);
                        }
                    }

                    parsedFileObject.CustomerPreferences.Add(customerPreferences);
                }
                return(parsedFileObject);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
        }