generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
55 lines (44 loc) · 912 Bytes
/
solution.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
package day09p1
import (
"io"
"strconv"
"strings"
"aoc/utils"
)
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
sum := 0
for _, ln := range lines {
parts := strings.Fields(ln)
values := []int{}
for _, p := range parts {
v, err := strconv.Atoi(p)
utils.Check(err, "Unable to convert %s to int", p)
values = append(values, v)
}
// Start differentiating
valueRows := [][]int{values}
for {
newvalues := make([]int, 0, len(values)-1)
allzeros := true
for i := 1; i < len(values); i++ {
nv := values[i] - values[i-1]
if nv != 0 {
allzeros = false
}
newvalues = append(newvalues, nv)
}
valueRows = append(valueRows, newvalues)
values = newvalues
if allzeros {
break
}
}
delta := 0
for i := len(valueRows) - 1; i >= 0; i-- {
delta = valueRows[i][len(valueRows[i])-1] + delta
}
sum += delta
}
return sum
}