-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_lvl2_source.c
261 lines (221 loc) · 6.71 KB
/
single_lvl2_source.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
Testing Latin Squares Implementation File
Single Square, Testing to Level 2
v1.0z.0
Kat Cannon-MacMartin
Marlboro College
Sources:
SRC1 - Permutations - https://stackoverflow.com/a/9148661
*/
#include <stdlib.h>
#include <stdio.h>
#include "single_lvl2_source.h"
#define MAXORDER 30
#define MAXDIVISORS 10
int modulo(int x,int N){
return (x % N + N) %N;
}
first_line generate_line(int order){
first_line line = malloc(sizeof(struct _first_line));
line->order = order;
for (int i = 0; i < order; i++){
line->values[i] = i;
}
return line;
}
void free_line(first_line line){
free(line);
}
void wipe_lists(int *list_1, int *list_2, int order){
for (int y = 0; y < order - 1; y++){
list_1[y] = 0;
}
for (int z = 0; z < order - 2; z++){
list_2[z] = 0;
}
}
void print_line(first_line subject, FILE *target_file){
for (int i = 0; i < subject->order; i++){
fprintf(target_file, "%i ", subject->values[i]);
}
fprintf(target_file, "\n");
}
void generate_divisors(int target, int *divisor_list){
int array_counter = 0;
for (int t = 0; t < MAXDIVISORS; t++){
divisor_list[t] = 0;
}
for (int i = 1; i < target; i++){
if (target % i == 0){
divisor_list[array_counter] = i;
array_counter++;
}
}
}
int test_lines(first_line target1, first_line target2, int *single_inc_level1, int *single_inc_level2, int* double_inc_level1, int *double_inc_level2, FILE *result_file){
int added = 0;
int counter = 0;
int i = 0;
int l = 0;
for (i = 0; i < (target1->order - 1); i++){
added = modulo((target1->values[i] - target1->values[i+1]), target1->order);
//fprintf(result_file, "Added: %i\n", added);
for (l = 0; l < i; l++){
if (added == single_inc_level1[l]){
counter++;
//fprintf(result_file, "Counter: %i\n", counter);
if (counter > 1){
return 0;
}
}
}
counter = 0;
single_inc_level1[i] = added;
}
for (i = 0; i < (target2->order - 1); i++){
added = modulo((target2->values[i] - target2->values[i+1]), target1->order);
//fprintf(result_file, "Added: %i\n", added);
for (l = 0; l < (target1->order - 1); l++){
if (added == single_inc_level1[l]){
counter++;
//fprintf(result_file, "Counter: %i\n", counter);
if (counter > 1){
return 0;
}
}
}
for (l = 0; l < i; l++){
if (added == single_inc_level2[l]){
counter++;
//fprintf(result_file, "Counter: %i\n", counter);
if (counter > 1){
return 0;
}
}
}
counter = 0;
single_inc_level2[i] = added;
}
for (i = 0; i < (target1->order - 2); i++){
added = modulo((target1->values[i] - target1->values[i+2]), target1->order);
//fprintf(result_file, "Added: %i\n", added);
for (l = 0; l < i; l++){
if (added == double_inc_level1[l]){
counter++;
//fprintf(result_file, "Counter: %i\n", counter);
if (counter > 1){
return 0;
}
}
}
counter = 0;
double_inc_level1[i] = added;
}
for (i = 0; i < (target2->order - 2); i++){
added = modulo((target2->values[i] - target2->values[i+2]), target1->order);
//fprintf(result_file, "Added: %i\n", added);
for (l = 0; l < (target1->order - 2); l++){
if (added == double_inc_level1[l]){
counter++;
//fprintf(result_file, "Counter: %i\n", counter);
if (counter > 1){
return 0;
}
}
}
for (l = 0; l < i; l++){
if (added == double_inc_level2[l]){
counter++;
//fprintf(result_file, "Counter: %i\n", counter);
if (counter > 1){
return 0;
}
}
}
counter = 0;
double_inc_level2[i] = added;
}
return 1;
}
int test_two(first_line target1, first_line target2, int *single_inc_level1, int *single_inc_level2, int *double_inc_level1, int *double_inc_level2, FILE *result_file){
for (int i = 0; i < (target1->order - 1); i++){
single_inc_level1[i] = 0;
single_inc_level2[i] = 0;
}
for (int l = 0; l < (target1->order - 2); l++){
double_inc_level1[l] = 0;
double_inc_level2[l] = 0;
}
if (test_lines(target1, target2, single_inc_level1, single_inc_level2, double_inc_level1, double_inc_level2, result_file) == 1){
return 1;
}
else{
return 0;
}
}
void permute_test(first_line target1, first_line target2, int start, FILE *result_file, int *single_inc_level1, int *single_inc_level2, int *double_inc_level1, int *double_inc_level2, int which){
if (target1->order == start){
if (which == 0){
permute_test(target2, target1, 1, result_file, single_inc_level1, single_inc_level2, double_inc_level1, double_inc_level2, which + 1);
}
else if (which == 1){
if (test_two(target1, target2, single_inc_level1, single_inc_level2, double_inc_level1, double_inc_level2, result_file) == 1){
fprintf(result_file, "\n");
print_line(target1, result_file);
print_line(target2, result_file);
}
}
else{
fprintf(result_file, "$which HAS VALUE %i", which);
}
//print_line(target, result_file);
return;
}
int j = start;
for (j = start; j < target1->order; j++){
int holder = target1->values[start];
target1->values[start] = target1->values[j];
target1->values[j] = holder;
permute_test(target1, target2, start + 1, result_file, single_inc_level1, single_inc_level2, double_inc_level1, double_inc_level2, which);
holder = target1->values[j];
target1->values[j] = target1->values[start];
target1->values[start] = holder;
}
return;
}
void test_all_iterations(first_line target1, first_line target2, FILE *results_file){
int *divisor_list = malloc(sizeof(int) * MAXDIVISORS);
int *single_inc_level1 = malloc(sizeof(int) * (target1->order - 1));
int *single_inc_level2 = malloc(sizeof(int) * (target1->order - 1));
int *double_inc_level1 = malloc(sizeof(int) * (target1->order - 2));
int *double_inc_level2 = malloc(sizeof(int) * (target1->order - 2));
generate_divisors(target1->order, divisor_list);
/* ----TESTING PRINT----
fprintf(results_file, "\nDIVISOR LIST: ");
for (int t = 0; t < 9; t++){
fprintf(results_file, "%i ", divisor_list[t]);
}
fprintf(results_file, "\n\n");*/
for (int i = 0; i < MAXDIVISORS; i++){
if (divisor_list[i] == target1->values[1]){
goto TESTY;
}
if (divisor_list[i] == 0){
break;
}
fprintf(results_file, "\n");
for (int x = 2; x <= target1->order; x++){
if (divisor_list[i] == target1->values[x]){
target1->values[x] = target1->values[1];
target1->values[1] = divisor_list[i];
break;
}
}
TESTY:permute_test(target1, target2, 2, results_file, single_inc_level1, single_inc_level2, double_inc_level1, double_inc_level2, 0);
}
free(divisor_list);
free(single_inc_level1);
free(single_inc_level2);
free(double_inc_level1);
free(double_inc_level2);
}