Commits

Anonymous committed 2774d6d

add one_sol, max_p1d, interval, optimal

  • Participants
  • Parent commits 509cb37

Comments (0)

Files changed (1)

  */
 int sol[38];
 int num, len, p1_len = 0, p2_len;
+char sol_str[128];
 
+struct itimerval iv;
+
+int optimal = 0;
+int max_p1d = 0;
+int one_sol = 0;
+int interval= 0;
 int gen_tab = 0;
 int all_sol = 0;
 int max_len = 0;
 		memcpy(cp_, cp, sizeof(cp));
 		memcpy(ep_, ep, sizeof(ep));
 	}
+	int f = 0;
+	f |= memcmp(co_, co, sizeof(co));
+	f |= memcmp(eo_, eo, sizeof(eo));
+	f |= memcmp(cp_, cp, sizeof(cp));
+	f |= memcmp(ep_, ep, sizeof(ep));
+	if (f) { fprintf(stderr, "CUBES NOT EQUAL\n"); exit(EXIT_FAILURE); }
 }
 
-void print_solution(void)
+char *sprint_solution(char *str)
+{
+	int i;
+	char *p = str, *faces = "UDFBRL", *num = " 2'";
+
+	for (i = 20 - p1_len; i < 20; i++) {
+		*p++ = faces[sol[i] / 3];
+		*p++ = num  [sol[i] % 3];
+		*p++ = ' ';
+	}
+	for (i = 38 - p2_len; i < 38; i++) {
+		*p++ = faces[sol[i] / 3];
+		*p++ = num  [sol[i] % 3];
+		*p++ = ' ';
+	}
+	*p = '\0';
+
+	return str;
+}
+
+void print_solution(int old)
 {
 	int i;
 
-	for (i = 20 - p1_len; i < 20; i++)
-		print_move(sol[i]);
-	for (i = 38 - p2_len; i < 38; i++)
-		print_move(sol[i]);
-
+	if (!old) sprint_solution(sol_str);
+	fputs(sol_str, stdout);
 	if (verbose) printf(" (%d)(%d,%d)", len, p1_len, p2_len);
 	printf("\n");
 	fflush(stdout);
 
 int begin_phase2(int last)
 {
-	int i, success;
+	int i, solved;
 
 	copy_cube(1, co_, eo_, cp_, ep_);
 	for (i = 20 - p1_len; i < 20; i++)
 		do_move(sol[i]);
 
 	for (p2_len = 0; (!all_sol && p2_len + p1_len < len) || (all_sol && p2_len + p1_len <= len); p2_len++)
-		if ((success = solve(PHASE2, get_ep_coord(), get_cp_coord(), get_ud2_coord(), p2_len, last)))
+		if ((solved = solve(PHASE2, get_ep_coord(), get_cp_coord(), get_ud2_coord(), p2_len, last)) || optimal)
 			break;
-	if ((!all_sol && p2_len + p1_len < len) || (all_sol && success && p2_len + p1_len <= len)) {
+	if (solved <= 0)
+			return solved;
+	if ((!all_sol && p2_len + p1_len < len) || (all_sol && p2_len + p1_len <= len)) {
+		if (interval && (iv.it_value.tv_sec || iv.it_value.tv_usec))
+			if (setitimer(ITIMER_VIRTUAL, &iv, NULL) < 0)
+				printf("setitimer failed\n");
 		len = p2_len + p1_len;
-		print_solution();
+		if (one_sol) sprint_solution(sol_str);
+		else         print_solution(0);
 		num++;
-		return (p2_len == 0 || len <= end_len || (num >= num_sol && num_sol));
+		return (sigtstp || timeout || p2_len == 0 || len <= end_len || (num >= num_sol && num_sol));
 	}
 	return 0;
 }
  */
 int solve(int phase, int e_coord, int c_coord, int ud_coord, int depth, int last)
 {
-	int i, e_tmp, c_tmp, ud_tmp, face;
+	int i, e_tmp, c_tmp, ud_tmp, face, solved;
 
-	if (sigtstp || (timeout && num)) return 1;
+	if (sigtstp || (timeout && num)) return -1;
 	if (e_coord == 0 && c_coord == 0 && ud_coord == 0) { /* found a solution */
 		if (phase == PHASE2) return 1;
 		else if (depth != 0) return 0;
 			if (phase == PHASE1) sol[20 - depth] = face * 3 + i;
 			else                 sol[38 - depth] = face * 3 + ((face > 1) ? 1 : i);
 
-			if (solve(phase, e_tmp, c_tmp, ud_tmp, depth - 1, face)) return 1;
+			if ((solved = solve(phase, e_tmp, c_tmp, ud_tmp, depth - 1, face))) return solved;
 			if (phase == PHASE2 && face > 1) break;
 		}
 	}
 int main(int argc, char **argv)
 {
 	struct sigaction s = { .sa_handler = signal_handler };
-	struct itimerval iv;
-	struct timeval tv;
 
 	char buf[128];
 	int i;
 
-	while ((i = getopt(argc, argv, "1:agm:n:s:t:v")) != -1) {
+	while ((i = getopt(argc, argv, "1ad:gim:n:os:t:v")) != -1) {
 		switch (i) {
+			case '1': one_sol = 1;            break;
 			case 'a': all_sol = 1;            break;
+			case 'd': max_p1d = atoi(optarg); break;
 			case 'g': gen_tab = 1;            break;
+			case 'i': interval= 1;            break;
 			case 'm': max_len = atoi(optarg); break;
 			case 'n': num_sol = atoi(optarg); break;
+			case 'o': optimal = 1;            break;
 			case 's': end_len = atoi(optarg); break;
 			case 't': timeout = atoi(optarg); break;
 			case 'v': verbose = 1;            break;
 			exit(EXIT_FAILURE);
 		}
 
-		timeout  *= 1000; // ms to us
-		tv.tv_sec  = timeout / 1000000;
-		tv.tv_usec = timeout % 1000000;
+		timeout *= 1000; // ms to us
 
-		iv.it_value    = tv;
-		iv.it_interval = (struct timeval){ 0, 0 };
+		iv.it_value.tv_sec  = timeout / 1000000;
+		iv.it_value.tv_usec = timeout % 1000000;
+		iv.it_interval      = (struct timeval){ 0, 0 };
 	}
 	if (sigaction(SIGTSTP, &s, NULL) < 0) {
 		printf("sigaction failed\n");
 		copy_cube(0, co_, eo_, cp_, ep_);
 
 		if (iv.it_value.tv_sec || iv.it_value.tv_usec)
-			setitimer(ITIMER_VIRTUAL, &iv, NULL);
+			if (setitimer(ITIMER_VIRTUAL, &iv, NULL) < 0)
+				printf("setitimer failed\n");
 
-		for (p1_len = 0; p1_len <= len; p1_len++) {
+		for (p1_len = 0; p1_len <= len && (!max_p1d || p1_len <= max_p1d); p1_len++) {
 			if (solve(PHASE1, get_eo_coord(), get_co_coord(), get_ud1_coord(), p1_len, -1))
 				break;
-			if (verbose) printf("finished phase 1 search depth %d\n", p1_len);
+			if (verbose) { printf("finished phase 1 search depth %d\n", p1_len); fflush(stdout); }
 			copy_cube(1, co_, eo_, cp_, ep_);
 		}
 
-		if (verbose) printf("----------\n");
+		if (one_sol) print_solution(1);
+		if (verbose) { printf("----------\n"); fflush(stdout); }
 	}
 
 	return 0;