-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCleanSwordManager.java
More file actions
64 lines (52 loc) · 2.19 KB
/
CleanSwordManager.java
File metadata and controls
64 lines (52 loc) · 2.19 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
package Drones;
import CommonUtils.BetterQueue;
import java.io.*;
import java.util.ArrayList;
/**
* Manages everything regarding the cleaning of swords in our game.
* Will be integrated with the other drone classes.
*
* You may only use java.util.List, java.util.ArrayList, and java.io.* from
* the standard library. Any other containers used must be ones you created.
*/
public class CleanSwordManager implements CleanSwordManagerInterface {
/**
* Gets the cleaning times per the specifications.
*
* @param filename file to read input from
* @return the list of times requests were filled and times it took to fill them, as per the specifications
*/
@Override
public ArrayList<CleanSwordTimes> getCleaningTimes(String filename) {
try {
BufferedReader bf = new BufferedReader(new FileReader(filename));
String[] in = bf.readLine().split(" ");
int n = Integer.parseInt(in[0]);
int m = Integer.parseInt(in[1]);
int t = Integer.parseInt(in[2]);
BetterQueue<Long> times = new BetterQueue<>();
long time = 0;
// Push the timestamp each sword is finished cleaning to the queue
for (int i = 0; i < n; i++) {
time += Long.parseLong(bf.readLine());
times.add(time);
}
ArrayList<CleanSwordTimes> ans = new ArrayList<>();
// Take sword-cleaning requests. Each request adds a new sword to the queue that's ready in
// `t` time after the request.
for (int i = 0; i < m; i++) {
long reqTime = Long.parseLong(bf.readLine());
time = Math.max(time, reqTime) + t;
times.add(time);
long nextSwordTime = Math.max(times.remove(), reqTime);
ans.add(new CleanSwordTimes(nextSwordTime, nextSwordTime - reqTime));
}
return ans;
} catch (IOException e) {
// This should never happen... uh oh o.o
System.err.println("ATTENTION TAs: Couldn't find test file: \"" + filename + "\":: " + e.getMessage());
System.exit(1);
}
return null;
}
}