day(07): use ‹when› expression instead of body
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
638cf91099
commit
754daa65e4
1 changed files with 12 additions and 15 deletions
23
src/Day07.kt
23
src/Day07.kt
|
@ -7,18 +7,12 @@ private data class Equation(
|
||||||
result: Long,
|
result: Long,
|
||||||
i: Int,
|
i: Int,
|
||||||
op: (Long, Long) -> Long,
|
op: (Long, Long) -> Long,
|
||||||
): Boolean {
|
): Boolean =
|
||||||
// used all operands
|
when {
|
||||||
if (i >= operands.size) {
|
i >= operands.size -> result == target
|
||||||
return result == target
|
result > target -> false
|
||||||
}
|
else ->
|
||||||
|
ops.any { nextOp ->
|
||||||
// overshot
|
|
||||||
if (result > target) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return ops.any { nextOp ->
|
|
||||||
canSatisfy(ops, op(result, operands[i]), i + 1, nextOp)
|
canSatisfy(ops, op(result, operands[i]), i + 1, nextOp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,7 +59,10 @@ class Day07(
|
||||||
// no-op
|
// no-op
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getSum(ops: List<(Long, Long) -> Long>): Long = equations.filter { it.isSatisfiable(ops) }.sumOf { it.target }
|
private fun getSum(ops: List<(Long, Long) -> Long>): Long =
|
||||||
|
equations
|
||||||
|
.filter { it.isSatisfiable(ops) }
|
||||||
|
.sumOf { it.target }
|
||||||
|
|
||||||
override fun part1(): Long = getSum(PART1_OPS)
|
override fun part1(): Long = getSum(PART1_OPS)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue