aboutsummaryrefslogtreecommitdiff
path: root/tools/500_pause_resume_stop/pause_resume_stop.c
blob: 8beee5c9bb26573c099c755055768a43ad036eb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "../tool.h"

/*
This tool starts solving a scramble. It then runs for SOLVE_LEN seconds,
pauses for PAUSE_LEN seconds, runs again for SOLVE_LEN seconds and so on,
until eventually stopping after TOTAL_LEN seconds.
*/

#define SOL_BUFFER_LEN 10000

#define SOLVE_LEN 3000
#define PAUSE_LEN 1500
#define TOTAL_LEN 30000

char *scramble =
    "R' U' F D2 L2 F R2 U2 R2 B D2 L B2 D' B2 L' R' B D2 B U2 L U2 R' U' F";

char *solver;
int64_t size = 0;
unsigned char *buf;

int get_status(void *arg) {
	struct timespec start, now;
	double tdsec, tdnano;
	int tdiff;

	start = *(struct timespec *)arg;
	clock_gettime(CLOCK_MONOTONIC, &now);

	tdsec = now.tv_sec - start.tv_sec;
	tdnano = now.tv_nsec - start.tv_nsec;
	tdiff = 1000 * (tdsec + 1e-9 * tdnano);

	fprintf(stderr, "[tool] Status polled at %dms: ", tdiff);

	if (tdiff > TOTAL_LEN) {
		fprintf(stderr, "Total time elapsed, stopping\n");
		return NISSY_STATUS_STOP;
	}

	if (tdiff % (SOLVE_LEN + PAUSE_LEN) < SOLVE_LEN) {
		fprintf(stderr, "Running\n");
		return NISSY_STATUS_RUN;
	}

	fprintf(stderr, "Pausing\n");
	return NISSY_STATUS_PAUSE;
}

void run(void) {
	int64_t n;
	long long stats[NISSY_SIZE_SOLVE_STATS];
	char sol[SOL_BUFFER_LEN], cube[NISSY_SIZE_CUBE];
	struct timespec starttime;

	nissy_applymoves(NISSY_SOLVED_CUBE, scramble, cube);
	clock_gettime(CLOCK_MONOTONIC, &starttime);
	n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL,
	    0, 20, 100, -1, 0, size, buf, SOL_BUFFER_LEN, sol, stats,
	    get_status, &starttime);
	if (n == 0)
		printf("No solution found\n");
	else
		printf("Solutions:\n%s\n", sol);
}

int main(int argc, char **argv) {
	char filename[7+NISSY_SIZE_DATAID], dataid[NISSY_SIZE_DATAID];

	if (argc < 2) {
		printf("Error: not enough arguments. "
		    "A solver and must be given.\n"); return 1;
	}

	solver = argv[1];
	srand(time(NULL));
	nissy_setlogger(log_stderr, NULL);

	size = nissy_solverinfo(solver, dataid);
	sprintf(filename, "tables/%s", dataid);
	if (getdata(solver, &buf, filename) != 0)
		return 1;

	timerun(run);

	free(buf);
	return 0;
}

Generated with cgit - Back to sebastiano.tronto.net