-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
85 lines (65 loc) · 1.24 KB
/
main.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
79
80
81
82
83
84
85
package main
import (
"fmt"
"strings"
"github.com/devkvlt/aoc"
)
var lines = aoc.Lines("input")
func isZero(s []int) bool {
for _, v := range s {
if v != 0 {
return false
}
}
return true
}
func diffs(s []int) []int {
diffs := make([]int, len(s)-1)
for i := 0; i < len(s)-1; i++ {
diffs[i] = s[i+1] - s[i]
}
return diffs
}
func part1() {
sum := 0
for _, line := range lines {
nums := strings.Fields(line)
history := []int{}
for _, num := range nums {
history = append(history, aoc.Atoi(num))
}
last := []int{history[len(history)-1]}
for !isZero(history) {
history = diffs(history)
last = append(last, history[len(history)-1])
}
for i := 0; i < len(last); i++ {
sum += last[i]
}
}
fmt.Println(sum)
}
func part2() {
sum := 0
for _, line := range lines {
nums := strings.Fields(line)
history := []int{}
for _, num := range nums {
history = append(history, aoc.Atoi(num))
}
first := []int{history[0]}
for !isZero(history) {
history = diffs(history)
first = append(first, history[0])
}
for i := 0; i < len(first); i++ {
e := 1 - (i%2)*2 // 1 if i is even, -1 otherwise
sum += e * first[i]
}
}
fmt.Println(sum)
}
func main() {
part1() // 1798691765
part2() // 1104
}