forked from dlbeer/cdok
-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.c
253 lines (206 loc) · 5.7 KB
/
parser.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
/* cdok -- Calcudoku solver/generator
* Copyright (C) 2012 Daniel Beer <dlbeer@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "cdok.h"
#include "parser.h"
/* Check that all groups are single contiguous regions. On a copy of the
* group map, flood-fill from a single member of each group. If the groups
* are all contiguous, this should completely erase the map.
*/
static int validate_group_map(const struct cdok_puzzle *puz)
{
uint8_t map[CDOK_CELLS];
int i;
memcpy(map, puz->group_map, sizeof(map));
for (i = 0; i < CDOK_GROUPS; i++) {
const struct cdok_group *g = &puz->groups[i];
cdok_pos_t start = g->members[0];
if (g->size)
cdok_flood_fill(map, i,
CDOK_POS_X(start), CDOK_POS_Y(start));
}
for (i = 0; i < CDOK_CELLS; i++)
if (map[i] != CDOK_GROUP_NONE) {
fprintf(stderr, "Group %c is not contiguous at "
"cell (%d, %d)\n",
cdok_group_to_char(map[i]),
CDOK_POS_X(i), CDOK_POS_Y(i));
return -1;
}
return 0;
}
/* Check that each group has a target and type and satisfies basic
* constraints.
*/
static int validate_groups(const struct cdok_puzzle *puz)
{
int i;
for (i = 0; i < CDOK_GROUPS; i++) {
const struct cdok_group *g = &puz->groups[i];
uint8_t ch = cdok_group_to_char(i);
if (!g->size)
continue;
if (!g->type) {
fprintf(stderr, "Group %c has no type\n", ch);
return -1;
}
if (g->target < 0) {
fprintf(stderr, "Group %c has no target\n", ch);
return -1;
}
if (g->size < 2) {
fprintf(stderr, "Group %c has only a "
"single member\n", ch);
return -1;
}
if ((g->type == CDOK_RATIO || g->type == CDOK_PRODUCT) &&
!g->target) {
fprintf(stderr, "Group %c is of type %c but it has "
"a target of 0\n", ch, g->type);
return -1;
}
}
return 0;
}
/* Initialize a parser and clear the puzzle state. */
void cdok_parser_init(struct cdok_parser *p, struct cdok_puzzle *puz)
{
memset(p, 0, sizeof(*p));
p->group_name = CDOK_GROUP_NONE;
p->value = -1;
cdok_init_puzzle(puz, 0);
}
/* This is an internal parser function called whenever whitespace is
* encountered to end a cell.
*/
static int parser_end_cell(struct cdok_parser *p, struct cdok_puzzle *puz)
{
if (p->value < 0 && p->group_name == CDOK_GROUP_NONE)
return 0;
if (p->x >= CDOK_SIZE || p->y >= CDOK_SIZE) {
fprintf(stderr, "Maximum cell coordinates exceeded: (%d, %d)\n",
p->x, p->y);
return -1;
}
if (p->group_name != CDOK_GROUP_NONE) {
struct cdok_group *g = &puz->groups[p->group_name];
if (g->size >= CDOK_GROUP_SIZE) {
fprintf(stderr, "Maximum group size exceeded: (%d, %d) "
"(group %c)\n", p->x, p->y, p->group_name);
return -1;
}
g->members[g->size++] = CDOK_POS(p->x, p->y);
if (p->value >= 0) {
if (g->target >= 0 && p->value != g->target) {
fprintf(stderr, "Group %c has two conflicting "
"targets: %d vs %d\n",
p->group_name, p->value, g->target);
return -1;
}
g->target = p->value;
}
if (p->group_type) {
if (g->type && g->type != p->group_type) {
fprintf(stderr, "Group %c has two conflicting "
"types: %c vs %c\n",
p->group_name, g->type, p->group_type);
return -1;
}
g->type = p->group_type;
}
} else {
puz->values[CDOK_POS(p->x, p->y)] = p->value;
}
p->group_type = 0;
p->group_name = CDOK_GROUP_NONE;
p->value = -1;
p->x++;
return 0;
}
int cdok_parser_push(struct cdok_parser *p, struct cdok_puzzle *puz,
const char *text, int len)
{
if (p->eof)
return 0;
while (len) {
if (*text == '\n') {
if (parser_end_cell(p, puz) < 0)
return -1;
if (!p->x) {
p->eof = 1;
return 0;
}
if (!p->y) {
puz->size = p->x;
} else if (p->x != puz->size) {
fprintf(stderr, "Jagged row %d (expected "
"%d cells)\n", p->y, puz->size);
return -1;
}
p->y++;
p->x = 0;
} else if (isspace(*text)) {
if (parser_end_cell(p, puz) < 0)
return -1;
} else if (isdigit(*text)) {
if (p->value < 0)
p->value = 0;
p->value = p->value * 10 + *text - '0';
} else if (*text == '+' || *text == '-' ||
*text == '*' || *text == '/') {
p->group_type = *text;
} else {
uint8_t g = cdok_char_to_group(*text);
if (g != CDOK_GROUP_NONE)
p->group_name = g;
}
text++;
len--;
}
return 0;
}
/* Rebuild the map of cells to groups. */
static void build_group_map(struct cdok_puzzle *puz)
{
int i;
memset(puz->group_map, CDOK_GROUP_NONE, sizeof(puz->group_map));
for (i = 0; i < CDOK_GROUPS; i++) {
const struct cdok_group *g = &puz->groups[i];
unsigned int j;
for (j = 0; j < g->size; j++)
puz->group_map[g->members[j]] = i;
}
}
int cdok_parser_end(struct cdok_parser *p, struct cdok_puzzle *puz)
{
if (parser_end_cell(p, puz) < 0)
return -1;
if (!puz->size) {
fprintf(stderr, "No cells!\n");
return -1;
}
if (p->y < puz->size) {
fprintf(stderr, "Grid is not square (width = %d, "
"height = %d)\n", puz->size, p->y);
return -1;
}
if (validate_groups(puz) < 0)
return -1;
build_group_map(puz);
return validate_group_map(puz);
}