1
Fork 0
This commit is contained in:
Jan-Lukas Else 2020-12-11 08:45:39 +01:00
parent 71fa820871
commit 8c4e67b4cb
2 changed files with 153 additions and 0 deletions

96
10/input.txt Normal file
View File

@ -0,0 +1,96 @@
70
102
148
9
99
63
40
52
91
39
55
28
54
22
95
61
118
35
14
21
129
82
137
45
7
87
81
25
3
108
41
11
145
18
65
80
115
29
136
42
97
104
117
141
62
121
23
96
24
128
48
1
112
8
34
144
134
116
58
147
51
84
17
126
64
68
135
10
77
105
127
73
111
90
16
103
109
98
146
123
130
69
133
110
30
122
15
74
33
38
83
92
2
53
140
4

57
10/main.go Normal file
View File

@ -0,0 +1,57 @@
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
)
func main() {
// Part 1
joltages := []int{}
file, _ := os.Open("input.txt")
defer file.Close()
for scanner := bufio.NewScanner(file); scanner.Scan(); {
joltage, _ := strconv.Atoi(scanner.Text())
joltages = append(joltages, joltage)
}
sort.Ints(joltages)
jolts := map[int]int{}
currentJolt := 0
for _, j := range joltages {
jolts[j-currentJolt]++
currentJolt = j
}
// Device itself
jolts[3]++
fmt.Println("Part 1:", jolts[1]*jolts[3])
// Part 2
fmt.Println("Part 2:", countPaths(0, joltages, map[int]int{}))
}
func countPaths(current int, joltages []int, memory map[int]int) (total int) {
if len(joltages) == 0 {
return 1
}
for _, next := range joltages {
if next-current > 3 {
break
}
if _, exists := memory[next]; !exists {
memory[next] = countPaths(next, joltages[1:], memory)
}
total += memory[next]
}
return
}