-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10AdapterArray.kt
33 lines (27 loc) · 1017 Bytes
/
Day10AdapterArray.kt
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
package adventofcode.year2020
import adventofcode.Puzzle
import adventofcode.PuzzleInput
import adventofcode.common.product
class Day10AdapterArray(customInput: PuzzleInput? = null) : Puzzle(customInput) {
private val sortedInput by lazy { input.lines().map(String::toInt).sorted() }
override fun partOne() =
(sortedInput + listOf(sortedInput.last() + 3))
.foldIndexed(emptyMap<Int, Int>()) { index, acc, elem ->
val previous = sortedInput.getOrElse(index - 1) { 0 }
acc + mapOf(elem - previous to (acc[elem - previous] ?: 0) + 1)
}
.values
.product()
override fun partTwo(): Long {
val k = longArrayOf(1, 0, 0, 0)
(sortedInput + listOf(sortedInput.last() + 3))
.fold(0) { a, b ->
val d = b - a
k.copyInto(k, d, 0, k.size - d)
k.fill(0, 0, d)
k[0] += k.sum()
b
}
return k[0]
}
}