-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.go
62 lines (53 loc) · 1.14 KB
/
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
62
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
)
// Read the input file.
func getInput() []int {
file, err := os.Open("2021/inputs/day01.txt")
if err != nil {
log.Fatalf("Failed to open file!")
}
defer file.Close()
scanner := bufio.NewScanner(file)
var returnSlice []int
for scanner.Scan() {
lineStr := scanner.Text()
num, _ := strconv.Atoi(lineStr)
returnSlice = append(returnSlice, num)
}
return returnSlice
}
// Count the number of times the values increase
func partOne(inputVal []int) int {
returnVal := 0
for i := 1; i < len(inputVal); i++ {
if inputVal[i] > inputVal[i-1] {
returnVal++
}
}
return returnVal
}
// Count the number of times the sum of a rolling window of three values increase
func partTwo(inputVal []int) int {
returnVal := 0
priorSum := 0
for i := 1; i < len(inputVal)-2; i++ {
sum := inputVal[i] + inputVal[i+1] + inputVal[i+2]
if sum > priorSum {
returnVal++
}
priorSum = sum
}
return returnVal
}
// Run both parts
func main() {
inputVal := getInput()
fmt.Printf("Part One answer is: %d \n", partOne(inputVal))
fmt.Printf("Part Two answer is: %d", partTwo(inputVal))
}