/
pe112.groovy
42 lines (34 loc) · 1023 Bytes
/
pe112.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
#!/usr/bin/env groovy
def _ticks = System.nanoTime()
/**********************************************************************
http://projecteuler.net/index.php?section=problems&id=112
http://keyzero.wordpress.com/2010/09/05/project-euler-problem-112/
*********************************************************************/
def (answer, b) = [ 100, 0 ]
def isBouncy(n) {
def s = n.toString()
def direction = 0
def len = s.length()
for (i in 1..<len) {
if (direction == 1 && (s[i] < s[i-1])) return true
if (direction == -1 && (s[i] > s[i-1])) return true
if (direction == 0) {
if (s[i] > s[i-1]) {
direction = 1
}
else if (s[i] < s[i-1]) {
direction = -1
}
}
}
return false
}
while (true) {
if (isBouncy(answer)) b++
if (answer * 0.99 == b) break
answer++
}
/********************************************************************/
_ticks = System.nanoTime() - _ticks
println "Answer: $answer"
println "Elapsed time: ${_ticks / 1e6} ms"