-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDaySixteen.java
819 lines (687 loc) · 25.6 KB
/
DaySixteen.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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class DaySixteen {
public static void main(String[] args) throws IOException {
// "Valve HH has flow rate=22; tunnel leads to valve GG"
SolutionSixteen solution = new SolutionSixteen();
// solution.initVertexes();
solution.parseInput();
solution.findDistances();
// solution.printDistances();
//
// ValveNode currentNode = solution.start;
int remainingTime = 30;
Set<ValveNode> opened = new HashSet<>(solution.vertexes);
opened.removeIf(e -> e.getFlowRate() == 0);
int sumPressure = 0;
int currentFlowRate = 0;
// while (remainingTime > 0) {
// // find next node
// ValveNode next = solution.calcucate(
// currentNode, solution.findMaxFlowRateNode(opened), opened, remainingTime);
// System.out.println(next);
// // if it exists
// if (next != null) {
// int spentTime = currentNode.getDistances().get(next) + 1;
// if (spentTime > remainingTime) {
// sumPressure += remainingTime * currentFlowRate;
// break;
// }
// sumPressure += spentTime * currentFlowRate;
// remainingTime -= spentTime;
// currentFlowRate += next.getValue();
// currentNode = next;
// opened.remove(next);
// } else {
//// System.out.println(sumPressure);
// sumPressure += remainingTime * currentFlowRate;
// break;
// }
// System.out.println(remainingTime);
// }
// System.out.println(sumPressure + " " + currentFlowRate);
// ValveNode currentNode = solution.start;
// while (remainingTime > 0) {
// ValveNode next = solution.magicDady(currentNode, opened, remainingTime);
// if (next != null) {
// int spentTime = currentNode.getDistances().get(next) + 1;
// sumPressure += spentTime * currentFlowRate;
// remainingTime -= spentTime;
// currentFlowRate += next.getValue();
// currentNode = next;
// opened.remove(next);
// } else {
// sumPressure += remainingTime * currentFlowRate;
// break;
// }
// }
// System.out.println(sumPressure + " " + currentFlowRate);
List<ValveNode> nonZero = new ArrayList<>();
for (ValveNode vertex : solution.vertexes) {
if (vertex.getFlowRate() != 0) {
nonZero.add(vertex);
}
}
// System.out.println(solution.getMaxPressurePart1(solution.start, 0, nonZero));
List<ValveNode> closed = new LinkedList<>(nonZero);
// System.out.println(solution.findMaximumPressure(solution.start, 0, closed));
System.out.println(solution.findMaximumPressure2(
new Path(0, 0, solution.start, opened),
new HashMap<>()));
System.out.println(solution.findMaximumPressureWithElephant(
new Path(0, 0, solution.start, opened),
new Path(0, 0, solution.start, opened),
new HashMap<>()));
// a b c d e f g
// bc bd be bf bg
// cd ce cf cg
// de df dg
// ef eg
// fg
// ---- ok let's try bc ----
// de df dg
// ef eg
// fg
// ----- go de ----
// fg
//
// b c
// one to b one to c
//
// a (b c) (d e) (f g)
// a (b c) (d e) (g f)
// a (b c) (e d) (g f)
// a (b c) (e d) (f g)
// }
////
// public static void main(String[] args) {
//
//
// Map<String, ValveNode> map = new HashMap<>();
// String currentLine = "Valve EG has flow rate=21; tunnels lead to valves WZ, OF, ZP, QD";
// String[] split = currentLine.split("; ");
// String valveName = split[0].substring(6, 8);
// String flowRateString = split[0].substring(23);
// int flowRate = Integer.parseInt(flowRateString);
// String neighboursString = split[1].substring(23);
// String[] neighboursArrayString = neighboursString.split(", ");
//
//
//
}
}
class SolutionSixteen {
Set<ValveNode> vertexes = new HashSet<>();
ValveNode start;
String inputFileName = "test";
// String inputFileName = "input16"; // 1382 too low // 2087 right
Map<String, ValveNode> nodes = new HashMap<>();
int getMaxPressurePart1(ValveNode nodeAt, int minute, List<ValveNode> nonZeroValves) {
if (minute >= 30) {
return 0;
}
int maxFlow = 0;
Map<ValveNode, Integer> distances = nodeAt.getDistances();
for (int i = 0; i < nonZeroValves.size(); i++) {
ValveNode nonZeroValve = nonZeroValves.get(i);
if (nodeAt.equals(nonZeroValve)) {
continue;
}
int dist = distances.get(nonZeroValve);
int minAdj = minute + dist + 1;
nonZeroValves.remove(i);
int nodeFlow = ((30 - Math.min(minAdj, 30)) * nonZeroValve.getFlowRate());
int flow = nodeFlow + getMaxPressurePart1(nonZeroValve, minAdj, nonZeroValves);
nonZeroValves.add(i, nonZeroValve);
if(flow > maxFlow) {
maxFlow = flow;
}
}
return maxFlow;
}
ValveNode magicDady(final ValveNode node, Set<ValveNode> openedNodes, int remainingTime) {
Map<ValveNode, Integer> distances = node.getDistances();
ValveNode maxNode = null;
int maxPressure = 0;
for (ValveNode openedNode : openedNodes) {
if (remainingTime < distances.get(openedNode) + 1) {
continue;
}
int pressureOverEnd = (remainingTime - distances.get(openedNode) - 1) * openedNode.getFlowRate();
if (maxPressure < pressureOverEnd) {
maxPressure = pressureOverEnd;
maxNode = openedNode;
}
}
System.out.println(maxNode);
return maxNode;
}
void initVertexes(){
ValveNode AA = new ValveNode(0, "AA"); // start
start = AA;
ValveNode BB = new ValveNode(13, "BB");
ValveNode CC = new ValveNode(2, "CC");
ValveNode DD = new ValveNode(20, "DD");
ValveNode EE = new ValveNode(3, "EE");
ValveNode FF = new ValveNode(0, "FF");
ValveNode GG = new ValveNode(0, "GG");
ValveNode HH = new ValveNode(22, "HH");
ValveNode II = new ValveNode(0, "II");
ValveNode JJ = new ValveNode(21, "JJ");
vertexes.add(AA);
vertexes.add(BB);
vertexes.add(CC);
vertexes.add(DD);
vertexes.add(EE);
vertexes.add(FF);
vertexes.add(GG);
vertexes.add(HH);
vertexes.add(II);
vertexes.add(JJ);
AA.getNeighbours().add(BB);
AA.getNeighbours().add(DD);
AA.getNeighbours().add(II);
BB.getNeighbours().add(AA);
BB.getNeighbours().add(CC);
CC.getNeighbours().add(BB);
CC.getNeighbours().add(DD);
DD.getNeighbours().add(AA);
DD.getNeighbours().add(CC);
DD.getNeighbours().add(EE);
EE.getNeighbours().add(DD);
EE.getNeighbours().add(FF);
FF.getNeighbours().add(EE);
FF.getNeighbours().add(GG);
GG.getNeighbours().add(FF);
GG.getNeighbours().add(HH);
HH.getNeighbours().add(GG);
II.getNeighbours().add(AA);
II.getNeighbours().add(JJ);
JJ.getNeighbours().add(II);
}
/*
fill nodes
*/
void parseInput() throws IOException {
BufferedReader bf = new BufferedReader(new FileReader(inputFileName));
List<String> lines = bf.lines().toList();
// Map<String, ValveNode> nodes = new HashMap<>();
// add all nodes
for (String line : lines) {
String[] split = line.split("; ");
String valveName = split[0].substring(6, 8);
String flowRateString = split[0].substring(23);
int flowRate = Integer.parseInt(flowRateString);
nodes.put(valveName, new ValveNode(flowRate, valveName));
}
// add neighbours
for (String line : lines) {
String[] split = line.split("; ");
String valveName = split[0].substring(6, 8);
String[] neighboursArrayString;
if (split[1].contains(",")) {
String neighboursString = split[1].substring(23);
neighboursArrayString = neighboursString.split(", ");
} else {
String neighboursString = split[1].substring(22);
neighboursArrayString = new String[]{neighboursString};
}
ValveNode node = nodes.get(valveName);
for (String neighbour : neighboursArrayString) {
node.getNeighbours().add(nodes.get(neighbour));
}
}
vertexes.addAll(nodes.values());
start = nodes.get("AA");
}
void printGraph(ValveNode node, Set<ValveNode> visited) {
System.out.println(node);
visited.add(node);
for (ValveNode neighbour : node.getNeighbours()) {
if (visited.contains(neighbour)) {
continue;
}
printGraph(neighbour, visited);
}
}
ValveNode findMaxFlowRateNode(Set<ValveNode> vertexes) {
ValveNode maxFlowRateNode = new ValveNode(-1, "tmp");
for (ValveNode vertex : vertexes) {
if (vertex.getFlowRate() > maxFlowRateNode.getFlowRate()) {
maxFlowRateNode = vertex;
}
}
// System.out.println(maxFlowRateNode);
return maxFlowRateNode;
}
ValveNode calcucate(final ValveNode node, ValveNode maxFlowRateNode, Set<ValveNode> openedValveNodes,
int remainingTime) {
// (d_2 - d_1 + 1) * fR_1 < fR_2
// JJ 21 3
// BB 13 2
//
// boolean removed = openedValveNodes.removeIf(e -> node.getDistances().get(e) >= remainingTime);
// if (removed) {
// System.out.println("removed");
// }
if (openedValveNodes.isEmpty()) {
return null;
}
int distanceMaxFR = node.getDistances().get(maxFlowRateNode);
int maxFR = maxFlowRateNode.getFlowRate();
ValveNode maxFRNode = maxFlowRateNode;
ValveNode lastMaxFRNode = null;
Set<ValveNode> candidates = new HashSet<>(openedValveNodes);
candidates.remove(node);
candidates.remove(maxFlowRateNode);
candidates.removeIf(candidate -> candidate.getFlowRate() == 0);
// candidates.removeIf(candidate -> node.getDistances().get(candidate) >= remainingTime);
// if (candidates.isEmpty()) {
// return null;
// System.out.println("em");
// }
// while (lastMaxFRNode != maxFRNode) {
while (true) {
System.out.println(candidates);
for (ValveNode vertex : candidates) {
// if (node.getDistances().get(vertex) + 1 >= remainingTime) {
// System.out.println(node.getDistances().get(vertex) );
// System.out.println(vertex);
// System.out.println(remainingTime);
// System.out.println("vot tak vot");
// continue;
// }
int difference = (distanceMaxFR - node.getDistances().get(vertex) + 1) * vertex.getFlowRate();
// int difference = (remainingTime - node.getDistances().get(vertex) + 1) * vertex.getValue();
if (difference > maxFR) {
maxFRNode = vertex;
maxFR = difference;
} else if (difference == maxFR && !vertex.equals(lastMaxFRNode)) {
System.out.println("aaaaaaaaaaaaAAAAAAAAAA");
}
}
if (lastMaxFRNode != maxFRNode) {
lastMaxFRNode = maxFRNode;
distanceMaxFR = node.getDistances().get(maxFRNode);
maxFR = maxFRNode.getFlowRate();
} else {
break;
}
}
// System.out.println(maxFRNode);
return maxFRNode;
}
// void distanceDaddy(ValveNode node, Set<ValveNode> visited, Map<ValveNode, Integer> distances, Integer distance) {
// visited.add(node);
// System.out.println(node + " " + distance);
// for (ValveNode neighbour : node.getNeighbours()) {
// if (!distances.containsKey(node)) {
// distances.put(node, distance);
// } else {
// if (distance < distances.get(node)) {
// distances.put(node, distance);
// }
// }
// if (visited.contains(neighbour)) {
// continue;
// }
// distanceDaddy(neighbour, visited, distances, distance + 1);
// }
// System.out.println(distances);
// }
Map<ValveNode, Integer> dijkstra(ValveNode node) {
Map<ValveNode, Integer> distances = new HashMap<>();
for (ValveNode vertex : vertexes) {
if (vertex.equals(node)) {
distances.put(vertex, 0);
} else {
distances.put(vertex, Integer.MAX_VALUE - 2);
}
}
PriorityQueue<Map.Entry<ValveNode, Integer>> queue = new PriorityQueue<>(Map.Entry.comparingByValue());
queue.addAll(distances.entrySet());
while (!queue.isEmpty()) {
var vertex = queue.remove();
for (ValveNode neighbour : vertex.getKey().getNeighbours()) {
Integer distance = distances.get(vertex.getKey()) + 1;
if (distance < distances.get(neighbour)) {
queue.remove(Map.entry(neighbour, distances.get(neighbour)));
queue.add(Map.entry(neighbour, distance));
distances.put(neighbour, distance);
}
}
}
return distances;
}
void findDistances() {
for (ValveNode vertex : vertexes) {
vertex.setDistances(dijkstra(vertex));
}
}
void printDistances() {
for (ValveNode vertex : vertexes) {
System.out.println(vertex.getName() + " " + vertex.getDistances());
}
}
// void answer1(ValveNode node) {
// for (ValveNode neighbour : node.getNeighbours()) {
//
// Path path = new Path(1, 0, neighbour);
// if (neighbour.getFlowRate() == 0) {
//
// } else {
// path.getEnabled().add(neighbour);
// path.setMinutes(path.getMinutes() + 1);
// path.setPressure(path.getPressure() + neighbour.getFlowRate());
// }
// }
//
// int minutes = 0;
// int pressure = 0;
// Set<ValveNode> enabled = new HashSet<>();
// Iterator<ValveNode> iterator = node.getNeighbours().iterator();
// PriorityQueue<Path> paths = new PriorityQueue<>();
//
// ValveNode next = iterator.next();
// minutes += 1;
// if (next.getFlowRate() > 0) {
// // add two path
// Path enabledPath = new Path(minutes + 1, next.getFlowRate(), next);
// enabledPath.getEnabled().add(next);
//
// Path skippedPath = new Path(minutes, 0, next);
//
// }
//
//
// }
void brilliant() {
// current node
// pick the closest node with maximum value
// calculate minutes and pressure
// update current node
// save this path into priority queue
//
// fundamentals:
// ------------
// in begin this story we have only one path (AA)
// but later maybe more than one path (AA, AA-DD, AA-JJ, ...)
// each path have set of visited nodes and reference to current node
// and also have such parameters as current time and current pressure or flow rate
// we can store these paths into priority queue (maximize flow rate and minimize lost time)
// pick path from queue
// for current node find closest
// all what we need for solution:
// 1. map with minimal distances between all non-zero nodes
// 2. map with values (pressure) of all nodes
// 3. ???
// 4. PROFIT!!!
// p(t) = fR * t - N * (H + 1)
// fR = 33
// t = 6
// N = 2
// H = 3
// 33 * 6 - 2 * (3 + 1)
// p(t) =
// AA --1--> DD --2--> BB -> ...
// 0 * (1 + 1) = 0 ; fR = 20
// 20 * (2 + 1) = 60; fR = 20 + 13 = 33
// p = fR * (distance + 1)
// DD --3---> JJ
// 20 * (3 + 1) = 80; fR = 20 + 21 = 41
// BB --3--> JJ; fR = 20 + 13 + 21 = 54
// 33 * (3 + 1) = 132
// p(t) = sum(fR(t) * (distance + 1))
// t(0): fR = a;
// t(2): fR = b; (1+1) 20
// t(6): fR = c; (5+1) 22
// t(N): fR = X;
// t(distance + 1): fR > b
// !!!
// fR_X > fR_b * (distance_X - distance_b)
// from DD to JJ = 3
// fR_JJ = 21
// fR_BB = 13
// distance_JJ = 3
// distance_BB = 2
// t(0) -> t(7) sum = 22;
// t(3) -> t(7) sum = 20 * 4 = 80;
//
//
//
// currentFlowRate = 0
// candidateFlowRate = 22
// candidateDistance = 5 min
// anotherFlowRate = 20
// anotherDistance = 2 min
// 5 - 2 = 3
// 3 * 20 = 60 > 22
// 61 - 5 min
// 20 - 2 min
// 0 1 2 3 4 5 6
// 0 0 X 20 20 20 20
// 0 0 0 0 0 X 81
// 0 0 X 20 20 20 20 20! 101 101
// 0 0 0 0 0 X 81 81! 101 101
// fR_1 = 20; d_1 = 2
// fR_2 = 81; d_2 = 5
// (d_2 - d_1 + 1) * fR_1 < fR_2
// (5 - 2 + 1) * 20 < 81 ?
// 4 * 20 < 81
// 80 < 81 ; True!
// 5 - 2 + 1 = 4
// 4 * 20 = 80
// 1 * 81 = 81
// distances = [ 1, 2, 4, 6]
// fRates = [10, 13, 29, 14]
// find max fR: 29
// distance: 4
// for all other
// find
// (4 - distance + 1) * fR
// [40, 39, 29, -..]
// [1, 1, 2, 2, 3, 4, 5, 19]
// [3, 4, 12, 11, 4, ,...]
// if (distance > distance_R && fR < fR_R) skip;
// else if (==) ??;
}
int findMaximumPressure(ValveNode node, int minute, List<ValveNode> closedNodes) {
if (minute >= 30) {
return 0;
}
int maxPressure = 0;
for (int i = 0; i < closedNodes.size(); i++) {
// open node
ValveNode closedNode = closedNodes.remove(i);
int minuteAfterOpenNode = minute + node.getDistances().get(closedNode) + 1;
int pressure = (30 - Math.min(30, minuteAfterOpenNode)) * closedNode.getFlowRate()
+ findMaximumPressure(closedNode, minuteAfterOpenNode, closedNodes);
// close node
closedNodes.add(i, closedNode);
if (pressure > maxPressure) {
maxPressure = pressure;
}
}
return maxPressure;
}
int findMaximumPressure2(Path path, Map<Path, Integer> memo) {
if (path.getMinutes() >= 30) {
return 0;
}
if (memo.containsKey(path)) {
// if (memo.get(path) > 656) {
// System.out.println(path.getMinutes() + " " +
// path.getEnabled() + " " + memo.get(path));
// }
return memo.get(path);
}
int maxPressure = 0;
for (var closedNode : path.getEnabled()) {
int minuteAfterOpenNode = path.getMinutes() + path.getCurrentNode().getDistances().get(closedNode) + 1;
Set<ValveNode> closedNodes = new HashSet<>(path.getEnabled());
closedNodes.remove(closedNode);
Path newPath = new Path(minuteAfterOpenNode, maxPressure, closedNode, closedNodes);
int pressure = (30 - Math.min(30, minuteAfterOpenNode)) * closedNode.getFlowRate()
+ findMaximumPressure2(newPath, memo);
maxPressure = Math.max(pressure, maxPressure);
memo.put(path, maxPressure);
}
return maxPressure;
}
int findMaximumPressureWithElephant(Path myPath, Path elephantPath, Map<Path, Integer> memo) {
if (myPath.getMinutes() >= 26) {
return 0;
}
if (elephantPath.getMinutes() >= 26) {
return 0;
}
// if (memo.containsKey(myPath)) {
// return memo.get(myPath);
// } else if (memo.containsKey(elephantPath)) {
// return memo.get(elephantPath);
// }
int maxPressure = 0;
int maxPressure2 = 0;
for (var myNode : myPath.getEnabled()) {
if (!elephantPath.getEnabled().contains(myNode)) {
continue;
}
int minuteAfterOpenNode = myPath.getMinutes() + myPath.getCurrentNode().getDistances().get(myNode) + 1;
Set<ValveNode> closedNodes = new HashSet<>(myPath.getEnabled());
closedNodes.remove(myNode);
Path myNewPath = new Path(minuteAfterOpenNode, maxPressure, myNode, closedNodes);
int pressure = (26 - Math.min(26, minuteAfterOpenNode)) * myNode.getFlowRate()
+ findMaximumPressureWithElephant(myNewPath, elephantPath, memo);
maxPressure = Math.max(pressure, maxPressure);
memo.put(myPath, maxPressure);
}
for (var elephantNode : elephantPath.getEnabled()) {
if (!myPath.getEnabled().contains(elephantNode)) {
continue;
}
int minuteAfterOpenNode = elephantPath.getMinutes() + elephantPath.getCurrentNode().getDistances().get(elephantNode) + 1;
Set<ValveNode> closedNodes = new HashSet<>(elephantPath.getEnabled());
closedNodes.remove(elephantNode);
Path elephantNewPath = new Path(minuteAfterOpenNode, maxPressure, elephantNode, closedNodes);
int pressure = (26 - Math.min(26, minuteAfterOpenNode)) * elephantNode.getFlowRate()
+ findMaximumPressureWithElephant(myPath, elephantNewPath, memo);
maxPressure = Math.max(pressure, maxPressure);
memo.put(elephantPath, maxPressure);
}
return maxPressure;
}
// void f(closedNodes, openedNodes, minuteMy, minuteEl, )
// currentNode
// for available nodes :
// available.remove(node);
// opened.add(node);
// pressure = node * minute + dist + 1;
// find max
//
}
class ValveNode {
private Set<ValveNode> neighbours = new HashSet<>();
private int flowRate;
private String name;
private Map<ValveNode, Integer> distances;
public ValveNode(int value, String name) {
this.flowRate = value;
this.name = name;
}
public String getName() {
return name;
}
public void setDistances(Map<ValveNode, Integer> distances) {
this.distances = distances;
}
public Map<ValveNode, Integer> getDistances() {
return distances;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ValveNode valveNode = (ValveNode) o;
if (flowRate != valveNode.flowRate) return false;
return Objects.equals(name, valveNode.name);
}
@Override
public int hashCode() {
int result = flowRate;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
public int getFlowRate() {
return flowRate;
}
Set<ValveNode> getNeighbours() {
return neighbours;
}
@Override
public String toString() {
return "("+name + "," + flowRate +")" ;
}
}
class Path {
private int minutes;
private int pressure;
private Set<ValveNode> enabled;
private ValveNode currentNode;
private int flowRate;
public Path(int minutes, int pressure, ValveNode currentNode, Set<ValveNode> closedNodes) {
this.minutes = minutes;
this.pressure = pressure;
this.currentNode = currentNode;
this.enabled = closedNodes;
}
void update() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Path path = (Path) o;
if (minutes != path.minutes) return false;
if (!enabled.equals(path.enabled)) return false;
return currentNode.equals(path.currentNode);
}
@Override
public int hashCode() {
int result = minutes;
result = 31 * result + enabled.hashCode();
result = 31 * result + currentNode.hashCode();
return result;
}
public int getFlowRate() {
return flowRate;
}
public void setFlowRate(int flowRate) {
this.flowRate = flowRate;
}
public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
public int getPressure() {
return pressure;
}
public void setPressure(int pressure) {
this.pressure = pressure;
}
public Set<ValveNode> getEnabled() {
return enabled;
}
public void setEnabled(Set<ValveNode> enabled) {
this.enabled = enabled;
}
public ValveNode getCurrentNode() {
return currentNode;
}
public void setCurrentNode(ValveNode currentNode) {
this.currentNode = currentNode;
}
}