-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiles.c
224 lines (198 loc) · 5.61 KB
/
tiles.c
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdio.h>
#include <inttypes.h>
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
typedef uint8_t u8;
typedef uint64_t u64;
u64 powi(u64 base, u64 exponent) {
u64 result = 1;
for(u64 i = 0; i < exponent; i++) result *= base;
return result;
}
typedef struct {
u8 data[SIZE][SIZE];
} pattern;
#if STATES == 2
const int permutationCount = 2;
const u8 permutations[permutationCount][STATES] = {
{0, 1},
{1, 0}
};
#elif STATES == 3
const int permutationCount = 6;
const u8 permutations[permutationCount][STATES] = {
{0, 1, 2},
{0, 2, 1},
{1, 0, 2},
{2, 0, 1},
{1, 2, 0},
{2, 1, 0}
};
#else
#error Unsupported state count
#endif
pattern patternFromNumber(u64 patternNumber) {
pattern result;
for(int i = SIZE - 1; i >= 0; i--) {
for(int j = SIZE - 1; j >= 0; j--) {
result.data[i][j] = patternNumber % STATES;
patternNumber /= STATES;
}
}
return result;
}
u64 patternNumber(pattern pattern) {
u64 result = 0;
for(int i = 0; i < SIZE; i++) {
for(int j = 0; j < SIZE; j++) {
result *= STATES;
result += pattern.data[i][j];
}
}
return result;
}
bool patternEqual(pattern x, pattern y) {
for(int i = 0; i < SIZE; i++)
for(int j = 0; j < SIZE; j++)
if(x.data[i][j] != y.data[i][j])
return false;
return true;
}
pattern right(pattern x) {
pattern result;
for(int i = 0; i < SIZE; i++)
for(int j = 0; j < SIZE; j++)
result.data[(i + 1) % SIZE][j] = x.data[i][j];
return result;
}
pattern down(pattern x) {
pattern result;
for(int i = 0; i < SIZE; i++)
for(int j = 0; j < SIZE; j++)
result.data[i][(j + 1) % SIZE] = x.data[i][j];
return result;
}
pattern transpose(pattern x) {
pattern result;
for(int i = 0; i < SIZE; i++)
for(int j = 0; j < SIZE; j++)
result.data[j][i] = x.data[i][j];
return result;
}
pattern reflect(pattern x) {
pattern result;
for(int i = 0; i < SIZE; i++)
for(int j = 0; j < SIZE; j++)
result.data[SIZE - 1 - i][j] = x.data[i][j];
return result;
}
pattern rotate(pattern x) {
return reflect(transpose(x));
}
pattern permute(pattern x, int permutationNumber) {
pattern result;
for(int i = 0; i < SIZE; i++)
for(int j = 0; j < SIZE; j++)
result.data[i][j] = permutations[permutationNumber][x.data[i][j]];
return result;
}
void printPattern(pattern x) {
for(int i = 0; i < SIZE; i++)
for(int j = 0; j < SIZE; j++)
putchar('0' + (STATES - 1 - x.data[i][j]));
putchar('\n');
}
#ifdef brick
#if (SIZE & 1) == 1
#error Brick symmetry only works with even sizes
#endif
u64 initialPatternCount() {
return powi(STATES, SIZE * SIZE / 2);
}
pattern initialPatternNumber(u64 initialPatternNumber) {
pattern result;
for(int i = (SIZE / 2) - 1; i >= 0; i--) {
for(int j = SIZE - 1; j >= 0; j--) {
result.data[i][j] = result.data[SIZE / 2 + i][(j + SIZE / 2) % SIZE] = initialPatternNumber % STATES;
initialPatternNumber /= STATES;
}
}
return result;
}
#else
u64 initialPatternCount() {
return powi(STATES, SIZE * SIZE);
}
pattern initialPatternNumber(u64 initialPatternNumber) {
return patternFromNumber(initialPatternNumber);
}
#endif
const bool doRight = true;
const bool doDown = true;
const bool doRotate = true;
const bool doTranspose = true;
const bool doReflect = true;
const bool doPermutations = true;
const int rightIterations = doRight ? SIZE : 1;
const int downIterations = doDown ? SIZE : 1;
const int rotateIterations = doRotate ? 4 : 1;
const int transposeIterations = doTranspose ? 2 : 1;
const int reflectIterations = doReflect ? 2 : 1;
const int permutationIterations = doPermutations ? permutationCount : 1;
const int iterations = rightIterations * downIterations * rotateIterations * transposeIterations * reflectIterations * permutationIterations;
u8 *alreadySeen;
bool hasAlreadySeen(pattern pattern) {
u64 thisPatternNumber = patternNumber(pattern);
return alreadySeen[thisPatternNumber / 8] & (1 << (thisPatternNumber % 8));
}
void recordSeen(pattern pattern) {
u64 thisPatternNumber = patternNumber(pattern);
alreadySeen[thisPatternNumber / 8] |= (1 << (thisPatternNumber % 8));
}
int main() {
u64 patternCount = powi(STATES, SIZE * SIZE);
alreadySeen = calloc(patternCount / 8 + 1 /* Round up */, sizeof(u8));
int j, k, l, m, n, o;
pattern a, b, c, d, e, f;
pattern equivalenceClass[iterations];
for(u64 i = 0; i < initialPatternCount(); i++) {
int equivalenceClassCounter = 0;
for(j = 0, a = initialPatternNumber(i); j < rotateIterations; j++, a = doRotate ? rotate(a) : a) {
for(k = 0, b = a; k < transposeIterations; k++, b = doTranspose ? transpose(b) : b) {
for(l = 0, c = b; l < reflectIterations; l++, c = doReflect ? reflect(c) : c) {
for(m = 0, d = c; m < rightIterations; m++, d = doRight ? right(d) : d) {
for(n = 0, e = d; n < downIterations; n++, e = doDown ? down(e) : e) {
for(o = 0, f = e; o < permutationIterations; o++, f = doPermutations ? permute(e, o % permutationIterations) : e) {
if(hasAlreadySeen(f)) {
assert(patternEqual(f, initialPatternNumber(i)));
goto nextNumber;
}
equivalenceClass[equivalenceClassCounter++] = f;
}
assert(patternEqual(f, e));
}
assert(patternEqual(e, d));
}
assert(patternEqual(d, c));
}
assert(patternEqual(c, b));
}
assert(patternEqual(b, a));
}
assert(patternEqual(a, initialPatternNumber(i)));
int uniqueCount = 0;
for(j = 0; j < iterations; j++) {
recordSeen(equivalenceClass[j]);
for(k = 0; k < j; k++) {
if(patternEqual(equivalenceClass[j], equivalenceClass[k])) goto isRepeated;
}
uniqueCount++;
isRepeated:;
}
printf("%d ", uniqueCount);
printPattern(equivalenceClass[0]);
nextNumber:;
}
return 0;
}