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_file.c (1617B)


      1 #include "../tool.h"
      2 
      3 #define SOL_BUFFER_LEN   1000
      4 #define MAX_SCR          10000
      5 #define MAX_SCR_LEN      250
      6 
      7 char *solver;
      8 int64_t size = 0, N = 0;
      9 char *buf;
     10 char scrambles[MAX_SCR][MAX_SCR_LEN];
     11 
     12 void run(void) {
     13 	int64_t i, nsols;
     14 	long long stats[NISSY_SIZE_SOLVE_STATS];
     15 	char sol[SOL_BUFFER_LEN], cube[22];
     16 
     17 	printf("Solved the following scrambles:\n\n");
     18 	for (i = 0; i < N; i++) {
     19 		printf("%" PRId64 ". %s\n", i+1, scrambles[i]);
     20 		printf("Solving scramble %s\n", scrambles[i]);
     21 		if (nissy_applymoves(NISSY_SOLVED_CUBE, scrambles[i], cube)
     22 		     == -1) {
     23 			printf("Invalid scramble\n");
     24 			continue;
     25 		}
     26 		nsols = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL,
     27 		    0, 20, 1, -1, 0, size, buf, SOL_BUFFER_LEN, sol, stats);
     28 		if (nsols == 0)
     29 			printf("No solution found\n");
     30 		else
     31 			printf("Solutions:\n%s\n", sol);
     32 	}
     33 }
     34 
     35 int main(int argc, char **argv) {
     36 	char filename[255], *scrfilename;
     37 	FILE *scrfile;
     38 
     39 	if (argc < 3) {
     40 		printf("Error: not enough arguments. "
     41 		    "A solver and a scramble file must be given.\n");
     42 		return 1;
     43 	}
     44 
     45 	solver = argv[1];
     46 	scrfilename = argv[2];
     47 
     48 	srand(time(NULL));
     49 	nissy_setlogger(log_stderr);
     50 
     51 	sprintf(filename, "tables/%s", solver);
     52 	if (getdata(solver, &buf, filename) != 0)
     53 		return 1;
     54 
     55 	size = nissy_datasize(solver);
     56 
     57 	if ((scrfile = fopen(scrfilename, "r")) == NULL) {
     58 		printf("Error: could not read given file '%s'.\n",
     59 		    scrfilename);
     60 		return 1;
     61 	}
     62 
     63 	printf("Reading scrambles from file '%s'.\n", scrfilename);
     64 	for (N = 0; fgets(scrambles[N], MAX_SCR_LEN, scrfile) != NULL; N++) ;
     65 	fclose(scrfile);
     66 
     67 	timerun(run);
     68 
     69 	free(buf);
     70 	return 0;
     71 }