1
0
This commit is contained in:
Hubert Van De Walle 2020-12-10 08:47:44 +01:00
parent 67c9cf0391
commit 34259208d5
2 changed files with 143 additions and 0 deletions

View File

@ -0,0 +1,50 @@
package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.input.Day
import be.vandewalleh.aoc.utils.input.Input
import be.vandewalleh.aoc.utils.input.Lines
import be.vandewalleh.aoc.utils.input.createDay
import org.eclipse.collections.api.list.primitive.MutableIntList
import org.eclipse.collections.impl.factory.primitive.IntLists
import org.eclipse.collections.impl.factory.primitive.IntLongMaps
@Day(10)
class Day10(@Lines val input: Input<IntArray>) {
fun part1(): Int {
val sorted = IntLists.mutable.of(0, *input.value).apply {
sortThis()
add(last + 3)
}.toArray().toList()
var ones = 0
var threes = 0
sorted.zipWithNext().forEach { (a, b) ->
if (a + 1 == b) ones++
else if (a + 3 == b) threes++
}
return ones * threes
}
fun part2(): Long {
val sorted: MutableIntList = IntLists.mutable.of(*input.value).apply { sortThis() }
val map = IntLongMaps.mutable.empty().apply {
put(0, 1L)
}
sorted.forEach { i ->
map.put(i, map.get(i - 1) + map.get(i - 2) + map.get(i - 3))
}
return map.get(sorted.last)
}
}
fun main() = with(createDay<Day10>()) {
println(part1())
println(part2())
}

View File

@ -0,0 +1,93 @@
47
99
115
65
10
55
19
73
80
100
71
110
64
135
49
3
1
98
132
2
38
118
66
116
104
87
79
114
40
37
44
97
4
140
60
86
56
133
7
146
85
111
134
53
121
77
117
21
12
81
145
129
107
93
22
48
11
54
92
78
67
20
138
125
57
96
26
147
124
34
74
143
13
28
126
50
29
70
39
63
41
91
32
84
144
27
139
33
88
72
23
103
16