-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTC.java
231 lines (204 loc) · 6.09 KB
/
CTC.java
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
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class CTC {
/**
* Some Abbreviations used:
* BFM -> Brute Force Method
* PM --> Parallel Method
* OCM -> Optimized Column Method
* @param args
*/
public static void main(String[] args) {
int[] startingNumbers = { 0, 1, 4, 5, 8, 9 };
int row = 5233;
int col = 6;
StopwatchCPU sw;
/*BRUTE FORCE TESTING for SMSPortal Code. TODO: Separate testing for these */
int [][] BF_grid = BF_generateGrid(row);
sw = new StopwatchCPU();
System.out.println ("Code found via BFM: "+ extractElement(row, col, BF_grid));
System.out.println("Time Taken: "+ sw.elapsedTime());
/*PARALLEL FIND TESTING */
int [][] Parallel_grid = Parallel_generateGrid(row, startingNumbers);
sw = new StopwatchCPU();
System.out.println ("Code found via PM: "+ extractElement(row, col, Parallel_grid));
System.out.println("Time Taken: "+ sw.elapsedTime());
/*OPTIMIZED COLUMN TESTING */
sw = new StopwatchCPU();
System.out.println("Code found via OCM: "+ valueAt_row(row, startingNumbers[col-1], true));
System.out.println("Time Taken: "+ sw.elapsedTime());
}
/*******************************************************************
* Optimised Column Method
*******************************************************************/
/**
* Optimises space usage by not generating an array
* @param rowToFind desired row (given that the row we are starting at is row 1)
* @param row1Value
* @param first if it is the first row then set the add10 variable to true otherwise false
* so the algorithm knows whent to add 10 or add 2 to follow the pattern and crack the code
* @return
*/
public static int valueAt_row(int rowToFind, int row1Value, boolean first){
int insert = row1Value;
boolean add10 = true;
for (int row = 1; row <= rowToFind; row++) {
if (row == 1) {
continue;
} else if (add10) {
insert += 10;
} else {
insert += 2;
}
add10 = !add10; /* flip the switch */
}
return insert;
}
/*******************************************************************
* Parallel Generation
*******************************************************************/
public static int[][] Parallel_generateGrid(int rowCount, int[] startingNumbers) {
/* INITIALIZATION of algorithm */
int nRows = rowCount;
if (rowCount % 2 != 0)
nRows = rowCount + 1;
int[][] out = new int[nRows][6];
ExecutorService executor = Executors.newFixedThreadPool(6);
for (int col = 1; col <= 6; col++) {
final int currentCol = col;
executor.execute((() -> genColumn(out, currentCol, startingNumbers[currentCol - 1])));
}
// Shutdown the executor
executor.shutdown();
try {
// Wait for all tasks to finish
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
return out;
}
public static void genColumn(int[][] grid, int col, int startingNumber) {
int insert = startingNumber;
boolean add10 = true;
for (int row = 0; row < grid.length; row++) {
if (row == 0) {
grid[row][col - 1] = insert;
} else if (add10) {
insert += 10;
grid[row][col - 1] = insert;
} else {
insert += 2;
grid[row][col - 1] = insert;
}
add10 = !add10; /* flip the switch */
}
}
/*******************************************************************
* Brute Force Generation Methods
*******************************************************************/
public static int[][] BF_generateGrid(int rowCount) {
int hPtr = 1; /* Ranges 1-3 */
int vPtr = 1; /* Ranges 1-2 */
int row = 0, col = 0; /* Track the index in the grid */
int i = 0;
/* Code should populate 2 rows at a time */
/* INITIALIZATION of algorithm */
int nRows = rowCount;
if (rowCount % 2 != 0)
nRows = rowCount + 1;
int[][] grid = init2D(nRows, 6);
/* Population of Grid */
System.out.println("nRows: " + nRows);
while (row + 1 < nRows) {
/*
* Algorithm
* 1. Fill Cell with I (grid[row][col] = i)
* <i++>
* 2. col ++;
* 3. Fill Cell with I
* <i++>
* 4. col --; row++;
* 5. Fill Cell with I
* <i++>
* 6. col ++;
* 7. Fill Cell with I
* <i++>
* 8. -- Set up for next section: hPtr++; col++; row--;
* 9. IF (hPtr >=3){
* Reset col to 0
* hPtr = 1;
* Row+=2 (to start a new double row and to undo the -- in 8 while adding 1)}
* 10. Return to 1.
*/
/* This if statement is in a way step 8.5 */
if (hPtr <= 3) {
/* Step 1- 7 */
grid[row][col] = i;
i++;
col++;
grid[row][col] = i;
i++;
col--;
row++;
grid[row][col] = i;
i++;
col++;
// System.out.println("row: " + row + " col: " + col + " VALUE: " + i);
grid[row][col] = i;
i++;
/* Step 8 */
hPtr++;
col++;
row--;
} else {
/* Step 9 */
// System.out.println("RESET");
col = 0;
hPtr = 1;
row += 2;
}
}
return grid;
}
public static int[] BF_extractRow(int rowNr, int[][] grid) {
int[] out = new int[6];
for (int i = 0; i < 6; i++) {
out[i] = grid[rowNr - 1][i];
}
return out;
}
/*******************************************************************
* General Matrix/2D array methods
*******************************************************************/
public static int[][] init2D(int rows, int cols) {
int[][] out = new int[rows][cols];
for (int r = 0; r < out.length; r++) {
for (int c = 0; c < out[0].length; c++) {
out[r][c] = 0;
}
}
return out;
}
public static void printArr(int[][] array) {
for (int r = 0; r < array.length; r++) {
System.out.print("ROW: " + (r + 1) + "\t|");
for (int c = 0; c < array[0].length; c++) {
System.out.print(array[r][c] + "\t|");
}
System.out.println();
}
}
public static void printArr(int[] array) {
for (int c = 0; c < array.length; c++) {
System.out.print(array[c] + "\t|");
}
System.out.println();
}
public static int extractElement(int rowNr, int colNr, int[][] grid) {
int[] row = BF_extractRow(rowNr, grid);
return row[colNr - 1];
}
}