-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (48 loc) · 1.08 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
package main
import (
"fmt"
"github.com/davidporos92/aoc-2020/utils"
)
const preambleLength = 25
func main() {
input := utils.NewReader("./input-1.dat").MustReadIntSliceFromFile()
solution1 := part1(input)
solution2 := part2(input, solution1)
fmt.Printf("Solution 1: %d\n", solution1)
fmt.Printf("Solution 2: %d\n", solution2)
}
func part1(input []int) int {
for i := preambleLength; i < len(input); i++ {
if !isPartOfCypherList(input[i], input[i-preambleLength:i]) {
return input[i]
}
}
panic("No solution found")
}
func part2(input []int, search int) int {
for i := 0; i < len(input)-2; i++ {
for j := i + 2; j < len(input); j++ {
list := input[i:j]
sum := utils.SumInts(list)
if sum > search {
break
}
if sum == search {
min, max := utils.MinMax(list)
return min + max
}
}
}
panic("No solution found")
}
func isPartOfCypherList(search int, haystack []int) bool {
for i := 0; i < len(haystack)-1; i++ {
a := haystack[i]
for j := i + 1; j < len(haystack); j++ {
if a+haystack[j] == search {
return true
}
}
}
return false
}