-
Notifications
You must be signed in to change notification settings - Fork 0
/
Challenge.java
107 lines (92 loc) · 2.73 KB
/
Challenge.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
import java.util.*;
class Main {
public static void main (String[] args) {
Scheduler test = new Scheduler();
test.parseInput();
test.run();
}
}
class Scheduler {
private Queue<Job> _pq;
private List<Worker> _workers;
public Scheduler () {
JobComparator comp = new JobComparator();
this._pq = new PriorityQueue<Job>(10, comp);
this._workers = new ArrayList<Worker>();
}
public void run() {
int currentTime = 0;
while(!_pq.isEmpty()) {
Job j = _pq.poll();
//fast forward if necessary
if (j.canBeginTime > currentTime) currentTime = j.canBeginTime;
//Assign each worker to the current job at the same time
for (Worker w : _workers)
System.out.println(currentTime + " " + w.name + " " + j.name);
// Calculate time it takes to complete with all bots on
int tasksPerBot = (j.tasks % _workers.size() == 0) ? j.tasks / _workers.size() : j.tasks / _workers.size() + 1;
int timeToComplete = tasksPerBot * j.taskTime;
currentTime += timeToComplete;
}
}
public void parseInput() {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) {
if (!in.hasNext()) break;
String nextLine = in.next();
if (nextLine.equals("job")) {
String name = in.next();
int tasks = in.nextInt();
int taskTime = in.nextInt();
int canBeginTime = in.nextInt();
int priority = in.nextInt();
_pq.add(new Job(name, tasks, taskTime, canBeginTime, priority));
} else if (nextLine.equals("worker")) {
String name = in.next();
_workers.add(new Worker(name));
}
}
}
public void printJobs() {
while(!_pq.isEmpty()) {
Job j = _pq.poll();
System.out.println("name: "+ j.name);
System.out.println("tasks: "+ j.tasks);
System.out.println("taskTime: "+ j.taskTime);
System.out.println("canBeginTime: "+ j.canBeginTime);
System.out.println("priority: "+ j.priority);
}
}
public void printWorkers() {
for (Worker w : _workers) {
System.out.println(w.name);
}
}
}
class Job {
public String name;
public int tasks;
public int taskTime;
public int canBeginTime;
public int priority;
public Job (String name, int tasks, int taskTime, int canBeginTime, int priority) {
this.name = name;
this.tasks = tasks;
this.taskTime = taskTime;
this.canBeginTime = canBeginTime;
this.priority = priority;
}
}
class Worker {
public String name;
public Worker (String name) {
this.name = name;
}
}
class JobComparator implements Comparator<Job> {
@Override
public int compare(Job x, Job y) {
// We're going to make this the lowest start time for now
return x.canBeginTime - y.canBeginTime;
}
}