From f15c44dc186eedbe248127797b6d42fbfcaa9589 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 9 Dec 2021 21:59:22 +0100 Subject: Added solutions for 9th day : minimums and basins in height map --- 2021/09/first.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 2021/09/first.go (limited to '2021/09/first.go') diff --git a/2021/09/first.go b/2021/09/first.go new file mode 100644 index 0000000..edfca87 --- /dev/null +++ b/2021/09/first.go @@ -0,0 +1,69 @@ +package main + +import ( + "bufio" + "fmt" + "os" +) + +// DOES NOT WORK!!! + +func countMins(prev []byte, prevS []bool) (score int) { + for i, v := range prevS { + if v { + fmt.Println(prev[i]) + score += int(prev[i]) + 1 + } + } + return +} + +func main() { + score := 0 + + s := bufio.NewScanner(os.Stdin) + s.Scan() + width := len(s.Text()) + prevS := make([]bool, width) + prev := make([]byte, width) + for i, ch := range s.Text() { + prev[i] = byte(ch) - '0' + if i == 0 { + prevS[i] = true + } else if prev[i] < prev[i-1] { + prevS[i-1] = false + prevS[i] = true + } + } + + curS := make([]bool, width) + cur := make([]byte, width) + fmt.Println(s.Text()) + for s.Scan() { + for i, ch := range s.Text() { + cur[i] = byte(ch) - '0' + if i > 0 && cur[i] < cur[i-1] { + curS[i-1] = false + if cur[i] < prev[i] { + prevS[i] = false + curS[i] = true + } else { + curS[i] = false + } + } else { + if cur[i] < prev[i] { + prevS[i] = false + } + curS[i] = false + } + } + fmt.Printf("%+v\n", prevS) + fmt.Println(s.Text()) + score += countMins(prev, prevS) + copy(prev, cur) + copy(prevS, curS) + } + score += countMins(prev, prevS) + fmt.Printf("%+v\n", prevS) + fmt.Println(score) +} -- cgit v1.2.3