-
Notifications
You must be signed in to change notification settings - Fork 0
/
day1.go
78 lines (59 loc) · 1.12 KB
/
day1.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
partA()
partB()
}
func partA() {
file, err := os.Open("./input_day1.txt")
check(err)
defer file.Close()
scanner := bufio.NewScanner(file)
var row, increases, previous, next int
for scanner.Scan() {
if row == 0 {
previous, err = strconv.Atoi(scanner.Text())
} else {
next, err = strconv.Atoi(scanner.Text())
if next > previous {
increases++
}
}
row++
previous = next
}
fmt.Println("Increases part 1: ", increases)
}
func partB() {
file, err := os.Open("./input_day1.txt")
check(err)
defer file.Close()
scanner := bufio.NewScanner(file)
var row, tmp, previous, next, increases int
values := make([]int, 0)
for scanner.Scan() {
tmp, err = strconv.Atoi(scanner.Text())
values = append(values, tmp)
}
for row < len(values) {
if row > 2 {
previous = values[row-3] + values[row-2] + values[row-1]
next = values[row-2] + values[row-1] + values[row]
if next > previous {
increases++
}
}
row++
}
fmt.Println("Increases part 2: ", increases)
}