-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.go
90 lines (75 loc) · 1.72 KB
/
day10.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
90
package main
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
"sort"
"strconv"
"strings"
)
// Read the input file.
func getInput() []string {
filepath, err := filepath.Abs("2020/inputs/day10test.txt")
if err != nil {
log.Fatal(err)
}
inputVal, err := ioutil.ReadFile(filepath)
if err != nil {
log.Fatal(err)
}
return strings.Split(string(inputVal), "\n")
}
func convertList(inputVal []string) []int {
var returnVal []int
for _, element := range inputVal {
convertedElement, err := strconv.Atoi(element)
if err != nil {
log.Fatal(err)
}
returnVal = append(returnVal, convertedElement)
}
sort.Ints(returnVal)
return returnVal
}
// Find the voltage differences given a list of adapters.
func partOne(voltages []int) int {
voltageGaps := make(map[int]int)
for i, element := range voltages {
if i == 0 {
voltageGaps[voltages[i]]++
continue
}
voltageGap := element - voltages[i-1]
voltageGaps[voltageGap]++
}
// Multiple the 1 and 3 gap differences
gapProduct := voltageGaps[1] * (voltageGaps[3] + 1)
return gapProduct
}
// Find how many possible combinations of adapters can exist
func partTwo(voltages []int) int {
allCombinations := 1
for i, element := range voltages {
nCombinations := 0
for ii := 1; ii < 4; ii++ {
if i+ii >= len(voltages) {
continue
}
diff := voltages[i+ii] - element
if diff <= 3 {
nCombinations++
}
}
allCombinations *= nCombinations
}
return allCombinations
}
func main() {
inputVal := getInput()
voltages := convertList(inputVal)
partOneAnswer := partOne(voltages)
partTwoAnswer := partTwo(voltages)
fmt.Printf("Part One - voltage gap product: %d\n", partOneAnswer)
fmt.Printf("Part Two - total combinations: %d\n", partTwoAnswer)
}