aboutsummaryrefslogtreecommitdiff
path: root/src/nissy.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-08-18 14:26:45 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-08-18 14:26:45 +0200
commit18c9a8b8905304cf5f8fc15825769046a3144866 (patch)
treea7807bb32b0a5d9ded7d3cedccc598f64a9b00fe /src/nissy.c
parentf25a10e19eca294c4e6a99e4f80ce5cfd11a0e5f (diff)
downloadnissy-core-18c9a8b8905304cf5f8fc15825769046a3144866.tar.gz
nissy-core-18c9a8b8905304cf5f8fc15825769046a3144866.zip
Reorganized folder structure
Diffstat (limited to 'src/nissy.c')
-rw-r--r--src/nissy.c265
1 files changed, 265 insertions, 0 deletions
diff --git a/src/nissy.c b/src/nissy.c
new file mode 100644
index 0000000..9df775c
--- /dev/null
+++ b/src/nissy.c
@@ -0,0 +1,265 @@
1#include <inttypes.h>
2#include <stdarg.h>
3#include <stdbool.h>
4#include <string.h>
5
6#include "utils/utils.h"
7#include "arch/arch.h"
8#include "core/core.h"
9#include "solvers/solvers.h"
10
11#include "nissy.h"
12
13_static int64_t write_result(cube_t, char [static 22]);
14
15/* TODO: add option to get DR, maybe C-only, E-only, eo... */
16#define GETCUBE_OPTIONS(S, F) { .option = S, .fix = F }
17struct {
18 char *option;
19 void (*fix)(int64_t *, int64_t *, int64_t *, int64_t *);
20} getcube_options[] = {
21 GETCUBE_OPTIONS("fix", getcube_fix),
22 GETCUBE_OPTIONS(NULL, NULL)
23};
24
25_static int64_t
26write_result(cube_t cube, char result[static 22])
27{
28 if (!isconsistent(cube)) {
29 writecube("B32", zero, result);
30 return 2;
31 }
32
33 writecube("B32", cube, result);
34
35 return issolvable(cube) ? 0 : 1;
36}
37
38int64_t
39nissy_compose(
40 const char cube[static 22],
41 const char permutation[static 22],
42 char result[static 22]
43)
44{
45 cube_t c, p, res;
46
47 c = readcube("B32", cube);
48 p = readcube("B32", permutation);
49 res = compose(c, p);
50
51 return write_result(res, result);
52}
53
54int64_t
55nissy_inverse(
56 const char cube[static 22],
57 char result[static 22]
58)
59{
60 cube_t c, res;
61
62 c = readcube("B32", cube);
63 res = inverse(c);
64
65 return write_result(res, result);
66}
67
68int64_t
69nissy_applymoves(
70 const char cube[static 22],
71 const char *moves,
72 char result[static 22]
73)
74{
75 cube_t c, res;
76
77 c = readcube("B32", cube);
78 res = applymoves(c, moves);
79
80 return write_result(res, result);
81}
82
83int64_t
84nissy_applytrans(
85 const char cube[static 22],
86 const char *transformation,
87 char result[static 22]
88)
89{
90 cube_t c, res;
91
92 c = readcube("B32", cube);
93 res = applytrans(c, transformation);
94
95 return write_result(res, result);
96}
97
98int64_t
99nissy_frommoves(
100 const char *moves,
101 char result[static 22]
102)
103{
104 cube_t res;
105
106 res = applymoves(solved, moves);
107
108 return write_result(res, result);
109}
110
111int64_t
112nissy_convert(
113 const char *format_in,
114 const char *format_out,
115 const char *cube_string,
116 char *result
117)
118{
119 cube_t c;
120
121 c = readcube(format_in, cube_string);
122 writecube(format_out, c, result);
123
124 return isconsistent(c) ? 0 : 2;
125}
126
127int64_t
128nissy_getcube(
129 int64_t ep,
130 int64_t eo,
131 int64_t cp,
132 int64_t co,
133 const char *options,
134 char result[static 22]
135)
136{
137 int i;
138 cube_t c;
139
140 for (i = 0; getcube_options[i].option != NULL; i++)
141 if (!strcmp(options, getcube_options[i].option))
142 getcube_options[i].fix(&ep, &eo, &cp, &co);
143
144 c = getcube(ep, eo, cp, co);
145
146 return write_result(c, result);
147}
148
149int64_t
150nissy_datasize(
151 const char *solver,
152 const char *options
153)
154{
155 /* gendata() handles a NULL *data as a "dryrun" request */
156 return nissy_gendata(solver, options, NULL);
157}
158
159int64_t
160nissy_gendata(
161 const char *solver,
162 const char *options,
163 void *data
164)
165{
166 int64_t ret;
167 uint8_t maxdepth, h, i, j;
168
169 if (!strcmp(solver, "h48")) {
170 /* options are in the form "h;maxdepth" */
171 for (i = 0; options[i] != ';'; i++) ;
172 for (j = i; options[j]; j++) ;
173 h = atoi(options);
174 if (h != 0) {
175 LOG("Temporarily only h=0 is supported\n");
176 ret = -1;
177 } else {
178 maxdepth = atoi(&options[i+1]);
179 ret = gendata_h48h0k4(data, maxdepth);
180 }
181 } else if (!strcmp(solver, "h48stats")) {
182 ret = gendata_h48h0k4(data, 20);
183 } else {
184 LOG("gendata: implemented only for h48 solver\n");
185 ret = -1;
186 }
187
188 return ret;
189}
190
191int64_t
192nissy_solve(
193 const char cube[static 22],
194 const char *solver,
195 const char *options,
196 const char *nisstype,
197 int8_t minmoves,
198 int8_t maxmoves,
199 int64_t maxsolutions,
200 int8_t optimal,
201 const void *data,
202 char *solutions
203)
204{
205 cube_t c;
206 int64_t ret;
207 int h;
208
209 c = readcube_B32(cube);
210
211 if (!issolvable(c)) {
212 LOG("solve: cube is not solvable\n");
213 return -1;
214 }
215
216 if (minmoves < 0) {
217 LOG("solve: 'minmoves' is negative, setting it to 0\n");
218 minmoves = 0;
219 }
220
221 if (maxmoves < 0) {
222 LOG("solve: 'maxmoves' is negative, setting it to 20\n");
223 maxmoves = 20;
224 }
225
226 if (maxsolutions < 0) {
227 LOG("solve: 'maxsols' is negative, stopping\n");
228 return -1;
229 }
230
231 if (maxsolutions == 0) {
232 LOG("solve: 'maxsols' is 0, returning no solution\n");
233 return 0;
234 }
235
236 if (solutions == NULL) {
237 LOG("solve: return parameter 'solutions' is NULL, stopping\n");
238 return -1;
239 }
240
241 /* TODO define and use solve_options_t */
242 if (!strcmp(solver, "h48")) {
243 h = atoi(options); /* TODO: better parsing */
244 ret = solve_h48(
245 c, minmoves, maxmoves, maxsolutions,
246 (uint8_t)h, data, solutions);
247 ret = -1;
248 } else if (!strcmp(solver, "h48stats")) {
249 ret = solve_h48stats(c, maxmoves, data, solutions);
250 } else if (!strcmp(solver, "simple")) {
251 ret = solve_simple(
252 c, minmoves, maxmoves, maxsolutions, optimal, solutions);
253 } else {
254 LOG("solve: unknown solver '%s'\n", solver);
255 ret = -1;
256 }
257
258 return ret;
259}
260
261void
262nissy_setlogger(void (*log)(const char *, ...))
263{
264 nissy_log = log;
265}

Generated with cgit - Back to sebastiano.tronto.net