-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdventOfCode2018Day07.java
297 lines (275 loc) · 11.7 KB
/
AdventOfCode2018Day07.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
import java.util.*;
/**
* Similarly to the topological ordering algorithm,
* (1) find all verticies with no predecessors and add them to the priority queue called pq,
* (2) poll a vertex from pq and put it on the output, after that you delete that vertex from the available ones
* (3) repeat
*
* Also, there will be vertices in the graph that do not have successors, I called them leaves here.
* Just add those leaves at the end to the output list in the alphabetical order.
*/
class Job implements Comparable<Job> {
Character c;
int time;
Job() {}
Job(Character c, int time) {
this.c = c;
this.time = time;
}
public int compareTo(Job j) {
return c.compareTo(j.c);
}
public String toString() {
return "" + c;
}
}
public class AdventOfCode2018Day07 {
private static void findVerticesWithNoPredecessors(TreeMap<Character, LinkedList<Character>> tm, PriorityQueue<Character> pq) {
Collection<LinkedList<Character>> values = tm.values();
ArrayList<Character> withPredecessors = new ArrayList<>();
for (LinkedList<Character> v : values) {
for (Character c : v) {
if (!withPredecessors.contains(c))
withPredecessors.add(c);
}
}
// find the vertices without predecessors and push them on the priority queue
Set<Character> keys = tm.keySet();
for (Character key : keys) {
if (!withPredecessors.contains(key) && !pq.contains(key))
pq.add(key);
}
}
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
// create a linked list version to store a graph
TreeMap<Character, LinkedList<Character>> tm = new TreeMap<>(); // for the first part
TreeMap<Character, LinkedList<Character>> tm2 = new TreeMap<>(); // for the second part
ArrayList<Character> withPredecessors = new ArrayList<>();
while (true) {
String[] line = s.nextLine().split(" ");
if (line.length < 2) break;
Character start = line[1].charAt(0);
Character end = line[7].charAt(0);
// find all nodes that have predecessors
if (!withPredecessors.contains(end))
withPredecessors.add(end);
if (!tm.containsKey(start)) {
tm.put(start, new LinkedList<Character>());
tm2.put(start, new LinkedList<Character>());
}
tm.get(start).add(end);
tm2.get(start).add(end);
}
s.close();
// find all leaves in this graph (aka tree)
Collection<LinkedList<Character>> values = tm.values();
Set<Character> keys = tm.keySet();
PriorityQueue<Character> leaves = new PriorityQueue<>(); // for the first part
PriorityQueue<Character> leaves2 = new PriorityQueue<>(); // for the second part
for (LinkedList<Character> v : values) {
for (Character c : v) {
if (!keys.contains(c) && !leaves.contains(c)) {
leaves.add(c);
leaves2.add(c);
}
}
}
// use topological order algorithm to solve the first part
ArrayList<Character> topological = new ArrayList<>();
PriorityQueue<Character> pq = new PriorityQueue<>();
// push all vertices without predecessor on the stack
findVerticesWithNoPredecessors(tm, pq);
while (pq.size() != 0) {
Character c = pq.poll();
// add c to the topologically ordered list
topological.add(c);
// do not forget to remove it from the treemap to recognize that we have already visited this vertex
tm.remove(c);
findVerticesWithNoPredecessors(tm, pq);
}
// add all leaves to the topologically ordered list
while (leaves.size() != 0) {
topological.add(leaves.poll());
}
System.out.print("Answer to the first part: ");
for (Character t : topological) {
System.out.print(t);
}
System.out.println();
System.out.print("Answer to the second part: ");
// here comes the second part
int w = 5;
int freeWorkers = w;
int currentTime = 0;
ArrayList<Job> jobs = new ArrayList<>();
pq = new PriorityQueue<Character>();
// push all vertices without predecessor on the stack
findVerticesWithNoPredecessors(tm2, pq);
while (pq.size() != 0 || jobs.size() > 0) {
// add new jobs if we have work to do and free workers
while (freeWorkers > 0 && pq.size() != 0) {
Character c = pq.poll();
jobs.add(new Job(c, 60 + c - 'A' + 1));
freeWorkers--;
}
// do the jobs until we have at least one of them done
boolean done = false;
ArrayList<Job> doneJobs = new ArrayList<>();
while (!done) {
currentTime++;
for (Job j : jobs) {
j.time--;
if (j.time == 0) { // the job is done
doneJobs.add(j);
done = true;
System.out.print(j.c);
tm2.remove(j.c);
freeWorkers++;
}
}
}
// delete finished jobs from the jobs array list
for (Job j : doneJobs) {
jobs.remove(j);
}
findVerticesWithNoPredecessors(tm2, pq);
// remove the jobs from pq that are already running
PriorityQueue<Character> temp = new PriorityQueue<>();
for (Character c : pq) {
boolean running = false;
for (Job j : jobs) {
if (j.c == c) {
running = true;
break;
}
}
if (!running) temp.add(c);
}
pq = temp;
// maybe it is time to add the leaves
// add only those leaves that are not dependent on other jobs
for (Character c : leaves2) {
Collection<LinkedList<Character>> vals = tm2.values();
boolean leafFound = false;
for (LinkedList<Character> v : vals) {
if (v.contains(c)) {
leafFound = true;
break;
}
}
if (!leafFound) {
pq.add(c);
}
}
// delete the leaves that we have just added to the priority queue
for (Character c : pq) {
if (leaves2.contains(c)) leaves2.remove(c);
}
}
System.out.println(" " + (currentTime));
}
}
/*
Step A must be finished before step R can begin.
Step J must be finished before step B can begin.
Step D must be finished before step B can begin.
Step X must be finished before step Z can begin.
Step H must be finished before step M can begin.
Step B must be finished before step F can begin.
Step Q must be finished before step I can begin.
Step U must be finished before step O can begin.
Step T must be finished before step W can begin.
Step V must be finished before step S can begin.
Step N must be finished before step P can begin.
Step P must be finished before step O can begin.
Step E must be finished before step C can begin.
Step F must be finished before step O can begin.
Step G must be finished before step I can begin.
Step Y must be finished before step Z can begin.
Step M must be finished before step K can begin.
Step C must be finished before step W can begin.
Step L must be finished before step W can begin.
Step W must be finished before step S can begin.
Step Z must be finished before step O can begin.
Step K must be finished before step S can begin.
Step S must be finished before step R can begin.
Step R must be finished before step I can begin.
Step O must be finished before step I can begin.
Step A must be finished before step Q can begin.
Step Z must be finished before step R can begin.
Step T must be finished before step R can begin.
Step M must be finished before step O can begin.
Step Q must be finished before step Z can begin.
Step V must be finished before step C can begin.
Step Y must be finished before step W can begin.
Step N must be finished before step F can begin.
Step J must be finished before step D can begin.
Step D must be finished before step N can begin.
Step B must be finished before step M can begin.
Step P must be finished before step I can begin.
Step W must be finished before step Z can begin.
Step Q must be finished before step V can begin.
Step V must be finished before step K can begin.
Step B must be finished before step Z can begin.
Step M must be finished before step I can begin.
Step G must be finished before step C can begin.
Step K must be finished before step O can begin.
Step E must be finished before step O can begin.
Step C must be finished before step I can begin.
Step X must be finished before step G can begin.
Step B must be finished before step T can begin.
Step B must be finished before step I can begin.
Step E must be finished before step F can begin.
Step N must be finished before step K can begin.
Step D must be finished before step W can begin.
Step R must be finished before step O can begin.
Step V must be finished before step I can begin.
Step T must be finished before step O can begin.
Step B must be finished before step Q can begin.
Step T must be finished before step L can begin.
Step M must be finished before step C can begin.
Step A must be finished before step M can begin.
Step F must be finished before step L can begin.
Step X must be finished before step T can begin.
Step G must be finished before step K can begin.
Step C must be finished before step L can begin.
Step D must be finished before step Z can begin.
Step H must be finished before step L can begin.
Step P must be finished before step Z can begin.
Step A must be finished before step V can begin.
Step G must be finished before step R can begin.
Step E must be finished before step G can begin.
Step D must be finished before step P can begin.
Step X must be finished before step L can begin.
Step U must be finished before step C can begin.
Step Z must be finished before step K can begin.
Step E must be finished before step W can begin.
Step B must be finished before step Y can begin.
Step J must be finished before step I can begin.
Step U must be finished before step P can begin.
Step Y must be finished before step L can begin.
Step N must be finished before step L can begin.
Step L must be finished before step S can begin.
Step H must be finished before step P can begin.
Step P must be finished before step S can begin.
Step J must be finished before step S can begin.
Step J must be finished before step U can begin.
Step H must be finished before step T can begin.
Step L must be finished before step I can begin.
Step N must be finished before step Z can begin.
Step A must be finished before step G can begin.
Step H must be finished before step S can begin.
Step S must be finished before step I can begin.
Step H must be finished before step E can begin.
Step W must be finished before step R can begin.
Step B must be finished before step G can begin.
Step U must be finished before step Y can begin.
Step J must be finished before step G can begin.
Step M must be finished before step L can begin.
Step G must be finished before step Z can begin.
Step N must be finished before step W can begin.
Step D must be finished before step E can begin.
Step A must be finished before step W can begin.
Step G must be finished before step Y can begin.
*/