Esempio n. 1
0
        static void Main(string[] args)
        {
            SudokuWriter        sudokuWriter = new SudokuWriter();
            ImmutableSudokuGrid filledGrid   = sudokuWriter.CreateFilledGrid();

            filledGrid.PrintGrid();

            Console.WriteLine("\n");

            ImmutableSudokuGrid emptiedGrid = sudokuWriter.EmptyGridForHardSolve(filledGrid);

            emptiedGrid.PrintGrid();

            Console.WriteLine("\n");

            SudokuGrid   mutableEmptiedGrid = emptiedGrid.MakeMutableCopy();
            SudokuSolver sudokuSolver       = new SudokuSolver();

            sudokuSolver.Solve(mutableEmptiedGrid);
            mutableEmptiedGrid.PrintGrid();

            if (mutableEmptiedGrid.FindAllEmptySquares().Count != 0)
            {
                Console.WriteLine("\n");
                HarderSudokuSolver  harderSolver = new HarderSudokuSolver();
                ImmutableSudokuGrid solvedGrid   = harderSolver.Solve(emptiedGrid);
                solvedGrid.PrintGrid();
            }
        }
Esempio n. 2
0
        private static void Solve(List <string> boards, int maxSolutions, bool prettyPrint)
        {
            maxSolutions = Math.Abs(maxSolutions);

            var parser = new SudokuParser();
            var solver = new SudokuSolver();

            var boardsToSolve = new List <SolverResult>();

            foreach (var b in boards)
            {
                byte[] rawBoard;
                if (!parser.TryParse9X9Board(b, out rawBoard))
                {
                    WriteLine($"Failed to parse {b}");
                    continue;
                }

                var board = solver.CreateBoard(rawBoard);
                boardsToSolve.Add(new SolverResult()
                {
                    OriginalBoard = board
                });
            }

            var sw = new Stopwatch();

            WriteLine("Solving started...");

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            for (var i = 0; i < boardsToSolve.Count; ++i)
            {
                sw.Start();
                boardsToSolve[i].SolutionList = solver.FindAllSolutions(boardsToSolve[i].OriginalBoard, maxSolutions);
                sw.Stop();
                boardsToSolve[i].TotalDurationMs = sw.Elapsed.TotalMilliseconds;
                sw.Reset();
            }

            var messages = new List <string>();

            var totalSolutionCount = boardsToSolve.Aggregate(0, (total, item) => total + item.SolutionList.Count);
            var totalSolvingTime   = boardsToSolve.Aggregate(0.0, (total, item) => total + item.TotalDurationMs);

            messages.Add($"Finding {totalSolutionCount} solution for {boardsToSolve.Count} boards took {totalSolvingTime}ms");
            messages.Add($"Avg. solving time for single board is {totalSolvingTime / totalSolutionCount}ms");

            var writer = new SudokuWriter();

            boardsToSolve.ForEach(s =>
            {
                messages.Add($"Solutions found in {s.TotalDurationMs}ms");
                if (prettyPrint)
                {
                    messages.Add(writer.GetPrettyStringRepresentation(s.OriginalBoard));
                    s.SolutionList.ForEach(b => messages.Add(writer.GetPrettyStringRepresentation(b)));
                }
                else
                {
                    messages.Add(writer.GetStringRepresentation(s.OriginalBoard));
                    s.SolutionList.ForEach(b => messages.Add(writer.GetStringRepresentation(b)));
                }
            });

            messages.ForEach(WriteLine);
            messages.Clear();
        }
Esempio n. 3
0
        private static void CreateRandom()
        {
            // todo - board generation WIP
            var boardsToCreate   = 100;
            var maxCluesToAccept = 80;
            var solver           = new SudokuSolver();
            var writer           = new SudokuWriter();
            var sw       = new Stopwatch();
            var messages = new List <string>();

            var solverResults = new List <SolverResult>();

            Write("How many boards to create?: ");
            WriteLine("\b");

            while (true)
            {
                var line = ReadLine();
                int num  = 0;
                if (Int32.TryParse(line, out num))
                {
                    boardsToCreate = num;
                    break;
                }
            }

            Write("Maximum clues to accept?[17 - 80]: ");
            while (true)
            {
                var line = ReadLine();
                int num  = 0;
                if (Int32.TryParse(line, out num))
                {
                    if (num > 16 && num < 81)
                    {
                        maxCluesToAccept = num;
                        break;
                    }
                }
            }

            Clear();

            WriteLine($"Boards to create: {boardsToCreate}");
            WriteLine($"Maximum clues to accept: {maxCluesToAccept}");
            WriteLine("Starting...\n\n");

            while (solverResults.Count < boardsToCreate)
            {
                Beep(3000, 50); // it takes so long that I put this here
                sw.Start();
                var workingBoard = solver.CreateRandomBoard();
                sw.Stop();
                if (workingBoard.Item1.Cells.Where(c => c.HasValue).Count() > maxCluesToAccept)
                {
                    sw.Reset();
                    continue;
                }
                WriteLine(writer.GetStringRepresentation(workingBoard.Item1));
                Write('\a');
                solverResults.Add(new SolverResult()
                {
                    TotalDurationMs = sw.Elapsed.TotalMilliseconds,
                    OriginalBoard   = workingBoard.Item1,
                    SolutionList    = new List <Board>()
                    {
                        workingBoard.Item2
                    }
                });
                sw.Reset();
            }

            messages.Add($"Total time it took to create {boardsToCreate} boards is {solverResults.Aggregate(0.0, (t, i) => t + i.TotalDurationMs)}");
            messages.Add($"Avg. {solverResults.Aggregate(0.0, (t, i) => t + i.TotalDurationMs) / solverResults.Count}ms per board.\n");

            foreach (var result in solverResults)
            {
                messages.Add($"Created in {result.TotalDurationMs}ms, total cues: {result.OriginalBoard.Cells.Where(c => c.HasValue).Count()}");
                messages.Add($"{writer.GetStringRepresentation(result.OriginalBoard)}");
                messages.Add($"{writer.GetStringRepresentation(result.SolutionList.Single())}");
            }

            messages.ForEach(WriteLine);
        }