Ejemplo n.º 1
0
        public void Solve()
        {
            var file = new FileStream("Assets\\sudoku.txt", FileMode.Open);
            var puzzles = new List<SudokuPuzzle>();

            #region SETUP
            using (var reader = new StreamReader(file))
            {
                while (!reader.EndOfStream)
                {
                    var puzzle = new SudokuPuzzle();
                    puzzle.Name = reader.ReadLine();
                    for (int i = 0; i < 9; i++)
                    {
                        var line = reader.ReadLine();

                        for (int j = 0; j < 9; j++)
                        {
                            var value = Int32.Parse(line[j].ToString());
                            if (value != 0)
                            {
                                puzzle[i, j] = new List<int>() { value };
                            }
                        }
                    }
                    puzzles.Add(puzzle);
                }
            }
            #endregion

            int topLeftSum = 0;
            foreach (var puzzle in puzzles)
            {
                Console.WriteLine("Puzzle " + puzzle.Name);
                puzzle.Solve();
                topLeftSum += puzzle[0, 0].First()*100 + puzzle[0, 1].First()*10 + puzzle[0, 2].First();
            }

            var answer = topLeftSum;
        }
Ejemplo n.º 2
0
        private bool _Solve()
        {
            try
            {

                var oldScore = PScore;
                while (PScore > 81)
                {
                    LookForLoneNumber();

                    LookForGroup(1);
                    LookForGroup(2);
                    LookForGroup(3);

                    LookForSectionCut();

                    if (!ConsistencyCheck())
                    {
                        throw new Exception("Not consistent");
                    }

                    //We need to make a guess
                    if (PScore >= oldScore)
                    {
                        var guesses = new List<Tuple<int, int>>();
                        for (int i = 0; i < 9; i++)
                        {
                            for (int j = 0; j < 9; j++)
                            {
                                if (this[i, j].Count > 1)
                                {
                                    guesses.Add(new Tuple<int, int>(i, j));
                                }
                            }
                        }

                        foreach (var guess in guesses)
                        {
                            foreach (var n in this[guess.Item1, guess.Item2])
                            {
                                var guessPuzzle = new SudokuPuzzle();
                                guessPuzzle.Name = guessPuzzle.Name;
                                for (int i = 0; i < 9; i++)
                                {
                                    for (int j = 0; j < 9; j++)
                                    {
                                        guessPuzzle[i, j] = this[i, j].ToList();
                                    }
                                }
                                guessPuzzle[guess.Item1, guess.Item2] = new List<int>() { n };
                                try
                                {
                                    guessPuzzle.Solve();
                                    for (int i = 0; i < 9; i++)
                                    {
                                        for (int j = 0; j < 9; j++)
                                        {
                                            this[i, j] = guessPuzzle[i, j].ToList();
                                        }
                                    }
                                    break;
                                }
                                catch (Exception e)
                                {
                                    //otherwise try another guess
                                }
                            }
                        }

                    }
                    oldScore = PScore;

                }
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }