Commits

Evan Gates  committed 1357b05

add 2_all to generate all possible 2x2x2 cube states, 2_rand for random states

  • Participants
  • Parent commits aa0b85e

Comments (0)

Files changed (2)

File stand_alone/2_all.c

+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+char *names[] = { "UFRUF", "URBUR", "UBLUB", "ULFUL", "DRFDR", "DFLDF", "DLBDL", "DBRDB" };
+/*
+ * index  : 0   1   2   3   4   5   6   n/a
+ * corners: UFR URB UBL ULF DRF DFL DLB DBR
+ */
+uint8_t cp[7], co[7];
+
+void set_co_coord(int coord)
+{
+	int i, s = 0, len = sizeof(co);
+
+	for (i = len - 2; i >= 0; i--) {
+		co[i] = coord % 3;
+		s = (s - co[i] + 3) % 3;
+		coord /= 3;
+	}
+	co[len - 1] = s;
+}
+
+void set_cp_coord(int coord)
+{
+	int i, j, len = sizeof(co);
+
+	cp[len - 1] = 1;
+
+	for (i = len - 2; i >= 0; i--) {
+		cp[i] = (coord % (len - i)) + 1;
+		coord /= len - i;
+		for (j = i + 1; j < len; j++)
+			if (cp[j] >= cp[i])
+				cp[j]++;
+	}
+}
+
+void print_cube(int cp_coord, int co_coord)
+{
+	int i;
+
+	set_cp_coord(cp_coord);
+	set_co_coord(co_coord);
+
+	for (i = 0; i < 7; i++)
+		printf("%.3s ", names[cp[i] - 1] + co[i]);
+	printf("DBR\n");
+}
+
+int main(void)
+{
+	char buf[128];
+	int i, j;
+
+	for (i = 0; i < 5040; i++)
+		for (j = 0; j < 729; j++)
+			print_cube(i, j);
+
+	return 0;
+}
+
+/* vim: set ts=4 sw=4 noexpandtab : */

File stand_alone/2_rand.c

+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/time.h>
+
+char *names[] = { "UFRUF", "URBUR", "UBLUB", "ULFUL", "DRFDR", "DFLDF", "DLBDL", "DBRDB" };
+
+void shuffle(char **perm, int len)
+{
+	char *temp;
+	int i, r;
+
+	for (i = len - 1; i > 0; i--) {
+		r = rand() % (i + 1);
+		temp    = perm[i];
+		perm[i] = perm[r];
+		perm[r] = temp;
+	}
+}
+
+void twist_and_print(char **perm, int len, int mod)
+{
+	int i, r, o;
+
+	for (i = o = 0; i < len - 1; i++) {
+		r = rand() % mod;
+		o = (o + mod - r) % mod;
+		printf("%.*s ", mod, perm[i] + r);
+	}
+	printf("%.*s ", mod, perm[i] + o);
+}
+
+int main(int argc, char **argv)
+{
+	int num;
+    struct timeval tv;
+
+    gettimeofday(&tv, NULL);
+    srand(tv.tv_sec * tv.tv_usec * getpid());
+
+	if (argc == 1) { /* assume one random cube */
+		num = 1;
+	} else if (argc == 2) { /* one argument, number of random cubes */
+		num = atoi(argv[1]);
+	} else {
+		fprintf(stderr, "supply number of random cubes as argument\n");
+		exit(EXIT_FAILURE);
+	}
+
+	while (num-- > 0) {
+		shuffle(names, 7);
+		twist_and_print(names, 7, 3);
+
+		printf("DBR\n");
+		fflush(stdout);
+	}
+
+    return 0;
+}