-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (65 loc) · 1.16 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
input "aoc2020/inpututils"
"fmt"
"sort"
)
func main() {
fmt.Println("--- Part One ---")
fmt.Println(Part1("input.txt"))
fmt.Println("--- Part Two ---")
fmt.Println(Part2("input.txt"))
}
func Part1(filename string) int {
nums := input.ReadNumbers(filename)
sort.Ints(nums)
ones, threes, v := 0, 0, 0
for _, x := range nums {
if (x - v) == 1 {
ones += 1
} else if (x - v) == 3 {
threes += 1
}
v = x
}
return ones * (threes + 1)
}
func Part2(filename string) int {
nums := input.ReadNumbers(filename)
numSet := map[int]struct{}{}
for _, n := range nums {
numSet[n] = struct{}{}
}
maxV := max(nums)
cache := make([]int, maxV)
for i := range cache {
cache[i] = -1
}
var numberOfWays func(int) int
numberOfWays = func(v int) int {
if _, ok := numSet[v]; !ok {
return 0
}
if v == maxV {
return 1
}
if cache[v] == -1 {
s := 0
for _, x := range []int{1, 2, 3} {
s += numberOfWays(v + x)
}
cache[v] = s
}
return cache[v]
}
return numberOfWays(1) + numberOfWays(2) + numberOfWays(3)
}
func max(s []int) int {
m := s[0]
for _, v := range s {
if v > m {
m = v
}
}
return m
}