From 0fb16f2822ded19a23b5b9f1a5b1e9e6dd3a89e3 Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Wed, 1 Dec 2021 16:44:15 +0100 Subject: [PATCH] 01: part2 --- 1/part2.go | 21 +++++++++++++++++---- 1/testinput | 10 ++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 1/testinput diff --git a/1/part2.go b/1/part2.go index 989a486..2bfd29c 100644 --- a/1/part2.go +++ b/1/part2.go @@ -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) diff --git a/1/testinput b/1/testinput new file mode 100644 index 0000000..167e291 --- /dev/null +++ b/1/testinput @@ -0,0 +1,10 @@ +199 +200 +208 +210 +200 +207 +240 +269 +260 +263