diff options
author | Julien Dessaux | 2021-12-09 21:59:22 +0100 |
---|---|---|
committer | Julien Dessaux | 2021-12-09 21:59:22 +0100 |
commit | f15c44dc186eedbe248127797b6d42fbfcaa9589 (patch) | |
tree | fa938a66a25edd6a7913449395257e1b685a420f /2021/09/first.go | |
parent | Added solutions for 8th day : the seven segments code to guess (diff) | |
download | advent-of-code-f15c44dc186eedbe248127797b6d42fbfcaa9589.tar.gz advent-of-code-f15c44dc186eedbe248127797b6d42fbfcaa9589.tar.bz2 advent-of-code-f15c44dc186eedbe248127797b6d42fbfcaa9589.zip |
Added solutions for 9th day : minimums and basins in height map
Diffstat (limited to '')
-rw-r--r-- | 2021/09/first.go | 69 |
1 files changed, 69 insertions, 0 deletions
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) +} |