-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.go
61 lines (54 loc) · 922 Bytes
/
day01.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
)
func main() {
file, err := os.Open("day01\\01.in")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
first := true
last := 0
counter := 0
var slider []int
for scanner.Scan() {
value, _ := strconv.Atoi(scanner.Text())
if !first {
if value > last {
counter++
}
} else {
first = false
}
last = value
slider = append(slider, last)
}
var windows []int
for i := range slider {
if i < len(slider)-2 {
sum := slider[i] + slider[i+1] + slider[i+2]
windows = append(windows, sum)
}
}
counter2 := 0
last = 0
for i, v := range windows {
if i != 0 {
if v > last {
counter2++
}
}
last = v
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
fmt.Println(counter)
fmt.Println(counter2)
}