From 48b0ef1cf22ec37927116ac83ea7c7cfc1f9083e Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Thu, 25 Jun 2020 14:44:33 -0400 Subject: puzzles: resync with upstream This brings the upstream version to 9aa7b7c (with some of my changes as well). Change-Id: I5bf8a3e0b8672d82cb1bf34afc07adbe12a3ac53 --- apps/plugins/puzzles/src/towers.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'apps/plugins/puzzles/src/towers.c') diff --git a/apps/plugins/puzzles/src/towers.c b/apps/plugins/puzzles/src/towers.c index a72cae680d..aee088fb54 100644 --- a/apps/plugins/puzzles/src/towers.c +++ b/apps/plugins/puzzles/src/towers.c @@ -574,6 +574,38 @@ static int solver_hard(struct latin_solver *solver, void *vctx) #define SOLVER(upper,title,func,lower) func, static usersolver_t const towers_solvers[] = { DIFFLIST(SOLVER) }; +static bool towers_valid(struct latin_solver *solver, void *vctx) +{ + struct solver_ctx *ctx = (struct solver_ctx *)vctx; + int w = ctx->w; + int c, i, n, best, clue, start, step; + for (c = 0; c < 4*w; c++) { + clue = ctx->clues[c]; + if (!clue) + continue; + + STARTSTEP(start, step, c, w); + n = best = 0; + for (i = 0; i < w; i++) { + if (solver->grid[start+i*step] > best) { + best = solver->grid[start+i*step]; + n++; + } + } + + if (n != clue) { +#ifdef STANDALONE_SOLVER + if (solver_show_working) + printf("%*sclue %s %d is violated\n", + solver_recurse_depth*4, "", + cluepos[c/w], c%w+1); +#endif + return false; + } + } + return true; +} + static int solver(int w, int *clues, digit *soln, int maxdiff) { int ret; @@ -589,7 +621,7 @@ static int solver(int w, int *clues, digit *soln, int maxdiff) ret = latin_solver(soln, w, maxdiff, DIFF_EASY, DIFF_HARD, DIFF_EXTREME, DIFF_EXTREME, DIFF_UNREASONABLE, - towers_solvers, &ctx, NULL, NULL); + towers_solvers, towers_valid, &ctx, NULL, NULL); sfree(ctx.iscratch); sfree(ctx.dscratch); -- cgit v1.2.3