h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

solve_test.c (2649B)


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