-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchallenge.go
More file actions
62 lines (53 loc) · 1015 Bytes
/
challenge.go
File metadata and controls
62 lines (53 loc) · 1015 Bytes
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 challenge
import (
"strconv"
"strings"
"github.com/codemicro/adventOfCode/lib/aocgo"
)
func parse(instr string) ([]int, error) {
var o []int
for _, line := range strings.Split(instr, "\n") {
if line == "" {
continue
}
t := strings.TrimSpace(line)
n, err := strconv.Atoi(t)
if err != nil {
return nil, err
}
o = append(o, n)
}
return o, nil
}
func countIncreases(data []int) int {
var c int
for i := 1; i < len(data); i += 1 {
if data[i] > data[i-1] {
c += 1
}
}
return c
}
type Challenge struct {
aocgo.BaseChallenge
}
func (c Challenge) One(instr string) (interface{}, error) {
data, err := parse(instr)
if err != nil {
return nil, err
}
return countIncreases(data), nil
}
func (c Challenge) Two(instr string) (interface{}, error) {
data, err := parse(instr)
if err != nil {
return nil, err
}
var sums []int
{
for i := 0; i < len(data)-2; i += 1 {
sums = append(sums, data[i]+data[i+1]+data[i+2])
}
}
return countIncreases(sums), nil
}