diff options
| author | Abigail <abigail@abigail.freedom.nl> | 2022-02-08 20:25:30 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.freedom.nl> | 2022-02-08 20:25:30 +0100 |
| commit | d77e2b954262d6a2299587cc83d572caa879b3de (patch) | |
| tree | 5aca25028463ff9dbb07feb84f57b74ab8f5af16 | |
| parent | 922b81eeb7516829d0311fd3fb5b9fa51fe48876 (diff) | |
| download | perlweeklychallenge-club-d77e2b954262d6a2299587cc83d572caa879b3de.tar.gz perlweeklychallenge-club-d77e2b954262d6a2299587cc83d572caa879b3de.tar.bz2 perlweeklychallenge-club-d77e2b954262d6a2299587cc83d572caa879b3de.zip | |
Week 151: Go solution for part 2
| -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]) + } +} |
