generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task.kt
67 lines (57 loc) · 1.8 KB
/
Task.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
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
package day07
import readInput
import kotlin.math.max
import kotlin.collections.sumOf
import kotlin.math.abs
import kotlin.math.min
fun main() {
val input = readInput("day07")
println(solvePartOne(input))
println(solvePartTwo(input))
}
fun solvePartOne(input: List<String>): Int = input.toInput()
.minValue { position -> linearConsumption(position) }
fun solvePartTwo(input: List<String>): Int = input.toInput()
.minValue { position -> progressiveConsumption(position) }
private fun List<String>.toInput() = first().split(',').map { value -> value.toInt() }
private fun List<Int>.linearConsumption(position: Int) = sumOf { value -> abs(position - value) }
private fun List<Int>.progressiveConsumption(position: Int) = sumOf { value ->
val stepsCount = abs(position - value)
(stepsCount + 1) * stepsCount / 2
}
private fun List<Int>.minValue(
block: List<Int>.(position: Int) -> Int,
): Int {
var maxValue = Int.MIN_VALUE
var minValue = Int.MAX_VALUE
onEach { value ->
maxValue = max(maxValue, value)
minValue = min(minValue, value)
}
return recursiveBinarySearch(
start = minValue,
end = maxValue,
) { index -> block(index) }
}
private fun recursiveBinarySearch(
start: Int,
end: Int,
selector: (position: Int) -> Int
): Int {
if (start == end) return selector(start)
val startSelector = selector(start)
val endSelector = selector(end)
val middle = (start + end) / 2
return when {
startSelector < endSelector -> recursiveBinarySearch(
start = start,
end = middle,
selector = selector
)
else -> recursiveBinarySearch(
start = if (start == middle) middle + 1 else middle,
end = end,
selector = selector
)
}
}