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,12 +10,14 @@ fun part1(input: List<Int>): Int = input.toSet().minOf { target ->
} }
} }
fun part2(input: List<Int>): Int = (input.minOrNull()!!..input.maxOrNull()!!).minOf { target -> fun part2(input: List<Int>): Int = (input.average().toInt()).let { average ->
(average - 1..average + 1).minOf { target ->
input.sumOf { input.sumOf {
val diff = (it - target).absoluteValue val diff = (it - target).absoluteValue
diff * (diff + 1) / 2 diff * (diff + 1) / 2
} }
} }
}
fun main() { fun main() {
val testInput = readInputAsCommaSeparatedInts(7, "test_input").sorted() val testInput = readInputAsCommaSeparatedInts(7, "test_input").sorted()