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


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