From 7e6c14a2f6e9e9cbac3431f3085c42bbe474d13d Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 29 Aug 2024 15:51:33 +0200 Subject: Refactor tools to remove duplicate code --- tools/003_solve_small/solve_small.c | 55 ++++--------------------------------- 1 file changed, 6 insertions(+), 49 deletions(-) (limited to 'tools/003_solve_small/solve_small.c') diff --git a/tools/003_solve_small/solve_small.c b/tools/003_solve_small/solve_small.c index 406e255..dee2b55 100644 --- a/tools/003_solve_small/solve_small.c +++ b/tools/003_solve_small/solve_small.c @@ -1,12 +1,12 @@ #include -#include -#include "../timerun.h" -#include "../../src/nissy.h" -#define OPTIONS "0;4;20" +#include "../tool.h" +const char *solver = "h48"; +const char *options = "0;4;20"; const char *filename = "tables/h48h0k4"; char *buf; + char *scrambles[] = { "R D' R2 D R U2 R' D' R U2 R D R'", /* 12 optimal */ "RLUD RLUD RLUD", /* 12 optimal */ @@ -28,7 +28,7 @@ void run(void) { continue; } n = nissy_solve( - cube, "h48", OPTIONS, "", 0, 20, 1, -1, buf, sol); + cube, "h48", options, "", 0, 20, 1, -1, buf, sol); if (n == 0) fprintf(stderr, "No solution found, " "continuing with next scramble\n"); @@ -36,56 +36,13 @@ void run(void) { printf("\n"); } -int getdata(int64_t size) { - int64_t s; - FILE *f; - - buf = malloc(size); - - if ((f = fopen(filename, "rb")) == NULL) { - fprintf(stderr, "Table file not found, generating them." - " This can take a while.\n"); - s = nissy_gendata("h48", OPTIONS, buf); - if (s != size) { - fprintf(stderr, "Error generating table"); - if (s != -1) - fprintf(stderr, " (got %" PRId64 " bytes)", s); - fprintf(stderr, "\n"); - return 1; - } - if ((f = fopen(filename, "wb")) == NULL) { - fprintf(stderr, "Could not write tables to file %s" - ", will be regenerated next time.\n", filename); - } else { - fwrite(buf, size, 1, f); - fclose(f); - } - } else { - fprintf(stderr, "Reading tables from file %s\n", filename); - fread(buf, size, 1, f); - fclose(f); - } - - return 0; -} - int main(void) { - int64_t size; srand(time(NULL)); - nissy_setlogger(log_stderr); - size = nissy_datasize("h48", OPTIONS); - if (size == -1) { - printf("h48 stats: error in datasize\n"); - return 1; - } - if (getdata(size) != 0) { - printf("Error getting table, stopping\n"); - free(buf); + if (getdata(solver, options, &buf, filename) != 0) return 1; - } timerun(run, "small solver benchmark"); -- cgit v1.3