-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathb.go
43 lines (34 loc) · 752 Bytes
/
b.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package day01
import (
"strconv"
"strings"
)
type PuzzleB struct{}
func (p PuzzleB) String() string {
return "01b"
}
func (p PuzzleB) Run() int {
depths := strings.Split(input, "\n")
w := [4]int{}
w[0], _ = strconv.Atoi(depths[0])
w[1], _ = strconv.Atoi(depths[1])
w[2], _ = strconv.Atoi(depths[2])
w[3], _ = strconv.Atoi(depths[3])
// Only need to compare the first and last numbers, as each sliding window
// will share 2 common values:
//
// [199, 200, 208, 210] -> A: [199, 299, 208] B: [200, 208, 210]
// 210 > 199 ++(increased)
inc := 0
for i := 4; i < len(depths)-1; i++ {
if w[3] > w[0] {
inc++
}
// Shift window
w[0] = w[1]
w[1] = w[2]
w[2] = w[3]
w[3], _ = strconv.Atoi(depths[i+1])
}
return inc
}