diff options
| -rw-r--r-- | challenge-151/abigail/go/ch-2.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/challenge-151/abigail/go/ch-2.go b/challenge-151/abigail/go/ch-2.go new file mode 100644 index 0000000000..a8b6725ffa --- /dev/null +++ b/challenge-151/abigail/go/ch-2.go @@ -0,0 +1,48 @@ +package main + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +// + +// +// Run as: go run ch-2.go < input-file +// + +import ( + "fmt" + "bufio" + "os" + "strconv" + "strings" +) + +func max (a int, b int) int { + if a < b { + return b + } + return a +} + +func main () { + var reader = bufio . NewReader (os. Stdin) + for { + var text, err = reader . ReadString ('\n') + if (err != nil) { + break + } + + s := strings . Fields (strings . Trim (text, "\n")) + h := make ([] int, len (s)) + for i, v := range s { + n, _ := strconv . Atoi (v) + h [i] = n + } + h = append (h, 0, 0) + + for i := len (h) - 3; i >= 2; i -- { + h [i] = max (h [i] + h [i + 2], h [i + 1]) + } + + fmt . Println (h [0] + h [2]) + } +} |
