1
0
Fork 0

day(07): remove brute force

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2021-12-07 13:22:17 +01:00
parent a92bf3b96f
commit f3b014b06c

View file

@ -10,10 +10,12 @@ fun part1(input: List<Int>): Int = input.toSet().minOf { target ->
}
}
fun part2(input: List<Int>): Int = (input.minOrNull()!!..input.maxOrNull()!!).minOf { target ->
input.sumOf {
val diff = (it - target).absoluteValue
diff * (diff + 1) / 2
fun part2(input: List<Int>): Int = (input.average().toInt()).let { average ->
(average - 1..average + 1).minOf { target ->
input.sumOf {
val diff = (it - target).absoluteValue
diff * (diff + 1) / 2
}
}
}