day(07): remove brute force
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
a92bf3b96f
commit
f3b014b06c
1 changed files with 6 additions and 4 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue