-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunequal_plus.c
2625 lines (2255 loc) · 80.4 KB
/
unequal_plus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* unequal.c
*
* Implementation of 'Futoshiki', a puzzle featured in the Guardian.
*
* TTD:
* add multiple-links-on-same-col/row solver nous
* Optimise set solver to use bit operations instead
*
* Guardian puzzles of note:
* #1: 5:0,0L,0L,0,0,0R,0,0L,0D,0L,0R,0,2,0D,0,0,0,0,0,0,0U,0,0,0,0U,
* #2: 5:0,0,0,4L,0L,0,2LU,0L,0U,0,0,0U,0,0,0,0,0D,0,3LRUD,0,0R,3,0L,0,0,
* #3: (reprint of #2)
* #4:
* #5: 5:0,0,0,0,0,0,2,0U,3U,0U,0,0,3,0,0,0,3,0D,4,0,0,0L,0R,0,0,
* #6: 5:0D,0L,0,0R,0,0,0D,0,3,0D,0,0R,0,0R,0D,0U,0L,0,1,2,0,0,0U,0,0L,
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include "puzzles.h"
#include "latin.h" /* contains typedef for digit */
/* ----------------------------------------------------------
* Constant and structure definitions
*/
#define FLASH_TIME 0.4F
#define PREFERRED_TILE_SIZE 32
#define TILE_SIZE (ds->tilesize)
#define GAP_SIZE (TILE_SIZE/2)
#define SQUARE_SIZE (TILE_SIZE + GAP_SIZE)
#define BORDER (TILE_SIZE / 2)
#define COORD(x) ( (x) * SQUARE_SIZE + BORDER )
#define FROMCOORD(x) ( ((x) - BORDER + SQUARE_SIZE) / SQUARE_SIZE - 1 )
#define GRID(p,w,x,y) ((p)->w[((y)*(p)->order)+(x)])
#define GRID3(p,w,x,y,z) ((p)->w[ (((x)*(p)->order+(y))*(p)->order+(z)) ])
#define HINT(p,x,y,n) GRID3(p, hints, x, y, n)
enum {
COL_BACKGROUND,
COL_GRID,
COL_TEXT, COL_GUESS, COL_ERROR, COL_PENCIL,
COL_HIGHLIGHT, COL_LOWLIGHT, COL_SPENT = COL_LOWLIGHT,
COL_BLACK, COL_WHITE,
NCOLOURS
};
typedef enum {
MODE_UNEQUAL, /* Puzzle indicators are 'greater-than'. */
MODE_ADJACENT, /* Puzzle indicators are 'adjacent number'. */
MODE_KROPKI /* Puzzle indicators are 'adjacent' and 'double' */
} Mode;
struct game_params {
int order; /* Size of latin square */
int diff; /* Difficulty */
Mode mode;
};
#define F_IMMUTABLE 1 /* passed in as game description */
#define F_ADJ_UP 2
#define F_ADJ_RIGHT 4
#define F_ADJ_DOWN 8
#define F_ADJ_LEFT 16
#define F_ERROR 32
#define F_ERROR_UP 64
#define F_ERROR_RIGHT 128
#define F_ERROR_DOWN 256
#define F_ERROR_LEFT 512
#define F_SPENT_UP 1024
#define F_SPENT_RIGHT 2048
#define F_SPENT_DOWN 4096
#define F_SPENT_LEFT 8192
#define F_DBL_UP 16384
#define F_DBL_RIGHT 32768
#define F_DBL_DOWN 65536
#define F_DBL_LEFT 131072
#define ADJ_TO_SPENT(x) ((x) << 9)
#define ADJ_TO_DOUBLE(x) ((x) << 13)
#define F_ERROR_MASK (F_ERROR|F_ERROR_UP|F_ERROR_RIGHT|F_ERROR_DOWN|F_ERROR_LEFT)
struct game_state {
int order;
bool completed, cheated;
Mode mode;
digit *nums; /* actual numbers (size order^2) */
unsigned char *hints; /* remaining possiblities (size order^3) */
unsigned long *flags; /* flags (size order^2) */
};
/* ----------------------------------------------------------
* Game parameters and presets
*/
/* Steal the method from map.c for difficulty levels. */
#define DIFFLIST(A) \
A(LATIN,Trivial,NULL,t) \
A(EASY,Easy,solver_easy, e) \
A(SET,Tricky,solver_set, k) \
A(EXTREME,Extreme,NULL,x) \
A(RECURSIVE,Recursive,NULL,r)
#define ENUM(upper,title,func,lower) DIFF_ ## upper,
#define TITLE(upper,title,func,lower) #title,
#define ENCODE(upper,title,func,lower) #lower
#define CONFIG(upper,title,func,lower) ":" #title
enum { DIFFLIST(ENUM) DIFFCOUNT, DIFF_IMPOSSIBLE = diff_impossible, DIFF_AMBIGUOUS = diff_ambiguous, DIFF_UNFINISHED = diff_unfinished };
static char const *const unequal_diffnames[] = { DIFFLIST(TITLE) };
static char const unequal_diffchars[] = DIFFLIST(ENCODE);
#define DIFFCONFIG DIFFLIST(CONFIG)
#define DEFAULT_PRESET 0
static const struct game_params unequal_presets[] = {
{ 5, DIFF_SET, 0 },
{ 5, DIFF_SET, 1 },
{ 5, DIFF_SET, 2 },
{ 5, DIFF_EXTREME, 0 },
{ 5, DIFF_EXTREME, 1 },
{ 5, DIFF_EXTREME, 2 },
{ 6, DIFF_EXTREME, 0 },
{ 6, DIFF_EXTREME, 1 },
{ 6, DIFF_EXTREME, 2 },
};
static bool game_fetch_preset(int i, char **name, game_params **params)
{
game_params *ret;
char buf[80];
if (i < 0 || i >= lenof(unequal_presets))
return false;
ret = snew(game_params);
*ret = unequal_presets[i]; /* structure copy */
sprintf(buf, "%s: %dx%d %s",
ret->mode == MODE_KROPKI ? "Kropki" :
ret->mode == MODE_ADJACENT ? "Adjacent" :
"Unequal",
ret->order, ret->order,
unequal_diffnames[ret->diff]);
*name = dupstr(buf);
*params = ret;
return true;
}
static game_params *default_params(void)
{
game_params *ret;
char *name;
if (!game_fetch_preset(DEFAULT_PRESET, &name, &ret)) return NULL;
sfree(name);
return ret;
}
static void free_params(game_params *params)
{
sfree(params);
}
static game_params *dup_params(const game_params *params)
{
game_params *ret = snew(game_params);
*ret = *params; /* structure copy */
return ret;
}
static void decode_params(game_params *ret, char const *string)
{
char const *p = string;
ret->order = atoi(p);
while (*p && isdigit((unsigned char)*p)) p++;
if (*p == 'k') {
p++;
ret->mode = MODE_KROPKI;
} else if (*p == 'a') {
p++;
ret->mode = MODE_ADJACENT;
} else
ret->mode = MODE_UNEQUAL;
if (*p == 'd') {
int i;
p++;
ret->diff = DIFFCOUNT+1; /* ...which is invalid */
if (*p) {
for (i = 0; i < DIFFCOUNT; i++) {
if (*p == unequal_diffchars[i])
ret->diff = i;
}
p++;
}
}
}
static char *encode_params(const game_params *params, bool full)
{
char ret[80];
sprintf(ret, "%d", params->order);
if (params->mode == MODE_KROPKI)
sprintf(ret + strlen(ret), "k");
else if (params->mode == MODE_ADJACENT)
sprintf(ret + strlen(ret), "a");
if (full)
sprintf(ret + strlen(ret), "d%c", unequal_diffchars[params->diff]);
return dupstr(ret);
}
static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
ret = snewn(4, config_item);
ret[0].name = "Mode";
ret[0].type = C_CHOICES;
ret[0].u.choices.choicenames = ":Unequal:Adjacent:Kropki";
ret[0].u.choices.selected = params->mode;
ret[1].name = "Size (s*s)";
ret[1].type = C_STRING;
sprintf(buf, "%d", params->order);
ret[1].u.string.sval = dupstr(buf);
ret[2].name = "Difficulty";
ret[2].type = C_CHOICES;
ret[2].u.choices.choicenames = DIFFCONFIG;
ret[2].u.choices.selected = params->diff;
ret[3].name = NULL;
ret[3].type = C_END;
return ret;
}
static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
ret->mode = cfg[0].u.choices.selected;
ret->order = atoi(cfg[1].u.string.sval);
ret->diff = cfg[2].u.choices.selected;
return ret;
}
static const char *validate_params(const game_params *params, bool full)
{
if (params->order < 3 || params->order > 32)
return "Order must be between 3 and 32";
if (params->diff >= DIFFCOUNT)
return "Unknown difficulty rating";
if (params->order < 5 && params->mode == MODE_ADJACENT &&
params->diff >= DIFF_SET)
return "Order must be at least 5 for Adjacent puzzles of this difficulty.";
return NULL;
}
/* ----------------------------------------------------------
* Various utility functions
*/
static const struct { unsigned long f, fo, fe; int dx, dy; char c, ac; } adjthan[] = {
{ F_ADJ_UP, F_ADJ_DOWN, F_ERROR_UP, 0, -1, '^', '-' },
{ F_ADJ_RIGHT, F_ADJ_LEFT, F_ERROR_RIGHT, 1, 0, '>', '|' },
{ F_ADJ_DOWN, F_ADJ_UP, F_ERROR_DOWN, 0, 1, 'v', '-' },
{ F_ADJ_LEFT, F_ADJ_RIGHT, F_ERROR_LEFT, -1, 0, '<', '|' }
};
static game_state *blank_game(int order, Mode mode)
{
game_state *state = snew(game_state);
int o2 = order*order, o3 = o2*order;
state->order = order;
state->mode = mode;
state->completed = false;
state->cheated = false;
state->nums = snewn(o2, digit);
state->hints = snewn(o3, unsigned char);
state->flags = snewn(o2, unsigned long);
memset(state->nums, 0, o2 * sizeof(digit));
memset(state->hints, 0, o3);
memset(state->flags, 0, o2 * sizeof(unsigned long));
return state;
}
static game_state *dup_game(const game_state *state)
{
game_state *ret = blank_game(state->order, state->mode);
int o2 = state->order*state->order, o3 = o2*state->order;
memcpy(ret->nums, state->nums, o2 * sizeof(digit));
memcpy(ret->hints, state->hints, o3);
memcpy(ret->flags, state->flags, o2 * sizeof(unsigned long));
return ret;
}
static void free_game(game_state *state)
{
sfree(state->nums);
sfree(state->hints);
sfree(state->flags);
sfree(state);
}
#define CHECKG(x,y) grid[(y)*o+(x)]
/* Returns false if it finds an error, true if ok. */
static bool check_num_adj(digit *grid, game_state *state,
int x, int y, bool me)
{
unsigned long f = GRID(state, flags, x, y);
bool ret = true;
int i, o = state->order;
for (i = 0; i < 4; i++) {
int dx = adjthan[i].dx, dy = adjthan[i].dy, n, dn;
if (x+dx < 0 || x+dx >= o || y+dy < 0 || y+dy >= o)
continue;
n = CHECKG(x, y);
dn = CHECKG(x+dx, y+dy);
assert (n != 0);
if (dn == 0) continue;
if (state->mode == MODE_KROPKI) {
int gd = abs(n-dn);
int id = (2*n == dn || 2*dn == n);
int ot = (n == 1 && dn == 2) || (n == 2 && dn == 1);
if ((f & ADJ_TO_DOUBLE(adjthan[i].f)) && !id) {
debug(("check_adj error (%d,%d):%d should be * (%d,%d):%d",
x, y, n, x+dx, y+dy, dn));
if (me) GRID(state, flags, x, y) |= adjthan[i].fe;
ret = false;
}
else if ((f & adjthan[i].f) && (gd != 1 && !ot)) {
debug(("check_adj error (%d,%d):%d should be O (%d,%d):%d",
x, y, n, x+dx, y+dy, dn));
if (me) GRID(state, flags, x, y) |= adjthan[i].fe;
ret = false;
}
else if (!(f & adjthan[i].f) &&
!(f & ADJ_TO_DOUBLE(adjthan[i].f)) &&
(id || (gd == 1))) {
debug(("check_adj error (%d,%d):%d should be neither * or O (%d,%d):%d",
x, y, n, x+dx, y+dy, dn));
if (me) GRID(state, flags, x, y) |= adjthan[i].fe;
ret = false;
}
} else if (state->mode == MODE_ADJACENT) {
int gd = abs(n-dn);
if ((f & adjthan[i].f) && (gd != 1)) {
debug(("check_adj error (%d,%d):%d should be | (%d,%d):%d",
x, y, n, x+dx, y+dy, dn));
if (me) GRID(state, flags, x, y) |= adjthan[i].fe;
ret = false;
}
if (!(f & adjthan[i].f) && (gd == 1)) {
debug(("check_adj error (%d,%d):%d should not be | (%d,%d):%d",
x, y, n, x+dx, y+dy, dn));
if (me) GRID(state, flags, x, y) |= adjthan[i].fe;
ret = false;
}
} else {
if ((f & adjthan[i].f) && (n <= dn)) {
debug(("check_adj error (%d,%d):%d not > (%d,%d):%d",
x, y, n, x+dx, y+dy, dn));
if (me) GRID(state, flags, x, y) |= adjthan[i].fe;
ret = false;
}
}
}
return ret;
}
/* Returns false if it finds an error, true if ok. */
static bool check_num_error(digit *grid, game_state *state,
int x, int y, bool mark_errors)
{
int o = state->order;
int xx, yy, val = CHECKG(x,y);
bool ret = true;
assert(val != 0);
/* check for dups in same column. */
for (yy = 0; yy < state->order; yy++) {
if (yy == y) continue;
if (CHECKG(x,yy) == val) ret = false;
}
/* check for dups in same row. */
for (xx = 0; xx < state->order; xx++) {
if (xx == x) continue;
if (CHECKG(xx,y) == val) ret = false;
}
if (!ret) {
debug(("check_num_error (%d,%d) duplicate %d", x, y, val));
if (mark_errors) GRID(state, flags, x, y) |= F_ERROR;
}
return ret;
}
/* Returns: -1 for 'wrong'
* 0 for 'incomplete'
* 1 for 'complete and correct'
*/
static int check_complete(digit *grid, game_state *state, bool mark_errors)
{
int x, y, ret = 1, o = state->order;
if (mark_errors)
assert(grid == state->nums);
for (x = 0; x < state->order; x++) {
for (y = 0; y < state->order; y++) {
if (mark_errors)
GRID(state, flags, x, y) &= ~F_ERROR_MASK;
if (grid[y*o+x] == 0) {
ret = 0;
} else {
if (!check_num_error(grid, state, x, y, mark_errors)) ret = -1;
if (!check_num_adj(grid, state, x, y, mark_errors)) ret = -1;
}
}
}
if (ret == 1 && latin_check(grid, o))
ret = -1;
return ret;
}
static char n2c(digit n, int order) {
if (n == 0) return ' ';
if (order < 10) {
if (n < 10) return '0' + n;
} else {
if (n < 11) return '0' + n-1;
n -= 11;
if (n <= 26) return 'A' + n;
}
return '?';
}
/* should be 'digit', but includes -1 for 'not a digit'.
* Includes keypresses (0 especially) for interpret_move. */
static int c2n(int c, int order) {
if (c < 0 || c > 0xff)
return -1;
if (c == ' ' || c == '\b')
return 0;
if (order < 10) {
if (c >= '0' && c <= '9')
return (int)(c - '0');
} else {
if (c >= '0' && c <= '9')
return (int)(c - '0' + 1);
if (c >= 'A' && c <= 'Z')
return (int)(c - 'A' + 11);
if (c >= 'a' && c <= 'z')
return (int)(c - 'a' + 11);
}
return -1;
}
static bool game_can_format_as_text_now(const game_params *params)
{
return true;
}
static char *game_text_format(const game_state *state)
{
int x, y, len, n;
char *ret, *p;
len = (state->order*2) * (state->order*2-1) + 1;
ret = snewn(len, char);
p = ret;
for (y = 0; y < state->order; y++) {
for (x = 0; x < state->order; x++) {
n = GRID(state, nums, x, y);
*p++ = n > 0 ? n2c(n, state->order) : '.';
if (x < (state->order-1)) {
if (state->mode == MODE_KROPKI) {
if (GRID(state, flags, x, y) & F_DBL_RIGHT)
*p++ = '*';
else if (GRID(state, flags, x, y) & F_ADJ_RIGHT)
*p++ = '@';
else
*p++ = ' ';
} else if (state->mode == MODE_ADJACENT) {
*p++ = (GRID(state, flags, x, y) & F_ADJ_RIGHT) ? '|' : ' ';
} else {
if (GRID(state, flags, x, y) & F_ADJ_RIGHT)
*p++ = '>';
else if (GRID(state, flags, x+1, y) & F_ADJ_LEFT)
*p++ = '<';
else
*p++ = ' ';
}
}
}
*p++ = '\n';
if (y < (state->order-1)) {
for (x = 0; x < state->order; x++) {
if (state->mode == MODE_KROPKI) {
if (GRID(state, flags, x, y) & F_DBL_DOWN)
*p++ = '*';
else if (GRID(state, flags, x, y) & F_ADJ_DOWN)
*p++ = '@';
else
*p++ = ' ';
} else if (state->mode == MODE_ADJACENT) {
*p++ = (GRID(state, flags, x, y) & F_ADJ_DOWN) ? '-' : ' ';
} else {
if (GRID(state, flags, x, y) & F_ADJ_DOWN)
*p++ = 'v';
else if (GRID(state, flags, x, y+1) & F_ADJ_UP)
*p++ = '^';
else
*p++ = ' ';
}
if (x < state->order-1)
*p++ = ' ';
}
*p++ = '\n';
}
}
*p++ = '\0';
assert(p - ret == len);
return ret;
}
#ifdef STANDALONE_SOLVER
static void game_debug(game_state *state)
{
char *dbg = game_text_format(state);
printf("%s", dbg);
sfree(dbg);
}
#endif
/* ----------------------------------------------------------
* Solver.
*/
struct solver_link {
int len, gx, gy, lx, ly;
};
struct solver_ctx {
game_state *state;
int nlinks, alinks;
struct solver_link *links;
};
static void solver_add_link(struct solver_ctx *ctx,
int gx, int gy, int lx, int ly, int len)
{
if (ctx->alinks < ctx->nlinks+1) {
ctx->alinks = ctx->alinks*2 + 1;
/*debug(("resizing ctx->links, new size %d", ctx->alinks));*/
ctx->links = sresize(ctx->links, ctx->alinks, struct solver_link);
}
ctx->links[ctx->nlinks].gx = gx;
ctx->links[ctx->nlinks].gy = gy;
ctx->links[ctx->nlinks].lx = lx;
ctx->links[ctx->nlinks].ly = ly;
ctx->links[ctx->nlinks].len = len;
ctx->nlinks++;
/*debug(("Adding new link: len %d (%d,%d) < (%d,%d), nlinks now %d",
len, lx, ly, gx, gy, ctx->nlinks));*/
}
static struct solver_ctx *new_ctx(game_state *state)
{
struct solver_ctx *ctx = snew(struct solver_ctx);
int o = state->order;
int i, x, y;
unsigned long f;
ctx->nlinks = ctx->alinks = 0;
ctx->links = NULL;
ctx->state = state;
if (state->mode != MODE_UNEQUAL)
return ctx; /* adjacent and kropki mode don't use links. */
for (x = 0; x < o; x++) {
for (y = 0; y < o; y++) {
f = GRID(state, flags, x, y);
for (i = 0; i < 4; i++) {
if (f & adjthan[i].f)
solver_add_link(ctx, x, y, x+adjthan[i].dx, y+adjthan[i].dy, 1);
}
}
}
return ctx;
}
static void *clone_ctx(void *vctx)
{
struct solver_ctx *ctx = (struct solver_ctx *)vctx;
return new_ctx(ctx->state);
}
static void free_ctx(void *vctx)
{
struct solver_ctx *ctx = (struct solver_ctx *)vctx;
if (ctx->links) sfree(ctx->links);
sfree(ctx);
}
static void solver_nminmax(struct latin_solver *solver,
int x, int y, int *min_r, int *max_r,
unsigned char **ns_r)
{
int o = solver->o, min = o, max = 0, n;
unsigned char *ns;
assert(x >= 0 && y >= 0 && x < o && y < o);
ns = solver->cube + cubepos(x,y,1);
if (grid(x,y) > 0) {
min = max = grid(x,y)-1;
} else {
for (n = 0; n < o; n++) {
if (ns[n]) {
if (n > max) max = n;
if (n < min) min = n;
}
}
}
if (min_r) *min_r = min;
if (max_r) *max_r = max;
if (ns_r) *ns_r = ns;
}
static int solver_links(struct latin_solver *solver, void *vctx)
{
struct solver_ctx *ctx = (struct solver_ctx *)vctx;
int i, j, lmin, gmax, nchanged = 0;
unsigned char *gns, *lns;
struct solver_link *link;
for (i = 0; i < ctx->nlinks; i++) {
link = &ctx->links[i];
solver_nminmax(solver, link->gx, link->gy, NULL, &gmax, &gns);
solver_nminmax(solver, link->lx, link->ly, &lmin, NULL, &lns);
for (j = 0; j < solver->o; j++) {
/* For the 'greater' end of the link, discount all numbers
* too small to satisfy the inequality. */
if (gns[j]) {
if (j < (lmin+link->len)) {
#ifdef STANDALONE_SOLVER
if (solver_show_working) {
printf("%*slink elimination, (%d,%d) > (%d,%d):\n",
solver_recurse_depth*4, "",
link->gx+1, link->gy+1, link->lx+1, link->ly+1);
printf("%*s ruling out %d at (%d,%d)\n",
solver_recurse_depth*4, "",
j+1, link->gx+1, link->gy+1);
}
#endif
cube(link->gx, link->gy, j+1) = false;
nchanged++;
}
}
/* For the 'lesser' end of the link, discount all numbers
* too large to satisfy inequality. */
if (lns[j]) {
if (j > (gmax-link->len)) {
#ifdef STANDALONE_SOLVER
if (solver_show_working) {
printf("%*slink elimination, (%d,%d) > (%d,%d):\n",
solver_recurse_depth*4, "",
link->gx+1, link->gy+1, link->lx+1, link->ly+1);
printf("%*s ruling out %d at (%d,%d)\n",
solver_recurse_depth*4, "",
j+1, link->lx+1, link->ly+1);
}
#endif
cube(link->lx, link->ly, j+1) = false;
nchanged++;
}
}
}
}
return nchanged;
}
static int solver_adjacent(struct latin_solver *solver, void *vctx)
{
struct solver_ctx *ctx = (struct solver_ctx *)vctx;
int nchanged = 0, x, y, i, n, o = solver->o, nx, ny, gd;
/* Update possible values based on known values and adjacency clues. */
for (x = 0; x < o; x++) {
for (y = 0; y < o; y++) {
if (grid(x, y) == 0) continue;
/* We have a definite number here. Make sure that any
* adjacent possibles reflect the adjacent/non-adjacent clue.
* For Kropki puzzles, additionally check double/non-double clues. */
for (i = 0; i < 4; i++) {
bool isadjacent =
(GRID(ctx->state, flags, x, y) & adjthan[i].f);
bool isdouble =
(GRID(ctx->state, flags, x, y) & ADJ_TO_DOUBLE(adjthan[i].f));
nx = x + adjthan[i].dx, ny = y + adjthan[i].dy;
if (nx < 0 || ny < 0 || nx >= o || ny >= o)
continue;
for (n = 0; n < o; n++) {
/* Continue past numbers the adjacent square _could_ be,
* given the clue we have. */
gd = abs((n+1) - grid(x, y));
if (ctx->state->mode == MODE_KROPKI) {
int ot = ( (n+1) == 1 && grid(x,y) == 2) || ( (n+1) == 2 && grid(x,y) == 1);
int dbl = ( (n+1)*2 == grid(x,y) || (n+1) == 2*grid(x,y) );
if (ot && (isdouble || isadjacent)) continue;
if (isdouble && dbl) continue;
if (!isdouble && isadjacent && (gd == 1)) continue;
if (!isdouble && !isadjacent && !dbl && (gd != 1)) continue;
} else if (ctx->state->mode == MODE_ADJACENT) {
if (isadjacent && (gd == 1)) continue;
if (!isadjacent && (gd != 1)) continue;
}
if (!cube(nx, ny, n+1))
continue; /* already discounted this possibility. */
#ifdef STANDALONE_SOLVER
if (solver_show_working) {
if (ctx->state->mode == MODE_KROPKI)
printf("%*skropki elimination, (%d,%d):%d %s (%d,%d):\n",
solver_recurse_depth*4, "",
x+1, y+1, grid(x, y),
isadjacent ? "O" :
isdouble ? "*": "!O*", nx+1, ny+1);
else
printf("%*sadjacent elimination, (%d,%d):%d %s (%d,%d):\n",
solver_recurse_depth*4, "",
x+1, y+1, grid(x, y), isadjacent ? "|" : "!|", nx+1, ny+1);
printf("%*s ruling out %d at (%d,%d)\n",
solver_recurse_depth*4, "", n+1, nx+1, ny+1);
}
#endif
cube(nx, ny, n+1) = false;
nchanged++;
}
}
}
}
return nchanged;
}
static int solver_adjacent_set(struct latin_solver *solver, void *vctx)
{
struct solver_ctx *ctx = (struct solver_ctx *)vctx;
int x, y, i, n, nn, o = solver->o, nx, ny, gd;
int nchanged = 0, *scratch = snewn(o, int);
/* Update possible values based on other possible values
* of adjacent squares, and adjacency clues.
* and also double clues in kropki mode */
for (x = 0; x < o; x++) {
for (y = 0; y < o; y++) {
for (i = 0; i < 4; i++) {
bool isadjacent =
(GRID(ctx->state, flags, x, y) & adjthan[i].f);
bool isdouble =
(GRID(ctx->state, flags, x, y) & ADJ_TO_DOUBLE(adjthan[i].f));
nx = x + adjthan[i].dx, ny = y + adjthan[i].dy;
if (nx < 0 || ny < 0 || nx >= o || ny >= o)
continue;
/* We know the current possibles for the square (x,y)
* and also the adjacency clue from (x,y) to (nx,ny).
* Construct a maximum set of possibles for (nx,ny)
* in scratch, based on these constraints... */
memset(scratch, 0, o*sizeof(int));
for (n = 0; n < o; n++) {
if (!cube(x, y, n+1)) continue;
for (nn = 0; nn < o; nn++) {
if (n == nn) continue;
gd = abs(nn - n);
if (ctx->state->mode == MODE_KROPKI) {
int ot = (nn == 1 && n == 2) || (nn == 2 && n == 1);
int dbl = ( ((nn+1)*2 == (n+1)) || ((nn+1) == 2*(n+1)) );
if (ot && !(isdouble || isadjacent)) continue;
if (isdouble && !dbl) continue;
if (!isdouble && isadjacent && (gd != 1)) continue;
if (!isdouble && !isadjacent && (dbl || (gd == 1)) ) continue;
} else if (ctx->state->mode == MODE_ADJACENT) {
if (isadjacent && (gd != 1)) continue;
if (!isadjacent && (gd == 1)) continue;
}
scratch[nn] = 1;
}
}
/* ...and remove any possibilities for (nx,ny) that are
* currently set but are not indicated in scratch. */
for (n = 0; n < o; n++) {
if (scratch[n] == 1) continue;
if (!cube(nx, ny, n+1)) continue;
#ifdef STANDALONE_SOLVER
if (solver_show_working) {
if (ctx->state->mode == MODE_KROPKI)
printf("%*skropki elimination, (%d,%d):%d %s (%d,%d):\n",
solver_recurse_depth*4, "",
x+1, y+1, grid(x, y),
isadjacent ? "O" :
isdouble ? "*": "!O*", nx+1, ny+1);
else
printf("%*sadjacent possible elimination, (%d,%d) %s (%d,%d):\n",
solver_recurse_depth*4, "",
x+1, y+1, isadjacent ? "|" : "!|", nx+1, ny+1);
printf("%*s ruling out %d at (%d,%d)\n",
solver_recurse_depth*4, "", n+1, nx+1, ny+1);
}
#endif
cube(nx, ny, n+1) = false;
nchanged++;
}
}
}
}
return nchanged;
}
static int solver_easy(struct latin_solver *solver, void *vctx)
{
struct solver_ctx *ctx = (struct solver_ctx *)vctx;
if (ctx->state->mode >= MODE_ADJACENT)
return solver_adjacent(solver, vctx);
else
return solver_links(solver, vctx);
}
static int solver_set(struct latin_solver *solver, void *vctx)
{
struct solver_ctx *ctx = (struct solver_ctx *)vctx;
if (ctx->state->mode >= MODE_ADJACENT)
return solver_adjacent_set(solver, vctx);
else
return 0;
}
#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 if (ctx->state->mode == MODE_UNEQUAL) {
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);
struct latin_solver solver;
int diff;
latin_solver_alloc(&solver, state->nums, state->order);
diff = latin_solver_main(&solver, maxdiff,
DIFF_LATIN, DIFF_SET, DIFF_EXTREME,
DIFF_EXTREME, DIFF_RECURSIVE,
unequal_solvers, unequal_valid, ctx,
clone_ctx, free_ctx);
memcpy(state->hints, solver.cube, state->order*state->order*state->order);
free_ctx(ctx);
latin_solver_free(&solver);
if (diff == DIFF_IMPOSSIBLE)
return -1;
if (diff == DIFF_UNFINISHED)
return 0;
if (diff == DIFF_AMBIGUOUS)
return 2;