-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolutionJava8
99 lines (77 loc) · 3.25 KB
/
SolutionJava8
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
package com.playground.java;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SolutionJava8 {
public static void main(String[] args) {
// int[] capacities = new int[3];
// capacities[0] = 6;
// capacities[1] = 9;
// capacities[2] = 20;
// int targetVolume = 44;
// dumpDeliveryOptions(capacities, targetVolume);
try (Stream<String> stream = Files.lines(Paths.get("src/test/resources/input.txt"))) {
stream.forEach(ln -> {
List<Integer> values = Arrays.stream((ln.split(", ")[0].replaceAll("[()]","").split(","))).map(Integer::parseInt).collect(Collectors.toList());
int targetVolume = Integer.parseInt(ln.split(", ")[1]);
dumpDeliveryOptions(values.stream()
.mapToInt(Integer::intValue)
.toArray(), targetVolume);
});
} catch (IOException e) {
e.printStackTrace();
}
}
static void dumpDeliveryOptions(int[] capacities, int targetVolume) {
int tankerCount = capacities.length;
int[] tankers = new int[tankerCount];
StringBuilder outputBuffer = new StringBuilder();
final BinaryOperator<Integer> dumps[] = new BinaryOperator[1];
dumps[0] = (index, volume) -> {
// Last option, evenly divisible.
if (index == (tankerCount - 1)) {
if ((volume % capacities[index]) == 0) {
tankers[index] = (volume / capacities[index]);
dumps[0].apply((index + 1), 0);
}
}
// Done.
else if (index == tankerCount) {
outputBuffer.append("[" + tankers[0]);
for (int i = 1; i < tankerCount; ++i) {
outputBuffer.append("," + tankers[i]);
}
outputBuffer.append("]");
}
// Try next arrangements.
else
for (int i = 0; i <= (volume / capacities[index]); ++i) {
tankers[index] = i;
dumps[0].apply((index + 1), (volume - (i * capacities[index])));
}
return 0;
};
Predicate<Integer> canDeliver = (volume) -> {
dumps[0].apply(0, volume);
// Dumped some results to the output stream buffer.
return (outputBuffer.length() > 0);
};
// Options exist as is.
if (canDeliver.test(targetVolume)) {
System.out.println(outputBuffer);
}
// Inflate target oil volume until delivery options become possible.
else
for (int volume = targetVolume;;)
if (canDeliver.test(++volume)) {
System.out.println(volume - targetVolume);
break;
}
}
}