public AlphameticEquationSolverHelper(AlphameticEquation equation, bool allowRepeatingNumbers, bool allowLeadingZeros, bool findOneSolutionOnly) { Equation = equation; AllowRepeatingNumbers = allowRepeatingNumbers; AllowLeadingZeros = allowLeadingZeros; FindOneSolutionOnly = findOneSolutionOnly; CreateLetterNumberPairs(); }
public void SolveEquation(AlphameticEquation equation, bool allowRepeatingNumbers, bool allowLeadingZeros, bool findOneSolutionOnly, Action <AlphameticEquation, Dictionary <char, byte> > onSolutionFound) { Equation = equation; AlphameticEquationSolverHelper[] alphameticEquationSolverHelpers = new AlphameticEquationSolverHelper[Environment.ProcessorCount]; Parallel.ForEach(alphameticEquationSolverHelpers, (alphameticEquationSolverHelper, state, index) => { alphameticEquationSolverHelper = new AlphameticEquationSolverHelper(equation, allowRepeatingNumbers, allowLeadingZeros, findOneSolutionOnly); long triesPerProcessor = (long)Math.Pow(10, alphameticEquationSolverHelper.GetCountOfUniqueLetters()) / Environment.ProcessorCount; alphameticEquationSolverHelper.Solve(index * triesPerProcessor, (index + 1) * triesPerProcessor, onSolutionFound); if (findOneSolutionOnly) { state.Break(); } }); }
public static void PrintSolution(AlphameticEquation equation, Dictionary <char, byte> letterNumberPairs) { lock (printLock) { string solution = AlphameticEquationSolverHelper.TranslateAlphametic(equation.ToString(), letterNumberPairs); Console.WriteLine(equation.ToString()); Console.WriteLine(solution); Console.WriteLine(); PrintDictionary(letterNumberPairs); Console.WriteLine(); for (int i = 0; i < solution.Length; i++) { Console.Write("-"); } Console.WriteLine("\n"); } }