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/unequal.c | 74 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) (limited to 'apps/plugins/puzzles/src/unequal.c') diff --git a/apps/plugins/puzzles/src/unequal.c b/apps/plugins/puzzles/src/unequal.c index 951e9ac80f..556cf01c45 100644 --- a/apps/plugins/puzzles/src/unequal.c +++ b/apps/plugins/puzzles/src/unequal.c @@ -816,6 +816,74 @@ static int solver_set(struct latin_solver *solver, void *vctx) #define SOLVER(upper,title,func,lower) func, static usersolver_t const unequal_solvers[] = { DIFFLIST(SOLVER) }; +static bool unequal_valid(struct latin_solver *solver, void *vctx) +{ + struct solver_ctx *ctx = (struct solver_ctx *)vctx; + if (ctx->state->mode == MODE_ADJACENT) { + int o = solver->o; + int x, y, nx, ny, v, nv, i; + + for (x = 0; x+1 < o; x++) { + for (y = 0; y+1 < o; y++) { + v = grid(x, y); + for (i = 0; i < 4; i++) { + bool is_adj, should_be_adj; + + should_be_adj = + (GRID(ctx->state, flags, x, y) & adjthan[i].f); + + nx = x + adjthan[i].dx, ny = y + adjthan[i].dy; + if (nx < 0 || ny < 0 || nx >= o || ny >= o) + continue; + + nv = grid(nx, ny); + is_adj = (labs(v - nv) == 1); + + if (is_adj && !should_be_adj) { +#ifdef STANDALONE_SOLVER + if (solver_show_working) + printf("%*s(%d,%d):%d and (%d,%d):%d have " + "adjacent values, but should not\n", + solver_recurse_depth*4, "", + x+1, y+1, v, nx+1, ny+1, nv); +#endif + return false; + } + + if (!is_adj && should_be_adj) { +#ifdef STANDALONE_SOLVER + if (solver_show_working) + printf("%*s(%d,%d):%d and (%d,%d):%d do not have " + "adjacent values, but should\n", + solver_recurse_depth*4, "", + x+1, y+1, v, nx+1, ny+1, nv); +#endif + return false; + } + } + } + } + } else { + int i; + for (i = 0; i < ctx->nlinks; i++) { + struct solver_link *link = &ctx->links[i]; + int gv = grid(link->gx, link->gy); + int lv = grid(link->lx, link->ly); + if (gv <= lv) { +#ifdef STANDALONE_SOLVER + if (solver_show_working) + printf("%*s(%d,%d):%d should be greater than (%d,%d):%d, " + "but is not\n", solver_recurse_depth*4, "", + link->gx+1, link->gy+1, gv, + link->lx+1, link->ly+1, lv); +#endif + return false; + } + } + } + return true; +} + static int solver_state(game_state *state, int maxdiff) { struct solver_ctx *ctx = new_ctx(state); @@ -827,7 +895,8 @@ static int solver_state(game_state *state, int maxdiff) diff = latin_solver_main(&solver, maxdiff, DIFF_LATIN, DIFF_SET, DIFF_EXTREME, DIFF_EXTREME, DIFF_RECURSIVE, - unequal_solvers, ctx, clone_ctx, free_ctx); + unequal_solvers, unequal_valid, ctx, + clone_ctx, free_ctx); memcpy(state->hints, solver.cube, state->order*state->order*state->order); @@ -2155,7 +2224,8 @@ static int solve(game_params *p, char *desc, int debug) diff = latin_solver_main(&solver, DIFF_RECURSIVE, DIFF_LATIN, DIFF_SET, DIFF_EXTREME, DIFF_EXTREME, DIFF_RECURSIVE, - unequal_solvers, ctx, clone_ctx, free_ctx); + unequal_solvers, unequal_valid, ctx, + clone_ctx, free_ctx); free_ctx(ctx); -- cgit v1.2.3