-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd01.go
More file actions
92 lines (71 loc) · 1.6 KB
/
d01.go
File metadata and controls
92 lines (71 loc) · 1.6 KB
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
90
91
92
package d01
import (
"sort"
"strconv"
"strings"
"com.github/augustoccesar/adventofcode/golang/structure"
)
type Day01 struct{}
func (d *Day01) Year() int { return 2020 }
func (d *Day01) Day() int { return 1 }
func (d *Day01) PartOne() string {
var result int
report := getReport(structure.ReadDefaultInput(d))
lookupTable := make(map[int]int)
for _, item := range report {
lookupTable[2020-item] = item
}
for _, item := range report {
if value, ok := lookupTable[item]; ok {
result = item * value
break
}
}
return strconv.Itoa(result)
}
func (d *Day01) PartTwo() string {
var result int
report := getReport(structure.ReadDefaultInput(d))
sort.Ints(report)
maximumThird := 2020 - (report[0] + report[1])
possibleNumbers := []int{}
for _, item := range report {
if item <= maximumThird {
possibleNumbers = append(possibleNumbers, item)
}
}
validPins := false
pin1 := 0
pin2 := 1
pin3 := len(possibleNumbers) - 1
for !validPins {
pinsResult := possibleNumbers[pin1] + possibleNumbers[pin2] + possibleNumbers[pin3]
if pinsResult == 2020 {
validPins = true
continue
}
if pinsResult < 2020 {
pin1++
pin2++
continue
}
if pinsResult > 2020 {
pin3--
continue
}
}
result = possibleNumbers[pin1] * possibleNumbers[pin2] * possibleNumbers[pin3]
return strconv.Itoa(result)
}
func getReport(input string) []int {
report := strings.Split(string(input), "\n")
intReport := make([]int, len(report))
for i, item := range report {
itemInt, err := strconv.Atoi(item)
if err != nil {
panic(err)
}
intReport[i] = itemInt
}
return intReport
}