-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (42 loc) · 977 Bytes
/
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
package main
import (
"fmt"
"os"
"strconv"
"github.com/jyggen/advent-of-go/internal/solver"
"github.com/jyggen/advent-of-go/internal/utils"
)
func main() {
p1, p2, err := solver.SolveFromFile(os.Stdin, SolvePart1, SolvePart2)
if err != nil {
panic(err)
}
fmt.Println(p1)
fmt.Println(p2)
}
func SolvePart1(input string) (string, error) {
intSlice, err := utils.ToIntegerSlice(input, "\n")
if err != nil {
return "", err
}
result := 0
for i, j := 1, 0; i < len(intSlice); i, j = i+1, j+1 {
if intSlice[i] > intSlice[j] {
result++
}
}
return strconv.Itoa(result), nil
}
func SolvePart2(input string) (string, error) {
intSlice, err := utils.ToIntegerSlice(input, "\n")
if err != nil {
return "", err
}
result := 0
for i, j, k, l := 3, 2, 1, 0; i < len(intSlice); i, j, k, l = i+1, j+1, k+1, l+1 {
if intSlice[j]+intSlice[k]+intSlice[l] < intSlice[i]+intSlice[j]+intSlice[k] {
result++
}
}
return strconv.Itoa(result), nil
}