01: part2

This commit is contained in:
ptrcnull 2021-12-01 16:44:15 +01:00
parent 4779e181a2
commit 0fb16f2822
2 changed files with 27 additions and 4 deletions

View file

@ -3,22 +3,35 @@ package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
)
func main() {
count := 0
lastValue := math.MaxInt64
windows := [4]int{0, 0, 0, 0}
i := 1
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
currentWindow := i % 4
nextWindow := (i + 1) % 4
value, _ := strconv.Atoi(s.Text())
if value > lastValue {
for j := 0; j < 4; j++ {
if j != currentWindow {
windows[j] += value
}
}
//fmt.Println(value, windows, currentWindow, nextWindow, windows[nextWindow], windows[currentWindow])
// skip the first 3 values to populate the windows
if i > 3 && windows[nextWindow] > windows[currentWindow] {
count++
}
lastValue = value
windows[currentWindow] = 0
i++
}
fmt.Println(count)

10
1/testinput Normal file
View file

@ -0,0 +1,10 @@
199
200
208
210
200
207
240
269
260
263