-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.go
114 lines (93 loc) · 3.21 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package day10
import (
"sort"
"strconv"
"strings"
"github.com/augustoccesar/adventofcode/utils"
)
type Day10 struct{}
func (d *Day10) InputFileName() string { return "input" }
func (d *Day10) PartOne(input string) string {
adapters := readIntSliceInput(input)
sort.Ints(adapters)
largestJolts := adapters[len(adapters)-1]
smalledsJolts := adapters[0]
lookup := map[int]bool{}
for _, item := range adapters {
lookup[item] = true
}
// 3 starts with 1 to compensate for the built-in adapter on the device
// that is always 3 jolts more than the largest adapter
diffs := map[int]int{1: 0, 2: 0, 3: 1}
// Add the starting adapter diff to the outlet
diffs[smalledsJolts]++
for _, adapter := range adapters {
if adapter == largestJolts {
break
}
validAdapters := []int{}
for i := 1; i <= 3; i++ {
if _, ok := lookup[adapter+i]; ok {
validAdapters = append(validAdapters, adapter+i)
}
}
diff := validAdapters[0] - adapter
diffs[diff]++
}
result := diffs[1] * diffs[3]
return strconv.Itoa(result)
}
func (d *Day10) PartTwo(input string) string {
// Considering 1, 4, 5, 6, 7, 10, 11, 12, 15, 16, 19
// Basically count the total for the arrows leaving the number (paths).
// 19(8) -> 16(8) -> 15(8) -> 12(8)
// |
// +-> 11(4)
// | |
// | v
// +-> 10(4) -> 7(4)
// |
// +-> 6(2) -+
// | | |
// | v |
// +-> 5(1) |
// | | |
// | v |
// +-> 4(1) <+
// |
// +-> 1 (1)
//
// 1: _ (1 -> 1 from 0 by default)
// 4: 1 (1 -> 1 from the 1 + 0 from the 3 + 0 from the 2)
// 5: 4 (1 -> 1 from the 4 + 0 from the 3 + 0 from the 2)
// 6: 5, 4 (2 -> 1 from the 5 + 1 from the 4 + 0 from the 3)
// 7: 6, 5, 4 (4 -> 2 from the 6 + 1 from the 5 + 1 from the 4)
// 10: 7 (4 -> 0 from the 9 + 0 from the 8 + 4 from the 7)
// 11: 10 (4 -> 4 from the 10 + 0 from the 9 + 0 from the 8)
// 12: 11, 10 (8 -> 4 from the 11 + 4 from the 10 + 0 from the 9)
// 15: 12 (8 -> 0 from the 14 + 0 from the 13 + 8 from the 12)
// 16: 15 (8 -> 8 from the 15 + 0 from the 14 + 0 from the 13)
// 19: 16 (8 -> 0 from the 18 + 0 from the 17 + 8 from the 16)
adapters := readIntSliceInput(input)
sort.Ints(adapters)
largestJolts := adapters[len(adapters)-1]
counter := map[int]int{}
counter[0] = 1
for _, item := range adapters {
counter[item] = 0
for i := 1; i <= 3; i++ {
if val, ok := counter[item-i]; ok {
counter[item] += val
}
}
}
return strconv.Itoa(counter[largestJolts])
}
func readIntSliceInput(input string) []int {
lines := strings.Split(input, "\n")
iInput := make([]int, len(lines))
for i, item := range lines {
iInput[i] = utils.Atoi(item)
}
return iInput
}