-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
66 lines (51 loc) · 1011 Bytes
/
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
61
62
63
64
65
66
package main
import (
"io"
"sort"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
jolts := lib.ReaderToInts(input)
sort.Ints(jolts)
jolts = append([]int{0}, jolts...)
jolts = append(jolts, jolts[len(jolts)-1]+3)
diffs := make(map[int]int)
for i := 0; i < len(jolts)-1; i++ {
diff := jolts[i+1] - jolts[i]
diffs[diff]++
}
return diffs[1] * diffs[3]
}
func fs2(input io.Reader) int {
jolts := lib.ReaderToInts(input)
sort.Ints(jolts)
jolts = append([]int{0}, jolts...)
jolts = append(jolts, jolts[len(jolts)-1]+3)
c := counter{
visited: make(map[int]int),
}
return c.count(0, jolts)
}
type counter struct {
visited map[int]int
}
func (c *counter) count(i int, jolts []int) int {
if v, exists := c.visited[i]; exists {
return v
}
if i == len(jolts)-1 {
return 1
}
sum := 0
for j := i + 1; ; j++ {
if j >= len(jolts) {
break
}
if jolts[j] > jolts[i]+3 {
break
}
sum += c.count(j, jolts)
}
c.visited[i] = sum
return sum
}