-
Notifications
You must be signed in to change notification settings - Fork 2
/
UploadedSkyScraper.java
474 lines (398 loc) · 13.4 KB
/
UploadedSkyScraper.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.HashSet;
import java.util.Collections;
import java.util.Arrays;
public class UploadedSkyScraper {
static List<int[]> allRowPermutations;
static int[] ruleUses = new int[10];
public static int[][] solvePuzzle (int[] clues) {
int sideLength = (clues.length + 1) / 4;
// solve based on clues.
// Lock in squares determined by clues
int[][] solution = new int[sideLength][sideLength];
allRowPermutations = getAllPossibleRows(sideLength);
solveByCluesStart(solution, clues);
int[][] lockGrid = new int[sideLength][sideLength];
for (int i = 0; i < sideLength; i++) {
for (int j = 0; j < sideLength; j++) {
lockGrid[i][j] = solution[i][j];
}
}
// benchmarking
int[] count = new int[1];
count[0] = 0;
solution = fillInGrid(solution, getPossibleRowsWithLocking(solution), lockGrid, 0, clues, count);
System.out.println(Arrays.toString(ruleUses));
return solution;
}
public static void solveByCluesStart(int[][] solution, int[] clues) {
for (int i = 0; i < clues.length; i++) {
int clueVal = clues[i];
if (clueVal == 1 || clueVal == solution.length) {
setRowCol(solution, i, clueVal);
}
}
}
// given a solution return all possible values for the specifed row
public static List<int[]> getPossibleRowsWithPartialCompletion(int[][] solution, List<int[]> possibleWithLocking, int rowNum) {
List<int[]> possibleForRow = new ArrayList<int[]>();
List<Set<Integer>> buildingsInColumn = new ArrayList<Set<Integer>>();
for (int i = 0; i < solution.length; i++) {
Set<Integer> set = new HashSet<Integer>();
buildingsInColumn.add(set);
}
for (int i = 0; i < solution.length; i++) {
for (int j = 0; j < solution.length; j++) {
int val = solution[i][j];
if (val != 0 && i != rowNum) {
buildingsInColumn.get(j).add(val);
}
}
}
for (int[] row : possibleWithLocking) {
Boolean isAMatch = true;
for (int i = 0; i < row.length; i++) {
int valToCheck = row[i];
if (buildingsInColumn.get(i).contains(row[i])) {
isAMatch = false;
break;
}
}
if (isAMatch) {
possibleForRow.add(row);
}
}
return possibleForRow;
}
public static List<List<int[]>> getPossibleRowsWithLocking(int[][] solution) {
List<List<int[]>> allPossibleRowsWithLocking = new ArrayList<List<int[]>>();
// for a given row filter out only those with the nums in the right place
for (int i = 0; i < solution.length; i++) {
// loop through each row and create the list of matches
List<int[]> matches = new ArrayList<int[]>();
allPossibleRowsWithLocking.add(matches);
for (int[] row : allRowPermutations) {
Boolean isAMatch = true;
for (int j = 0; j < row.length; j++) {
int lockedVal = solution[i][j];
if (lockedVal != 0 && lockedVal != row[j]) {
isAMatch = false;
break;
}
}
if (isAMatch) {
matches.add(row);
}
}
}
return allPossibleRowsWithLocking;
}
public static int getVisibleBuildings(int[] row) {
int visibleBuildings = 0;
int maxHeight = -1;
for (int height : row) {
if (height > maxHeight) {
visibleBuildings++;
maxHeight = height;
}
}
return visibleBuildings;
}
public static List<int[]> maxFilteredRows(int[][] solution, List<int[]> rows, int[] clues, int rowNum) {
List<int[]> maxFilteredRows = new ArrayList<int[]>(rows.size());
int dim = solution.length;
for (int[] row : rows) {
// Start by using row clues to filter
int clueNum1 = dim + rowNum;
int clueNum2 = clues.length - 1 - rowNum;
int clueVal1 = clues[clueNum1];
int clueVal2 = clues[clueNum2];
if (clueVal2 != 0 && getVisibleBuildings(row) != clueVal2) {
ruleUses[0]++;
continue;
}
int[] reverseArray = new int[dim];
for (int i = 0; i < dim; i++) {
reverseArray[i] = row[dim - 1 - i];
}
if (clueVal1 != 0 && getVisibleBuildings(reverseArray) != clueVal1) {
ruleUses[1]++;
continue;
}
int maxPos = -1;
Boolean hasZero = false;
for (int i = 0; i < dim; i++) {
if (row[i] == dim) {
maxPos = i;
}
}
if (maxPos != -1 && clues[maxPos] != 0) {
int[] col = new int[rowNum + 1];
for (int j = 0; j < col.length - 1; j++) {
col[j] = solution[j][maxPos];
if (solution[j][maxPos] == 0) {
hasZero = true;
break;
}
}
col[rowNum] = dim;
if (!hasZero) {
col[rowNum] = dim;
if (getVisibleBuildings(col) != clues[maxPos]) {
ruleUses[3]++;
continue;
}
}
}
// from the other direction
int bottomClueNum = dim *3 - maxPos - 1;
if (clues[bottomClueNum] != 0 && clues[bottomClueNum] > dim - rowNum + 1) {
ruleUses[4]++;
continue;
}
maxFilteredRows.add(row);
}
return maxFilteredRows;
}
public static int[][] fillInGrid(int[][] solution,
List<List<int[]>> possibleRowsWithLocking, int[][] lockGrid,
int rowNum, int[] clues, int[] count) {
Boolean isLastRow = rowNum == (solution.length - 1);
// try to fill in grid a bit before getting rows and what not.
List<int[]> possibleRows = possibleRowsWithLocking.get(rowNum);
List<int[]> possibleRowsFromPartialCompleteionAndLocking = getPossibleRowsWithPartialCompletion(solution, possibleRows, rowNum);
List<int[]> maxFilteredRows = maxFilteredRows(solution, possibleRowsFromPartialCompleteionAndLocking, clues, rowNum);
for (int i = 0; i < maxFilteredRows.size(); i++) {
count[0]++;
solution[rowNum] = Arrays.copyOf(maxFilteredRows.get(i), solution.length);
clearLaterRows(solution, rowNum, lockGrid);
if (isLastRow) {
if (isValidCol(solution) && isClueSafe(solution, clues, false)) {
return solution;
}
} else {
if (isValidCol(solution) && isClueSafe(solution, clues, true)) {
int[][] result = fillInGrid(solution, possibleRowsWithLocking, lockGrid, rowNum+1, clues, count);
if (result[solution.length - 1][solution.length -1] != 0) {
return result;
}
}
}
}
return new int[solution.length][solution.length];
}
public static void clearLaterRows(int[][] arr, int rowNum, int[][] lockGrid) {
for (int i = rowNum + 1; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = lockGrid[i][j];
}
}
}
public static Boolean isComplete(int[][] solution) {
for (int i = 0; i < solution.length; i++) {
for (int j = 0; j < solution[1].length; j++) {
if (solution[i][j] < 1 || solution[i][j] > solution.length) {
return false;
}
}
}
return true;
}
public static Boolean isValidCol(int[][] solution) {
// rows will always be valid right...
for (int j = 0; j < solution[0].length; j++) {
int[] heightCounts = new int[solution.length];
for (int i = 0; i < solution.length; i++) {
int height = solution[i][j];
if (height > 0) {
heightCounts[height - 1]++;
}
}
for (int i = 0; i < heightCounts.length; i++) {
if (heightCounts[i] > 1) {
return false;
}
}
}
return true;
}
public static Boolean isClueSafe(int[][] solution, int[] clues, Boolean tryFillIn) {
for (int i = 0; i < clues.length; i++) {
if (clues[i] != 0) {
int[] rowCol = getRowCol(solution, i);
int firstZeroIndex = -1;
int zeroCount = 0;
int maxBulidingIndex = -1;
int maxSoFar = -1;
int visibleBuildings = 0;
for (int j = 0; j < rowCol.length; j++) {
if (rowCol[j] == 0) {
if (firstZeroIndex == -1) {
firstZeroIndex = j;
}
zeroCount++;
} else if (rowCol[j] == rowCol.length) {
maxBulidingIndex = j;
}
if (rowCol[j] > maxSoFar) {
visibleBuildings++;
maxSoFar = rowCol[j];
}
}
if (firstZeroIndex != -1) {
if (maxBulidingIndex != -1 && maxBulidingIndex < firstZeroIndex) {
if (clues[i] != visibleBuildings) {
return false;
}
} else if (tryFillIn && zeroCount == 1) {
Set<Integer> usedValues = new HashSet<Integer>();
for (int n = 0; n < rowCol.length; n++) {
usedValues.add(rowCol[n]);
}
Set<Integer> remainingValues = new HashSet<Integer>();
for (int n = 0; n < solution.length; n++) {
if (!usedValues.contains(n + 1)) {
remainingValues.add(n + 1);
}
}
if (remainingValues.size() == 1 && zeroCount == 1) {
setValueFromClueAndIndex(solution, i, firstZeroIndex, remainingValues.iterator().next());
}
}
} else if (clues[i] != visibleBuildings) {
return false;
}
}
}
return true;
}
public static int[] getRowCol(int[][] solution, int clueNum) {
int[] result = new int[solution.length];
int section = clueNum / solution.length;
Boolean backwards = section == 1 || section == 2;
Boolean isCol = section == 0 || section == 2;
Boolean isCountBack = section == 2 || section == 3;
int index = clueNum % solution.length;
if (isCountBack) {
index = solution.length - index - 1;
}
for (int i = 0; i < solution.length; i++) {
if (isCol) {
result[i] = solution[i][index];
} else {
result[i] = solution[index][i];
}
}
if (backwards) {
for (int i = 0; i < solution.length / 2; i++) {
int tmp = result[i];
result[i] = result[solution.length - 1 - i];
result[solution.length - 1 - i] = tmp;
}
}
return result;
}
public static void setValueFromClueAndIndex(int[][] solution, int clueNum, int rowIndex, int value) {
int section = clueNum / solution.length;
int iResult;
int jResult;
if (section == 0) {
iResult = rowIndex;
jResult = clueNum;
} else if (section == 1) {
iResult = clueNum % solution.length;
jResult = solution.length - rowIndex - 1;
} else if (section == 2) {
iResult = solution.length - 1 - rowIndex;
jResult = solution.length - 1 - (clueNum % solution.length);
} else {
iResult = solution.length -1 - (clueNum % solution.length);
jResult = rowIndex;
}
solution[iResult][jResult] = value;
}
public static void setRowCol(int[][] solution, int clueNum, int clueVal) {
List<Integer> list = new ArrayList<Integer>();
int section = clueNum / solution.length;
Boolean backwards = section == 1 || section == 2;
Boolean isCol = section == 0 || section == 2;
Boolean isCountBack = section == 2 || section == 3;
int index = clueNum % solution.length;
if (isCountBack) {
index = solution.length - index - 1;
}
int clueMinVal = 1;
int clueMaxVal = solution.length;
for (int i = 0; i < solution.length; i++) {
int realI = i;
if (backwards) {
realI = solution.length - i - 1;
}
if (isCol) {
if (clueVal == clueMinVal) {
solution[realI][index] = clueMaxVal;
break;
} else if (clueVal == clueMaxVal) {
solution[realI][index] = i + 1;
}
} else {
if (clueVal == clueMinVal) {
solution[index][realI] = clueMaxVal;
break;
} else if (clueVal == clueMaxVal) {
solution[index][realI] = i + 1;
}
}
}
}
public static List<int[]> getAllPossibleRows(int size) {
List<List<Integer>> allPerms = new ArrayList<>();
List<int[]> allPermsFormatted = new ArrayList<>();
int[] nums = new int[size];
for (int i = 0; i < nums.length; i++) {
nums[i] = i+1;
}
allPerms = permute(nums);
for (List<Integer> list : allPerms) {
int[] arr = new int[list.size()];
for (int i =0; i < list.size(); i++) {
arr[i] = list.get(i);
}
allPermsFormatted.add(arr);
}
return allPermsFormatted;
}
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> results = new ArrayList<List<Integer>>();
if (nums == null || nums.length == 0) {
return results;
}
List<Integer> result = new ArrayList<>();
dfs(nums, results, result);
return results;
}
public static void dfs(int[] nums, List<List<Integer>> results, List<Integer> result) {
if (nums.length == result.size()) {
List<Integer> temp = new ArrayList<>(result);
results.add(temp);
}
for (int i=0; i<nums.length; i++) {
if (!result.contains(nums[i])) {
result.add(nums[i]);
dfs(nums, results, result);
result.remove(result.size() - 1);
}
}
}
public static void printArr(int[][] arr) {
System.out.println();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
}
}