aoc21/d1/d1.go

44 lines
863 B
Go
Raw Normal View History

2022-02-07 21:59:19 +02:00
package d1
import (
"strconv"
"strings"
)
func D1(scans string) int64 {
depths := strings.Split(strings.TrimSpace(scans), "\n")
downs := 0
for i := 1; i < len(depths); i++ {
next, _ := strconv.Atoi(strings.TrimSpace(depths[i]))
prev, _ := strconv.Atoi(strings.TrimSpace(depths[i-1]))
if next > prev {
downs++
}
}
return int64(downs)
}
2022-02-08 20:32:54 +02:00
func D1P2(scans string) int64 {
depths := strings.Split(strings.TrimSpace(scans), "\n")
downs := 0
for i := 3; i < len(depths); i++ {
fourth, _ := strconv.Atoi(strings.TrimSpace(depths[i]))
third, _ := strconv.Atoi(strings.TrimSpace(depths[i-1]))
second, _ := strconv.Atoi(strings.TrimSpace(depths[i-2]))
first, _ := strconv.Atoi(strings.TrimSpace(depths[i-3]))
prev := first + second + third
next := second + third + fourth
if next > prev {
downs++
}
}
return int64(downs)
}