8000 windowed approach · jamesjarvis/adventofcode@4a88cfb · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a88cfb

Browse files
committed
windowed approach
1 parent 869aae8 commit 4a88cfb

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

01/main.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,21 @@ func main() {
3030

3131
func countLargerThanPrevious(vals []int) int {
3232
var count int
33-
var last int = vals[0]
34-
for i := 1; i < len(vals); i++ {
35-
if vals[i] <= last {
36-
last = vals[i]
37-
continue
33+
var topWindow int = vals[0] + vals[1] + vals[2]
34+
35+
// top window = A,B,C
36+
//
37+
// On each check
38+
// W2 = W1 - v[i-3] + v[i]
39+
// if W2 > W1 count++
40+
// W1 = W2
41+
42+
for i := 3; i < len(vals); i++ {
43+
bottomWindow := topWindow - vals[i-3] + vals[i]
44+
if bottomWindow > topWindow {
45+
count++
3846
}
39-
count++
40-
last = vals[i]
47+
topWindow = bottomWindow
4148
}
4249
return count
4350
}

0 commit comments

Comments
 (0)
0