-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunningSumsStream.groovy
82 lines (67 loc) · 2.61 KB
/
RunningSumsStream.groovy
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
import java.util.concurrent.*
class RunningSumsStreamAction extends RecursiveAction {
String stream
def results = []
Integer numberStreamLength
RunningSumsStreamAction(String stream) {
this.stream = stream
this.numberStreamLength = stream.length()
}
protected void compute() {
def matcher = (stream =~ /[1-8]+9/)
if (matcher.size() < 2) {
parseRunningSums()
println "Chunk: ${stream} --- ${results}"
} else {
matcher.reset()
matcher.find()
RunningSumsStreamAction sums1 = new RunningSumsStreamAction(stream.substring(matcher.start(), matcher.end()))
RunningSumsStreamAction sums2 = new RunningSumsStreamAction(stream.substring(matcher.end()))
invokeAll(sums1, sums2)
results += sums1.results + sums2.results
}
}
def parseRunningSums() {
Integer sumTargetIdx = numberStreamLength - 1
while (sumTargetIdx >= 2) {
Integer runningTotal = Integer.parseInt(stream[sumTargetIdx-1])
Integer targetTotal = Integer.parseInt(stream[sumTargetIdx])
Integer currentIdx = sumTargetIdx - 2
while (runningTotal != targetTotal && runningTotal < targetTotal) {
runningTotal += Integer.parseInt(stream[currentIdx])
currentIdx--
}
if (runningTotal == targetTotal && (sumTargetIdx - currentIdx > 2)) {
results << stream.substring(currentIdx+1, sumTargetIdx+1)
}
sumTargetIdx--
}
results = results.reverse()
}
}
import java.util.Random
class RandomSumsStreamGenerator {
static generateStream(Integer size) {
def generator = new Random()
def stream = ""
size.times {
stream += (generator.nextInt(9)+1).toString()
}
stream
}
}
private void runConcurrent(String stream) {
println stream
println "Available processors: ${Runtime.getRuntime().availableProcessors()}";
ForkJoinPool pool = new ForkJoinPool();
RunningSumsStreamAction sums = new RunningSumsStreamAction(stream)
Long start = System.currentTimeMillis()
pool.invoke(sums)
Long end = System.currentTimeMillis()
println sums.results
println "Execution time: ${end - start} ms\n\n"
}
runConcurrent(RandomSumsStreamGenerator.generateStream(50))
runConcurrent("8745648184845171326578518184151512461752149647129746915414816354846454")
runConcurrent("1239632237176453697341812369")
runConcurrent("12345678987654321")