solve_file.c (2065B)
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[7+NISSY_SIZE_DATAID], dataid[NISSY_SIZE_DATAID]; 42 char *scrfilename; 43 FILE *scrfile; 44 45 if (argc < 3) { 46 printf("Error: not enough arguments. " 47 "A solver and a scramble file must be given.\n" 48 "Optionally, a maximum number of solutions per scramble " 49 "and a bound for the number of moves above the shortest " 50 "solution can be provided.\n"); 51 return 1; 52 } 53 54 solver = argv[1]; 55 scrfilename = argv[2]; 56 if (argc >= 5) { 57 nsols = atoi(argv[3]); 58 optimal = atoi(argv[4]); 59 } 60 61 srand(time(NULL)); 62 nissy_setlogger(log_stderr, NULL); 63 64 size = nissy_solverinfo(solver, dataid); 65 sprintf(filename, "tables/%s", dataid); 66 if (getdata(solver, &buf, filename) != 0) 67 return 1; 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 }