/// <summary> /// To check if a puzzle has only one solution or not. /// </summary> /// <param name="this">(<see langword="this"/> parameter) The puzzle to check.</param> /// <param name="solutionIfValid"> /// (<see langword="out"/> parameter) The solution if the puzzle is valid; /// otherwise, <see langword="null"/>. /// </param> /// <returns>A <see cref="bool"/> value indicating that.</returns> public static bool IsValid(this IReadOnlyGrid @this, [NotNullWhen(true)] out IReadOnlyGrid?solutionIfValid) { solutionIfValid = null; if (new BitwiseSolver().CheckValidity(@this.ToString(), out string?solution) || new SukakuBitwiseSolver().CheckValidity(@this.ToString("~"), out solution)) { solutionIfValid = Grid.Parse(solution); return(true); } else { return(false); } }
/// <inheritdoc/> public override AnalysisResult Solve(IReadOnlyGrid grid) { string str = grid.ToString("."); var sb = new CStyleString(82); var stopwatch = new Stopwatch(); try { stopwatch.Start(); int count = Solve32(str, sb, 2); stopwatch.Stop(); return(count switch { 0 => throw new NoSolutionException(grid), 1 => new AnalysisResult( puzzle: grid, solverName: SolverName, hasSolved: true, solution: Grid.Parse(sb.ToString()), elapsedTime: stopwatch.Elapsed, solvingList: null, additional: null, stepGrids: null), _ => throw new MultipleSolutionsException(grid) }); }
/// <inheritdoc/> public override AnalysisResult Solve(IReadOnlyGrid grid) { var stopwatch = new Stopwatch(); stopwatch.Start(); var results = SolveStrings(grid.ToString("0")); stopwatch.Stop(); return(results.Count switch { 0 => throw new NoSolutionException(grid), 1 => new AnalysisResult( puzzle: grid, solverName: SolverName, hasSolved: true, solution: Grid.Parse(results[0]), elapsedTime: stopwatch.Elapsed, solvingList: null, additional: null, stepGrids: null), _ => throw new MultipleSolutionsException(grid) });