Browse Source

Day10

master
Karl Spickermann 9 months ago
parent
commit
92ab7fae32
  1. 77
      day10/day10.go
  2. 106
      day10/day10Input.txt
  3. 11
      day10/day10Test.txt
  4. 31
      day10/day10Test2.txt

77
day10/day10.go

@ -0,0 +1,77 @@
package main
import (
"AoC2020/helper"
"fmt"
"os"
"sort"
)
func main() {
args := os.Args[1:]
input, err := helper.GetInput(args[0])
if err != nil {
fmt.Println(err)
}
numbers, err := helper.MapToNumber(input)
if err != nil {
fmt.Println(err)
}
part2(numbers)
}
func part2(numbers []int) {
sort.Ints(numbers)
numbers = prependInt(numbers,0)
numbers = append(numbers,numbers[len(numbers)-1]+3)
fmt.Printf("%v \n",numbers)
possibilities := make([][3]int, len(numbers))
possibilities[len(numbers)-1] = [3]int{0,0,1}
for i:= len(numbers)-1; i >= 0; i-- {
diff := 0
j := 1
for diff <= 3 && i+j<len(numbers){
diff = numbers[i+j] - numbers[i]
switch diff {
case 1:
possibilities[i][0] = sum(possibilities[i+j])
case 2:
possibilities[i][1] = sum(possibilities[i+j])
case 3:
possibilities[i][2] = sum(possibilities[i+j])
}
j++
}
}
fmt.Printf("%v \n",possibilities)
fmt.Printf("%d \n",sum(possibilities[0]))
}
func part1(numbers []int) {
sort.Ints(numbers)
diffCounter := make([]int, 3)
numbers = prependInt(numbers,0)
numbers = append(numbers,numbers[len(numbers)-1]+3)
fmt.Printf("%v \n",numbers)
for i:= 0; i < len(numbers)-1; i++ {
diffCounter[numbers[i+1] - numbers[i]-1]++
}
fmt.Printf("%v \n",diffCounter)
fmt.Print(diffCounter[0] * diffCounter[2])
}
func prependInt(x []int, y int) []int {
x = append(x, 0)
copy(x[1:], x)
x[0] = y
return x
}
func sum(array [3]int) int {
result := 0
for _, v := range array {
result += v
}
return result
}

106
day10/day10Input.txt

@ -0,0 +1,106 @@
66
7
73
162
62
165
157
158
137
125
138
59
36
40
94
95
13
35
136
96
156
155
24
84
42
171
142
3
104
149
83
129
19
122
68
103
74
118
20
110
54
127
88
31
135
26
126
2
51
91
16
65
128
119
67
48
111
29
49
12
132
17
41
166
75
146
50
30
1
164
112
34
18
72
97
145
11
117
58
78
152
90
172
163
89
107
45
37
79
159
141
105
10
115
69
170
25
100
80
4
85
169
106
57
116
23

11
day10/day10Test.txt

@ -0,0 +1,11 @@
16
10
15
5
1
11
7
19
6
12
4

31
day10/day10Test2.txt

@ -0,0 +1,31 @@
28
33
18
42
31
14
46
20
48
47
24
23
49
45
19
38
39
11
1
32
25
35
8
17
7
9
4
2
34
10
3
Loading…
Cancel
Save