-
Notifications
You must be signed in to change notification settings - Fork 1
/
pe068.groovy
executable file
·48 lines (33 loc) · 1.09 KB
/
pe068.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
#!/usr/bin/env groovy
def _ticks = System.nanoTime()
/**********************************************************************
http://projecteuler.net/index.php?section=problems&id=68
http://keyzero.wordpress.com/2010/05/21/project-euler-problem-68/
*********************************************************************/
// NOTE: Requires Groovy 1.7+ for the eachPermutation() method
def SEQ = [ 0, 6, 7, 1, 7, 8, 2, 8, 9, 3, 9, 5, 4, 5, 6 ]
def answer = ""
(1..10).eachPermutation { nums ->
if (nums[0] > 6) return
for (i in 1..<5) {
if (nums[0] > nums[i]) return
}
for (i in 5..<10) {
if (nums[i] == 10) return
}
def sum = nums[0] + nums[6] + nums[7]
def p = 3
while (p < 15) {
if (sum != nums[SEQ[p]] + nums[SEQ[p+1]] + nums[SEQ[p+2]]) return
p += 3
}
def sb = new StringBuilder()
for (i in 0..<15) {
sb.append(nums[SEQ[i]])
}
answer = [ sb.toString(), answer ].max()
}
/********************************************************************/
_ticks = System.nanoTime() - _ticks
println "Answer: $answer"
println "Elapsed: ${_ticks / 1e6} ms"