From d77e2b954262d6a2299587cc83d572caa879b3de Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 8 Feb 2022 20:25:30 +0100 Subject: Week 151: Go solution for part 2 --- challenge-151/abigail/go/ch-2.go | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 challenge-151/abigail/go/ch-2.go 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]) + } +} -- cgit