-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnakesolver.c
More file actions
executable file
·173 lines (140 loc) · 4.57 KB
/
snakesolver.c
File metadata and controls
executable file
·173 lines (140 loc) · 4.57 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Compile with:
// gcc -std=c99 snakesolver.c && ./a.out
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define DIM 4
int snake_short[]= { 5, 1, 1, 2, 1, 1, 1, 6, 1, 1, 2, 2, 4, 2, 3, 4, 3, 5, 5, 2, 2, 4, 2, 2, 2, 0 };
// int snake_short="7 1 1 2 1 1 1 6 1 1 2 2 4 2 3 4 3 5 5 2 2 4 2 2";
// int snake_short="2 4 2 2 4 4 4 2 2 4 1 1 4 2 2 4 4 4 2 2 4 3 1";
const int exit_after_first_solution= 1;
#define DIMM1 (DIM - 1)
#define DIMP1 (DIM + 1)
#define DIMP2 (DIM + 2)
#define DIMP2SQ (DIMP2 * DIMP2)
#define DIMP2CB (DIMP2 * DIMP2 * DIMP2)
#define SLEN (DIM * DIM * DIM)
#define xyz2n(x, y, z) (z * DIMP2SQ + y * DIMP2 + x )
#define n2x(n) (n % DIMP2)
#define n2y(n) ((n / DIMP2) % DIMP2)
#define n2z(n) ((n / DIMP2SQ) % DIMP2)
int result[DIMP2CB];
int solved[DIMP2CB];
int snake[SLEN];
int offset=0;
int tries= 0;
char *get_cube() {
static char buf[10000];
char *bufp= buf;
for ( int z= 1; z <= DIM; z++ ) {
bufp += sprintf(bufp, "z=%d\n"
"\t+----+----+----+----+"
"\t+---+---+---+---+\n", z);
for ( int y= 1; y <= DIM; y++ ) {
bufp += sprintf(bufp, "\t|");
for ( int x= 1; x <= DIM; x++ ) {
int n= xyz2n(x, y, z);
bufp += sprintf(bufp, " %2d |", result[n]);
}
bufp += sprintf(bufp, "\t|");
for ( int x= 1; x <= DIM; x++ ) {
int n= xyz2n(x, y, z);
int snake_i= result[n];
if ( snake_i < 0 ) {
bufp += sprintf(bufp, " ? |");
}
else if ( snake[snake_i] == 1 ) {
bufp += sprintf(bufp, " X |");
}
else {
bufp += sprintf(bufp, " X |");
}
}
bufp += sprintf(bufp, ""
"\n\t+----+----+----+----+"
"\t+---+---+---+---+\n");
}
bufp += sprintf(bufp, "\n");
}
return buf;
}
void print_cube() {
puts(get_cube());
}
void has_solved() {
printf("\n\nSOLVED with %d tries\n", tries);
print_cube();
if ( exit_after_first_solution ) {
exit(1);
}
}
void find_way( int n, int idx, int dir, int fin ) {
if ( fin ) {
if ( result[n] == 0 ) { // naechstes element ist der anfang der schlange
has_solved();
}
return;
}
// printf("fin=%d idx=%d dir=%d n=%d result=%d solved=%d\n", fin, idx, dir, n, result[n], solved[n]);
if ( result[n] > -2 ) return;
if ( solved[n] > 0 && solved[n] != snake[idx] ) return; // color cares but does not match
// printf("fin=%d idx=%d dir=%d n=%d\n", fin, idx, dir, n);
tries++;
if ( tries % 1000000 == 0 ) {
printf("\rtries: %d (%d,%d,%d) -> idx: %d\n", tries, n2x(n), n2y(n), n2z(n), snake[idx]);
}
result[n]= idx++;
fin= idx >= SLEN;
if ( dir != 1 ) find_way(n + 1, idx, 0, fin);
if ( dir != 0 ) find_way(n - 1, idx, 1, fin);
if ( dir != 3 ) find_way(n + DIMP2, idx, 2, fin);
if ( dir != 2 ) find_way(n - DIMP2, idx, 3, fin);
if ( dir != 5 ) find_way(n + DIMP2SQ, idx, 4, fin);
if ( dir != 4 ) find_way(n - DIMP2SQ, idx, 5, fin);
result[n]= -2;
}
int main () {
int color= 2;
int j= 0;
for ( int i= 0; snake_short[i]; i++ ) {
color= 3 - color;
while ( snake_short[i]-- ) snake[j++]= color;
}
assert( j == SLEN );
assert( (j & 1) == 0 );
for ( int x= 0; x <= DIMP1; x++ ) {
for ( int y= 0; y <= DIMP1; y++ ) {
for ( int z= 0; z <= DIMP1; z++ ) {
int n= xyz2n(x, y, z);
if ( x == 0 || y == 0 || z == 0
|| x == DIMP1 || y == DIMP1 || z == DIMP1
) {
result[n]= -1;
solved[n]= 0; // Unused
}
else if ( x == 1 || y == 1 || z == 1
|| x == DIM || y == DIM || z == DIM
) {
result[n]= -2;
solved[n]= (((x - 1) >> 1) + ((y - 1) >> 1) + ((z - 1) >> 1)) % 2 + 1;
}
else {
result[n]= -2;
solved[n]= 0;
}
}
}
}
while ( offset < SLEN ) {
if ( offset ) {
int last= snake[SLEN - 1];
for ( int i= SLEN - 1; i > 0; i-- ) snake[i]= snake[i - 1];
snake[0]= last;
printf("\nstarting over (offset %d)\n", offset);
}
offset++;
find_way(xyz2n(1, 1, 1), 0, -1, 0);
}
// print_cube();
return 0;
}