-
Notifications
You must be signed in to change notification settings - Fork 0
/
fountain.go
executable file
·50 lines (42 loc) · 1.39 KB
/
fountain.go
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
package main
import (
"fmt"
"math"
)
func fountain(array []int, index int, mostRecent int, covered int) float64{
fmt.Printf("At index: %d and Most Recent: %d\n", index, mostRecent)
if index >= len(array){
fmt.Printf("Returning Inf\n")
return math.Inf(-8)
}
if index == len(array)-1{
if mostRecent == index{
if covered + 1 == len(array){
fmt.Println("Returning 1")
return 1
} else{
fmt.Println("Returning Infinite")
return math.Inf(-8)
}
} else if mostRecent < index{
if covered + int(math.Min(float64(index - mostRecent), float64(array[index]))) + array[index] + 1 == len(array){
fmt.Println("Node Covered: ", covered + int(math.Min(float64(index - mostRecent), float64(array[index]))) + array[index] + 1)
return 1
} else{
fmt.Println("Returning Infinite")
return math.Inf(-8)
}
}
fmt.Println("Returning Infinite")
return math.Inf(-8)
}
// Calculating covered = backwards + forwards
newCovered := covered + int(math.Min(float64(index - mostRecent), float64(array[index]))) + array[index] + 1
fmt.Println("Node Covered: ", newCovered)
result := math.Min( 1 + fountain(array, index+array[index]+1, index+array[index]+1, newCovered), fountain(array, index+1, index, covered) )
fmt.Println("Result: ",result)
return result
}
func main(){
fmt.Println(fountain([]int{4,1,0,0,0,5}, 0, 0, 0))
}