nissy-core

The "engine" of nissy, including the H48 optimal solver.
git clone https://git.tronto.net/nissy-core
Download | Log | Files | Refs | README | LICENSE

solve_test.c (2713B)


      1 #include "../tool.h"
      2 #include "scrambles.h"
      3 
      4 #define SOL_BUFFER_LEN 100000
      5 
      6 char *solver;
      7 int64_t size = 0;
      8 unsigned char *buf;
      9 
     10 bool check_one(char *actual, char *expected) {
     11 	unsigned i;
     12 	size_t l_actual, l_expected;
     13 
     14 	for (l_actual = 0; actual[l_actual] != '\n'; l_actual++) ;
     15 	l_expected = strlen(expected);
     16 	if (l_actual > l_expected)
     17 		return false;
     18 	for (i = 0; i < l_expected; i++) {
     19 		if (!strncmp(actual, &expected[i], l_actual))
     20 			return true;
     21 		while(expected[i] != '\n') i++;
     22 	}
     23 	return false;
     24 }
     25 
     26 bool check_all(char *actual, char *expected) {
     27 	unsigned i, found, n_expected;
     28 	size_t l_actual;
     29 
     30 	l_actual = strlen(actual);
     31 	if (l_actual != strlen(expected))
     32 		return false;
     33 
     34 	for (i = 0, n_expected = 0; expected[i]; i++)
     35 		n_expected += expected[i] == '\n';
     36 
     37 	for (i = 0, found = 0; i < l_actual; i++)
     38 		if (i == 0 || actual[i-1] == '\n')
     39 			found += check_one(&actual[i], expected);
     40 
     41 	return found == n_expected;
     42 }
     43 
     44 void run(void) {
     45 	int i;
     46 	int64_t n;
     47 	long long stats[NISSY_SIZE_SOLVE_STATS];
     48 	char sol[SOL_BUFFER_LEN], cube[NISSY_SIZE_CUBE];
     49 
     50 	for (i = 0; s[i].scramble[0]; i++) {
     51 		printf("\n%d. %s\n", i, s[i].scramble);
     52 
     53 		/* Single solution */
     54 		if (nissy_applymoves(NISSY_SOLVED_CUBE, s[i].scramble, cube)
     55 		     == -1) {
     56 			printf("Invalid scramble\n");
     57 			continue;
     58 		}
     59 		n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL,
     60 		    0, 20, 1, -1, 0, size, buf, SOL_BUFFER_LEN, sol, stats);
     61 		if (n == 0) {
     62 			printf("Error: no solution\n");
     63 			return;
     64 		}
     65 		if (check_one(sol, s[i].solutions)) {
     66 			printf("Single solution is correct\n");
     67 		} else {
     68 			printf("Error!\n");
     69 			printf("Found solution(s):\n%s", sol);
     70 			printf("Valid solution(s):\n%s", s[i].solutions);
     71 			return;
     72 		}
     73 
     74 		/* Multiple solutions */
     75 		if (nissy_applymoves(NISSY_SOLVED_CUBE, s[i].scramble, cube)
     76 		     == -1) {
     77 			printf("Invalid scramble\n");
     78 			continue;
     79 		}
     80 		n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL,
     81 		    0, 20, 100, 0, 0, size, buf, SOL_BUFFER_LEN, sol, stats);
     82 		if (check_all(sol, s[i].solutions)) {
     83 			printf("All solutions are correct\n");
     84 		} else {
     85 			printf("Error!\n");
     86 			printf("Found solution(s):\n%s", sol);
     87 			printf("Valid solution(s):\n%s", s[i].solutions);
     88 			return;
     89 		}
     90 	}
     91 
     92 	printf("\nAll scrambles solved correctly\n");
     93 }
     94 
     95 int main(int argc, char **argv) {
     96 	char filename[255], dataid[NISSY_SIZE_DATAID];
     97 
     98 	if (argc < 2) {
     99 		printf("Error: not enough arguments. "
    100 		    "A solver must be given.\n");
    101 		return 1;
    102 	}
    103 
    104 	solver = argv[1];
    105 	srand(time(NULL));
    106 	nissy_setlogger(log_stderr, NULL);
    107 
    108 	sprintf(filename, "tables/%s", solver);
    109 	if (getdata(solver, &buf, filename) != 0)
    110 		return 1;
    111 
    112 	size = nissy_solverinfo(solver, dataid);
    113 
    114 	timerun(run);
    115 
    116 	free(buf);
    117 	return 0;
    118 }