-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemRequestManager.java
More file actions
131 lines (103 loc) · 4.44 KB
/
ItemRequestManager.java
File metadata and controls
131 lines (103 loc) · 4.44 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
package Drones;
import CommonUtils.BetterStack;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Manages everything regarding the requesting of items in our game.
* Will be integrated with the other drone classes.
*
* You may only use java.util.List, java.util.ArrayList, java.io.* and java.util.Scanner
* from the standard library. Any other containers used must be ones you created.
*/
public class ItemRequestManager implements ItemRequestManagerInterface {
/**
* Get the retrieval times as per the specifications
*
* @param filename file to read input from
* @return the list of times requests were filled and index of the original request, per the specifications
*/
@Override
public ArrayList<ItemRetrievalTimes> getRetrievalTimes(String filename) {
try {
// as all of the inputs are on the same line, it is actually more efficient to use scanner's nextInt since
// with BufferedReader you would have to read in the entire line (possibly 10m integers long) at once
Scanner scan = new Scanner(new FileReader(filename));
String[] in = scan.nextLine().split(" ");
int n = Integer.parseInt(in[0]);
int t = Integer.parseInt(in[1]);
int dronePos = t;
long time = scan.nextInt();
BetterStack<Pair<Integer, Integer>> targets = new BetterStack<>();
ArrayList<ItemRetrievalTimes> ans = new ArrayList<>();
// Start taking requests
long prevReqTime = time;
for (int i = 1; i < n; i++) {
int reqTime = scan.nextInt();
// Amount of time drone was allowed to work between requests. In that time...
long diff = reqTime - prevReqTime;
targets.push(new Pair<>(i - 1, 0));
long timeLeft = diff;
long tempTime = time;
while (true) {
if (targets.isEmpty()) break;
// Pop a target to deliver.
Pair<Integer, Integer> p = targets.pop();
int j = p.first;
int targetPos = p.second;
int fulfillTime = Math.abs(dronePos - targetPos) + (t - targetPos);
// The drone did not have enough time to finish the request; either it reached the item and
// dropped it at position `diff - dronePos` or it still needs to go to the item.
if (timeLeft < fulfillTime) {
int relDist = (int) (timeLeft - Math.abs(dronePos - targetPos));
targets.push(new Pair<>(p.first, Math.max(relDist, 0) + targetPos));
dronePos = Math.abs(relDist) + targetPos;
break;
}
// Otherwise, we can fulfill a request with time to spare.
// New drone pos: t
// New time left: timeLeft - fulfillTime
ans.add(new ItemRetrievalTimes(j, tempTime + fulfillTime));
timeLeft -= fulfillTime;
tempTime += fulfillTime;
dronePos = t;
}
time += diff;
prevReqTime = reqTime;
}
// Push the last target and run while targets remain.
targets.push(new Pair<>(n - 1, 0));
while (!targets.isEmpty()) {
Pair<Integer, Integer> p = targets.pop();
int j = p.first;
int targetPos = p.second;
int fulfillTime = Math.abs(dronePos - targetPos) + (t - targetPos);
ans.add(new ItemRetrievalTimes(j, time + fulfillTime));
time += fulfillTime;
dronePos = t;
}
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;
}
}
class Pair<T, E> {
public T first;
public E second;
Pair(T first, E second) {
this.first = first;
this.second = second;
}
@Override
public String toString() {
return "Pair{" +
"first=" + first +
", second=" + second +
'}';
}
}