Example #1
0
        static bool EvaluateEquation(List <MatchDigit> origEquation)
        {
            List <int> equationSubs  = new List <int>();
            int        currentValue  = 0;
            char       currentSymbol = '+';

            for (int i = 0; i < origEquation.Count; i++)
            {
                MatchDigit currentDigit = origEquation[i];
                if (currentDigit.DigitType == DigitType.Number)
                {
                    if (currentSymbol == '+')
                    {
                        currentValue += currentDigit.ToInteger();
                    }
                    else if (currentSymbol == '-')
                    {
                        currentValue -= currentDigit.ToInteger();
                    }
                    else
                    {
                        equationSubs.Add(currentValue);
                        currentValue = currentDigit.ToInteger();
                    }
                }
                else
                {
                    currentSymbol = currentDigit.ToSymbol();
                }
            }
            equationSubs.Add(currentValue);

            if (equationSubs.Count < 2)
            {
                return(false);
            }
            foreach (int subExpValue in equationSubs)
            {
                if (subExpValue != equationSubs.First())
                {
                    return(false);
                }
            }
            return(true);
        }
Example #2
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Usage: MathSolver <equation> [--verbose]");
                Environment.Exit(-1);
            }
            else
            {
                var argsList = args.ToList();
                if (argsList.Contains("--verbose"))
                {
                    verbose = true;
                    argsList.Remove("--verbose");
                }
                inputEquation = argsList.FirstOrDefault();
            }
            if (String.IsNullOrWhiteSpace(inputEquation))
            {
                Console.WriteLine("An equation is required.");
                Environment.Exit(-1);
            }

            List <MatchDigit> origEquation = ConstructEquation(inputEquation);

            for (int firstDigitIndex = 0; firstDigitIndex < origEquation.Count; firstDigitIndex++)
            {
                MatchDigit matchDigitToRemoveFrom = origEquation[firstDigitIndex];


                for (int i = 0; i < matchDigitToRemoveFrom.MatchPositions.Count; i++)
                {
                    if (matchDigitToRemoveFrom.MatchPositions[i] == true)
                    {
                        matchDigitToRemoveFrom.MatchPositions[i] = false;
                        for (int secondDigitIndex = 0; secondDigitIndex < origEquation.Count; secondDigitIndex++)
                        {
                            MatchDigit matchDigitToAddTo = origEquation[secondDigitIndex];


                            for (int j = 0; j < matchDigitToAddTo.MatchPositions.Count; j++)
                            {
                                if (i == j)
                                {
                                    continue;
                                }
                                if (matchDigitToAddTo.MatchPositions[j] == false)
                                {
                                    matchDigitToAddTo.MatchPositions[j] = true;
                                    if (matchDigitToAddTo.IsValid() && matchDigitToRemoveFrom.IsValid())
                                    {
                                        if (verbose)
                                        {
                                            Console.WriteLine(PrintEquation(origEquation, firstDigitIndex, i, secondDigitIndex, j));
                                        }
                                        if (EvaluateEquation(origEquation))
                                        {
                                            Console.WriteLine("SOLVED: " + PrintEquation(origEquation, firstDigitIndex, i, secondDigitIndex, j));
                                        }
                                    }
                                    matchDigitToAddTo.MatchPositions[j] = false;
                                }
                            }
                        }
                        matchDigitToRemoveFrom.MatchPositions[i] = true;
                    }
                }
            }

            Console.WriteLine("Done.");
        }