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 -> fun part2(input: List<Int>): Int = (input.average().toInt()).let { average ->
input.sumOf { (average - 1..average + 1).minOf { target ->
val diff = (it - target).absoluteValue input.sumOf {
diff * (diff + 1) / 2 val diff = (it - target).absoluteValue
diff * (diff + 1) / 2
}
} }
} }