-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.go
61 lines (56 loc) · 1.24 KB
/
day1.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
package main
import (
"fmt"
"io/ioutil"
"sort"
"strconv"
"strings"
)
func main() {
i, _ := ioutil.ReadFile("input.txt")
input := string(i)
fmt.Printf("Part 1: %v\n", SolveDay1Part1(listToSlice(input)))
fmt.Printf("Part 2: %v\n", SolveDay1Part2(listToSlice(input)))
}
//SolveDay1Part1 search for two numbers in a slice that sum is 2020 and multiply them
func SolveDay1Part1(nums []int) int {
for _, num := range nums {
for _, num2 := range nums {
if (num + num2) == 2020 {
return num * num2
}
}
}
return 0
}
//SolveDay1Part2 search for three numbers in a slice that sum is 2020 and multiply them
func SolveDay1Part2(nums []int) int {
sort.Ints(nums)
for _, num := range nums {
for _, num2 := range nums {
if (num + num2) > 2020 {
break
}
for _, num3 := range nums {
if (num + num2 + num3) == 2020 {
return num * num2 * num3
}
if (num + num2 + num3) > 2020 {
break
}
}
}
}
return 0
}
//listToSlice converts the list of numbers (each number one row) to a slice
func listToSlice(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
}