-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.go
89 lines (83 loc) · 1.83 KB
/
day9.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
86
87
88
89
package main
import (
"fmt"
"io/ioutil"
"os"
"sort"
"strconv"
"strings"
)
func main() {
i, err := ioutil.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
input := string(i)
fmt.Printf("Part 1: %v\n", SolveDay9Part1(intListToSlice(input), 25))
fmt.Printf("Part 2: %v\n", SolveDay9Part2(intListToSlice(input), 25))
}
//SolveDay9Part1 returns the first value that can not reached with a sum of 2 values of the last $start values
func SolveDay9Part1(i []int, start int) (s int) {
cur := start
if start > len(i) || start < 2 {
return 0
}
for {
found := false
if len(i) == cur {
break
}
for _, num := range i[cur-start : cur] {
if found {
break
}
for _, num2 := range i[cur-start : cur] {
if num+num2 == i[cur] && num != num2 {
found = true
break
}
}
}
if found {
cur++
continue
}
return i[cur]
}
return 0
}
//SolveDay9Part2 search for 5 numbers that sum is the value from SolveDay9Part1 and sum the lowest and highest
func SolveDay9Part2(i []int, start int) (s int) {
numToSolve := SolveDay9Part1(i, start)
var index, currentIndex, currentSum int
var currentNumbers []int
for {
currentSum += i[currentIndex]
currentNumbers = append(currentNumbers, i[currentIndex])
if currentSum == numToSolve {
break
}
if currentSum > numToSolve {
index++
currentIndex = index
currentSum = 0
currentNumbers = []int{}
continue
}
currentIndex++
}
sort.Ints(currentNumbers)
return currentNumbers[0] + currentNumbers[len(currentNumbers)-1]
}
//Helper functions
//intListToSlice converts the list of numbers (each number one row) to a slice
func intListToSlice(list string) (i []int) {
for _, line := range strings.Split(strings.TrimSuffix(list, "\n"), "\n") {
lineInt, err := strconv.Atoi(line)
if err != nil {
return nil
}
i = append(i, lineInt)
}
return
}