-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathold code
More file actions
305 lines (218 loc) · 7.86 KB
/
old code
File metadata and controls
305 lines (218 loc) · 7.86 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
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
Random code:
// for (Map.Entry<String, Integer> pixel : sortedPixels) { // for each pixel in county
// boolean withinRange = false;
// // get it's x and y coordinates
// String[] rawData = pixel.getKey().split(", ");
// int i = Integer.parseInt(rawData[0].trim());
// int j = Integer.parseInt(rawData[1].trim());
// // check to see if in range of any already-made stations
// for (Station station : countyStations) {
// withinRange = station.withinRange(i, j);
// if (withinRange) {
// station.add(i, j);
// break;
// }
// }
// // if not in range of any already-made stations
// if (withinRange == false) {
// Station nStation = new Station(i, j);
// countyStations.add(nStation);
// }
// }
String pixel = i + ", " + j;
Station station = new Station(i, j);
count++;
// base cases
if (countyStations.contains(station)) return; // if station was already created, stop
if (!countyPixels.contains(pixel)) return; // if pixel is not within county, stop
// check if within range pixels are also within county
int x = i+1;
pixel = x + ", " + j;
if (station.withinRange(x, j) && countyPixels.contains(pixel))
station.add(x, j);
x = i-1;
pixel = x + ", " + j;
if (station.withinRange(x, j) && countyPixels.contains(pixel))
station.add(x, j);
int y = j+1;
pixel = i + ", " + y;
if (station.withinRange(i, y) && countyPixels.contains(pixel))
station.add(i, y);
y = j-1;
pixel = i + ", " + y;
if (station.withinRange(i, y) && countyPixels.contains(pixel))
station.add(i, y);
// otherwise, add to list of stations made
countyStations.add(station);
// recursively find the remaining stations
makeStations(i+1, j+2, countyPixels);
makeStations(i+2, j-1, countyPixels);
makeStations(i-1, j-2, countyPixels);
makeStations(i-2, j+1, countyPixels);
String pixel = i + ", " + j;
int x = i-offsetX;
int y = j-offsetY;
if (!countyPixels.contains(pixel)) return;
if (x >= X || x < 0) return;
if (y >= Y || y < 0) return;
if (visited[x][y]) return;
count++;
// check if any of the surrounding is a station.. if so, this is not the center of a station
boolean thisIsStation = true;
if (countyStations.contains(new Station(i, j+1))) thisIsStation = false;
if (countyStations.contains(new Station(i+1, j+1))) thisIsStation = false;
if (countyStations.contains(new Station(i+1, j))) thisIsStation = false;
if (countyStations.contains(new Station(i+1, j-1))) thisIsStation = false;
if (countyStations.contains(new Station(i, j-1))) thisIsStation = false;
if (countyStations.contains(new Station(i-1, j-1))) thisIsStation = false;
if (countyStations.contains(new Station(i-1, j))) thisIsStation = false;
if (countyStations.contains(new Station(i-1, j+1))) thisIsStation = false;
// if this qualifies as a station
if (thisIsStation) {
// update visited array
visited[x][y] = true;
if (x < X && x >= 0 && (y+1) >= 0 && (y+1) < Y)
visited[x][y+1] = true;
if ((x+1) < X && (x+1) >= 0 && y >= 0 && y < Y)
visited[x+1][y] = true;
if (x < X && x >= 0 && (y-1) >= 0 && (y-1) < Y)
visited[x][y-1] = true;
if ((x-1) < X && (x-1) >= 0 && y >= 0 && y < Y)
visited[x-1][y] = true;
Station station = new Station(i, j);
int y1 = j+1;
pixel = i + ", " + y1;
if (station.withinRange(i, y1) && countyPixels.contains(pixel))
station.add(i, y1);
int x1 = i+1;
pixel = x1 + ", " + j;
if (station.withinRange(x1, j) && countyPixels.contains(pixel))
station.add(x1, j);
y1 = j-1;
pixel = i + ", " + y1;
if (station.withinRange(i, y1) && countyPixels.contains(pixel))
station.add(i, y1);
x1 = i-1;
pixel = x1 + ", " + j;
if (station.withinRange(x1, j) && countyPixels.contains(pixel))
station.add(x1, j);
countyStations.add(station);
makeStations(i+1, j+1, countyPixels, offsetX, offsetY);
makeStations(i+1, j-1, countyPixels, offsetX, offsetY);
makeStations(i-1, j-1, countyPixels, offsetX, offsetY);
makeStations(i-1, j+1, countyPixels, offsetX, offsetY);
}
else {
if (x < X && x >= 0 && (y+1) >= 0 && (y+1) < Y)
if (!visited[x][y+1])
makeStations(i, j+1, countyPixels, offsetX, offsetY);
if ((x+1) < X && (x+1) >= 0 && y >= 0 && y < Y)
if (!visited[x+1][y])
makeStations(i+1, j, countyPixels, offsetX, offsetY);
if (x < X && x >= 0 && (y-1) >= 0 && (y-1) < Y)
if (!visited[x][y-1])
makeStations(i, j-1, countyPixels, offsetX, offsetY);
if ((x-1) < X && (x-1) >= 0 && y >= 0 && y < Y)
if (!visited[x-1][y])
makeStations(i-1, j, countyPixels, offsetX, offsetY);
}
return;
public static void makeStations(int i, int j, Set<String> countyPixels) {
String pixel = i + ", " + j;
// base cases
if (!countyPixels.contains(pixel)) { // if pixel is out of county bounds
int x = i+1;
pixel = x + ", " + j;
if (countyPixels.contains(pixel)) {
Station station = new Station(x, j);
countyStations.add(station);
}
x = i-1;
pixel = x + ", " + j;
if (countyPixels.contains(pixel)) {
Station station = new Station(x, j);
countyStations.add(station);
}
int y = j+1;
pixel = i + ", " + y;
if (countyPixels.contains(pixel)) {
Station station = new Station(i, y);
countyStations.add(station);
}
y = j-1;
pixel = i + ", " + y;
if (countyPixels.contains(pixel)) {
Station station = new Station(x, j);
countyStations.add(station);
}
return;
}
for (Station station : countyStations) {
if (station.contains(i, j)) return; // if pixel is already in another station
}
Station station = new Station(i, j);
// add NESW pixels to station if applicable
int x = i+1;
pixel = x + ", " + j;
if (station.withinRange(x, j) && countyPixels.contains(pixel))
station.add(x, j);
x = i-1;
pixel = x + ", " + j;
if (station.withinRange(x, j) && countyPixels.contains(pixel))
station.add(x, j);
int y = j+1;
pixel = i + ", " + y;
if (station.withinRange(i, y) && countyPixels.contains(pixel))
station.add(i, y);
y = j-1;
pixel = i + ", " + y;
if (station.withinRange(i, y) && countyPixels.contains(pixel))
station.add(i, y);
// add station to final list
countyStations.add(station);
// recursively find other stations
makeStations(i+1, j+2, countyPixels);
makeStations(i+2, j-1, countyPixels);
makeStations(i-1, j-2, countyPixels);
makeStations(i-2, j+1, countyPixels);
}
-------------------
// save each new pixel keep track of how many departures leave from each pixel
int i = Integer.parseInt(rawData[5]);
int j = Integer.parseInt(rawData[4]);
if (i >= maxX && j >= maxY) {
maxX = i;
maxY = j;
}
if (i <= minX && j <= minY) {
minX = i;
minY = j;
}
String pixel = i + ", " + j;
int count = 1;
if (stationPixels.containsKey(pixel)) {
count = stationPixels.get(pixel);
count++;
}
stationPixels.put(pixel, count);
X = maxX - minX;
Y = maxY - minY;
visited = new boolean[X][Y];
List<Map.Entry<String, Integer>> sortedPixels = new ArrayList<Map.Entry<String, Integer>>(stationPixels.entrySet());
// sort list of pixels by number of departures
Collections.sort(sortedPixels,
new Comparator<Map.Entry<String,Integer>>() {
@Override
public int compare(Map.Entry<String,Integer> e1, Map.Entry<String,Integer> e2) {
return e2.getValue().compareTo(e1.getValue());
}
}
);
// AT SOME POINT PRINT INFORMATION ABOUT DEPARTURES IN EACH PIXEL, AND TOP 10 ORIGINATING PIXELS, EXPECTED NUMBER OF DEPARTURES PER PIXEL..?
String[] topPixel = sortedPixels.get(0).getKey().split(", ");
int x = Integer.parseInt(topPixel[0]);
int y = Integer.parseInt(topPixel[1]);
System.out.println(stationPixels.keySet().size());
makeStations(x, y, stationPixels.keySet());
System.out.println(count);
System.out.println(countyStations.size());