-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnowCleaningVis.java
602 lines (504 loc) · 19.7 KB
/
SnowCleaningVis.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.security.SecureRandom;
import java.util.*;
import java.util.List;
class Constants {
public static final int SIMULATION_TIME = 2000;
public static final int MAX_WORKERS = 100;
public static final String DIR_STR = "DLUR";
public static final int[] DR = new int[]{1, 0, -1, 0};
public static final int[] DC = new int[]{0, -1, 0, 1};
}
class CloudType {
public static final int MIN_CLOUD_SIZE = 1;
public static final int MAX_CLOUD_SIZE = 3;
public static final int MIN_CLOUD_TIME = 10;
public static final int MAX_CLOUD_TIME = 25;
int size;
int liveTime;
double snowProbGlobal;
double[][] snowProbLocal;
int[] moveProb;
int sumMoveProb;
public int getDirection(Random rnd) {
int val = rnd.nextInt(sumMoveProb);
int res = 0;
while (val >= moveProb[res]) {
val -= moveProb[res];
res++;
}
return res;
}
public CloudType(Random rnd) {
size = rnd.nextInt(MAX_CLOUD_SIZE - MIN_CLOUD_SIZE + 1) + MIN_CLOUD_SIZE;
liveTime = rnd.nextInt(MAX_CLOUD_TIME - MIN_CLOUD_TIME + 1) + MIN_CLOUD_TIME;
snowProbGlobal = rnd.nextDouble();
snowProbLocal = new double[2 * size + 1][2 * size + 1];
for (int i = 0; i <= 2 * size; i++) {
for (int j = 0; j <= 2 * size; j++) {
snowProbLocal[i][j] = rnd.nextDouble();
}
}
moveProb = new int[Constants.DR.length];
for (int i = 0; i < moveProb.length; i++) {
double x = rnd.nextDouble();
moveProb[i] = (int) Math.ceil(100 * x * x);
sumMoveProb += moveProb[i];
}
}
}
class Cell implements Comparable<Cell> {
public int r, c;
public Cell(int r, int c) {
this.r = r;
this.c = c;
}
public boolean equals(Object other) {
if (!(other instanceof Cell)) {
return false;
}
Cell otherCell = (Cell) other;
return this.r == otherCell.r && this.c == otherCell.c;
}
public int hashCode() {
return 100 * r + c;
}
public int compareTo(Cell other) {
return (r == other.r ? c - other.c : r - other.r);
}
}
class TestCase {
public static final int MIN_CLOUD_TYPES = 1;
public static final int MAX_CLOUD_TYPES = 10;
public static final int MIN_CLOUD_COUNT = 50;
public static final int MAX_CLOUD_COUNT = 200;
public static final int MIN_BOARD_SIZE = 20;
public static final int MAX_BOARD_SIZE = 50;
public static final int MIN_SALARY = 10;
public static final int MAX_SALARY = 100;
public static final int MIN_SNOW_FINE = 10;
public static final int MAX_SNOW_FINE = 100;
public int boardSize;
public int salary;
public int snowFine;
public int cloudTypeCnt;
public CloudType[] cloudTypes;
public int cloudCnt;
Set<Cell>[] snowFalls = new Set[Constants.SIMULATION_TIME];
public TestCase(long seed) {
SecureRandom rnd = null;
try {
rnd = SecureRandom.getInstance("SHA1PRNG");
} catch (Exception e) {
System.err.println("ERROR: unable to generate test case.");
System.exit(1);
}
rnd.setSeed(seed);
boardSize = rnd.nextInt(MAX_BOARD_SIZE - MIN_BOARD_SIZE + 1) + MIN_BOARD_SIZE;
salary = rnd.nextInt(MAX_SALARY - MIN_SALARY + 1) + MIN_SALARY;
snowFine = rnd.nextInt(MAX_SNOW_FINE - MIN_SNOW_FINE + 1) + MIN_SNOW_FINE;
cloudTypeCnt = rnd.nextInt(MAX_CLOUD_TYPES - MIN_CLOUD_TYPES + 1) + MIN_CLOUD_TYPES;
cloudTypes = new CloudType[cloudTypeCnt];
for (int i = 0; i < cloudTypeCnt; i++) {
cloudTypes[i] = new CloudType(rnd);
}
cloudCnt = rnd.nextInt(MAX_CLOUD_COUNT - MIN_CLOUD_COUNT + 1) + MIN_CLOUD_COUNT;
for (int t = 0; t < Constants.SIMULATION_TIME; t++) {
snowFalls[t] = new HashSet<Cell>();
}
for (int i = 0; i < cloudCnt; i++) {
int type = rnd.nextInt(cloudTypeCnt);
int curRow = rnd.nextInt(boardSize);
int curCol = rnd.nextInt(boardSize);
int startTime = rnd.nextInt(Constants.SIMULATION_TIME);
for (int t = startTime; t < startTime + cloudTypes[type].liveTime && t < Constants.SIMULATION_TIME; t++) {
if (rnd.nextDouble() < cloudTypes[type].snowProbGlobal) {
for (int r = 0; r <= 2 * cloudTypes[type].size; r++) {
for (int c = 0; c <= 2 * cloudTypes[type].size; c++) {
if (rnd.nextDouble() < cloudTypes[type].snowProbLocal[r][c]) {
int snowR = curRow + r - cloudTypes[type].size;
int snowC = curCol + c - cloudTypes[type].size;
if (snowR >= 0 && snowR < boardSize && snowC >= 0 && snowC < boardSize) {
snowFalls[t].add(new Cell(snowR, snowC));
}
}
}
}
}
int dir = cloudTypes[type].getDirection(rnd);
curRow += Constants.DR[dir];
curCol += Constants.DC[dir];
}
}
}
}
class Drawer extends JFrame {
public static final int EXTRA_WIDTH = 300;
public static final int EXTRA_HEIGHT = 100;
public World world;
public DrawerPanel panel;
public int cellSize, boardSize;
public int width, height;
public boolean pauseMode = false;
class DrawerKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent e) {
synchronized (keyMutex) {
if (e.getKeyChar() == ' ') {
pauseMode = !pauseMode;
}
keyPressed = true;
keyMutex.notifyAll();
}
}
}
class DrawerPanel extends JPanel {
public void paint(Graphics g) {
g.setColor(Color.DARK_GRAY);
g.fillRect(15, 15, cellSize * boardSize + 1, cellSize * boardSize + 1);
g.setColor(Color.RED);
for (int i = 0; i <= boardSize; i++) {
g.drawLine(15 + i * cellSize, 15, 15 + i * cellSize, 15 + cellSize * boardSize);
g.drawLine(15, 15 + i * cellSize, 15 + cellSize * boardSize, 15 + i * cellSize);
}
g.setColor(Color.WHITE);
for (int i=0; i < boardSize; i++) {
for (int j=0; j < boardSize; j++) {
if (world.haveSnow[i][j]) {
g.fillRect(15 + j * cellSize + 1, 15 + i * cellSize + 1, cellSize - 2, cellSize - 2);
}
}
}
g.setColor(Color.BLUE);
synchronized (world.workersLock) {
for (Cell worker : world.workers) {
g.fillRect(15 + worker.c * cellSize + 1, 15 + worker.r * cellSize + 1, cellSize - 2, cellSize - 2);
}
}
g.setColor(Color.BLACK);
g.setFont(new Font("Arial", Font.BOLD, 12));
Graphics2D g2 = (Graphics2D)g;
int horPos = 40 + boardSize * cellSize;
g2.drawString("Board size = " + world.haveSnow.length, horPos, 30);
g2.drawString("Snow fine = " + world.fine, horPos, 50);
g2.drawString("Salary = " + world.salary, horPos, 70);
g2.drawString("Day = " + world.curDay, horPos, 105);
g2.drawString("Uncleared snow cells = " + world.snowCnt, horPos, 125);
synchronized (world.workersLock) {
g2.drawString("Workers = " + world.workers.size(), horPos, 145);
}
g2.drawString("Total snow fine = ", horPos, 180);
g2.drawString("" + world.totFine, horPos + 100, 180);
g2.drawString("Total salary = ", horPos, 200);
g2.drawString("" + world.totSalary, horPos + 100, 200);
g2.drawString("Current score = ", horPos, 220);
g2.drawString("" + (world.totFine + world.totSalary), horPos + 100, 220);
}
}
class DrawerWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent event) {
SnowCleaningVis.stopSolution();
System.exit(0);
}
}
final Object keyMutex = new Object();
boolean keyPressed;
public void processPause() {
synchronized (keyMutex) {
if (!pauseMode) {
return;
}
keyPressed = false;
while (!keyPressed) {
try {
keyMutex.wait();
} catch (InterruptedException e) {
// do nothing
}
}
}
}
public Drawer(World world, int cellSize) {
super();
panel = new DrawerPanel();
getContentPane().add(panel);
addWindowListener(new DrawerWindowListener());
this.world = world;
boardSize = world.haveSnow.length;
this.cellSize = cellSize;
width = cellSize * boardSize + EXTRA_WIDTH;
height = cellSize * boardSize + EXTRA_HEIGHT;
addKeyListener(new DrawerKeyListener());
setSize(width, height);
setTitle("Visualizer tool for problem SnowCleaning");
setResizable(false);
setVisible(true);
}
}
class World {
final Object workersLock = new Object();
int snowCnt;
boolean[][] haveSnow;
List<Cell> workers = new ArrayList<Cell>();
Set<Integer> usedWorkers = new HashSet<Integer>();
int salary, fine;
int totSalary, totFine;
int curDay = -1;
public World(int boardSize, int salary, int fine) {
this.salary = salary;
this.fine = fine;
haveSnow = new boolean[boardSize][boardSize];
}
public void updateTotalSalary() {
synchronized (workersLock) {
totSalary += salary * workers.size();
}
}
public void updateTotalFine() {
totFine += snowCnt * fine;
}
public void addSnow(int r, int c) {
if (!haveSnow[r][c]) {
snowCnt++;
haveSnow[r][c] = true;
}
}
public void removeSnow(int r, int c) {
if (haveSnow[r][c]) {
snowCnt--;
haveSnow[r][c] = false;
}
}
public void startNewDay() {
curDay++;
usedWorkers.clear();
}
public String addWorker(int r, int c) {
synchronized (workersLock) {
if (workers.size() == Constants.MAX_WORKERS) {
return "You are allowed to have at most " + Constants.MAX_WORKERS + " workers.";
} else if (r < 0 || r >= haveSnow.length || c < 0 || c >= haveSnow.length) {
return "You are trying to hire a worker at a cell outside the board.";
} else {
workers.add(new Cell(r, c));
usedWorkers.add(workers.size() - 1);
removeSnow(r, c);
return "";
}
}
}
public String moveWorker(int id, int dir) {
synchronized (workersLock) {
if (id < 0 || id >= workers.size()) {
return "You are trying to move worker which does not exist.";
} else if (usedWorkers.contains(id)) {
return "You are trying to execute a command for some worker more than once during the same turn.";
} else {
Cell worker = workers.get(id);
worker.r += Constants.DR[dir];
worker.c += Constants.DC[dir];
if (worker.r < 0 || worker.c < 0 || worker.r >= haveSnow.length || worker.c >= haveSnow.length) {
return "You are trying to move a worker outside the board.";
}
removeSnow(worker.r, worker.c);
usedWorkers.add(id);
return "";
}
}
}
public void cleanAllSnow() {
synchronized (workersLock) {
for (Cell worker : workers) {
removeSnow(worker.r, worker.c);
}
}
}
}
public class SnowCleaningVis {
public static String MOVE_REG_EXP = "M [1-9]?[0-9] [ULDR]";
public static String HIRE_REG_EXP = "H [1-9]?[0-9] [1-9]?[0-9]";
public static String WRONG_COMMAND_ERROR = "ERROR: Each worker command must be formatted either \"M <ID> <DIR>\"" +
" or \"H <ROW> <COL>\". Here <ID>, <ROW> and <COL> are integers from 0 to 99 without leading zeros" +
" and <DIR> is one of 'U', 'L', 'D', 'R'.";
public static String execCommand = null;
public static long seed = 1;
public static boolean vis = true;
public static int cellSize = 12;
public static int delay = 100;
public static boolean startPaused = false;
public static Process solution;
public int runTest() {
solution = null;
try {
solution = Runtime.getRuntime().exec(execCommand);
} catch (Exception e) {
System.err.println("ERROR: Unable to execute your solution using the provided command: "
+ execCommand + ".");
return -1;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(solution.getInputStream()));
PrintWriter writer = new PrintWriter(solution.getOutputStream());
new ErrorStreamRedirector(solution.getErrorStream()).start();
TestCase tc = new TestCase(seed);
writer.println(tc.boardSize);
writer.println(tc.salary);
writer.println(tc.snowFine);
writer.flush();
World world = new World(tc.boardSize, tc.salary, tc.snowFine);
Drawer drawer = null;
if (vis) {
drawer = new Drawer(world, cellSize);
if (startPaused) {
drawer.pauseMode = true;
}
}
for (int t = 0; t < Constants.SIMULATION_TIME; t++) {
world.startNewDay();
int snowFallCnt = tc.snowFalls[t].size();
Cell[] snowFalls = new Cell[snowFallCnt];
int pos = 0;
for (Cell cell : tc.snowFalls[t]) {
snowFalls[pos++] = cell;
}
Arrays.sort(snowFalls);
StringBuilder sb = new StringBuilder();
sb.append(snowFallCnt).append("\n");
for (int i=0; i < snowFallCnt; i++) {
sb.append(snowFalls[i].r).append("\n").append(snowFalls[i].c).append("\n");
world.addSnow(snowFalls[i].r, snowFalls[i].c);
}
writer.print(sb.toString());
writer.flush();
int commandCnt;
try {
commandCnt = Integer.parseInt(reader.readLine());
} catch (Exception e) {
System.err.println("ERROR: time step = " + t + " (0-based). Unable to get the number of worker commands" +
" from your solution.");
return -1;
}
for (int i = 0; i < commandCnt; i++) {
String command;
try {
command = reader.readLine();
} catch (Exception e) {
System.err.println("ERROR: time step = " + t + " + (0-based). Unable to read " + i + "-th (0-based)" +
" worker command from your solution.");
return -1;
}
if (command.length() > 10) {
System.err.println("ERROR: time step = " + t + ", worker command = " + i + " (0-based indices). " + WRONG_COMMAND_ERROR);
return -1;
}
if (command.matches(HIRE_REG_EXP)) {
String[] items = command.split(" ");
int row = Integer.parseInt(items[1]);
int col = Integer.parseInt(items[2]);
String msg = world.addWorker(row, col);
if (msg.length() > 0) {
System.err.println("ERROR: time step = " + t + ", worker command = " + i + " (0-based indices). " + msg);
return -1;
}
} else if (command.matches(MOVE_REG_EXP)) {
String[] items = command.split(" ");
int id = Integer.parseInt(items[1]);
int dir = Constants.DIR_STR.indexOf(items[2]);
String msg = world.moveWorker(id, dir);
if (msg.length() > 0 ){
System.err.println("ERROR: time step = " + t + ", worker command = " + i + " (0-based indices). " + msg);
return -1;
}
} else {
System.err.println("ERROR: time step = " + t + ", worker command = " + i + " (0-based indices). " + WRONG_COMMAND_ERROR);
return -1;
}
}
world.cleanAllSnow();
world.updateTotalFine();
world.updateTotalSalary();
if (vis) {
drawer.processPause();
drawer.repaint();
try {
Thread.sleep(delay);
} catch (Exception e) {
// do nothing
}
}
}
stopSolution();
System.out.println("Fine = " + world.totFine);
System.out.println("Salary = " + world.totSalary);
return world.totFine + world.totSalary;
}
public static void stopSolution() {
if (solution != null) {
try {
solution.destroy();
} catch (Exception e) {
// do nothing
}
}
}
public static void main(String[] args) {
for (int i = 0; i < args.length; i++)
if (args[i].equals("-exec")) {
execCommand = args[++i];
} else if (args[i].equals("-seed")) {
seed = Long.parseLong(args[++i]);
} else if (args[i].equals("-novis")) {
vis = false;
} else if (args[i].equals("-sz")) {
cellSize = Integer.parseInt(args[++i]);
} else if (args[i].equals("-delay")) {
delay = Integer.parseInt(args[++i]);
} else if (args[i].equals("-pause")) {
startPaused = true;
} else {
System.out.println("WARNING: unknown argument " + args[i] + ".");
}
if (execCommand == null) {
System.err.println("ERROR: You did not provide the command to execute your solution." +
" Please use -exec <command> for this.");
System.exit(1);
}
SnowCleaningVis vis = new SnowCleaningVis();
try {
int score = vis.runTest();
System.out.println("Score = " + score);
} catch (RuntimeException e) {
System.err.println("ERROR: Unexpected error while running your test case.");
e.printStackTrace();
SnowCleaningVis.stopSolution();
}
}
}
class ErrorStreamRedirector extends Thread {
public BufferedReader reader;
public ErrorStreamRedirector(InputStream is) {
reader = new BufferedReader(new InputStreamReader(is));
}
public void run() {
while (true) {
String s;
try {
s = reader.readLine();
} catch (Exception e) {
// e.printStackTrace();
return;
}
if (s == null) {
break;
}
System.out.println(s);
}
}
}