4 ms·
Never thought I'd see anything more concise than Python, but here's the Kotlin version: data class Candidate(val x: Int, val y: Int) fun List<Candidat
by yen223 4y ago
Never thought I'd see anything more concise than Python, but here's the Kotlin version:
data class Candidate(val x: Int, val y: Int)
fun List<Candidate>.filterCandidates(knows: Boolean, aggBy: (Candidate) -> Int) =
groupBy(aggBy)
.values
.filter {value -> if (knows) {value.count() == 1} else {value.count() > 1} }
.flatten()
fun main() {
var cs = (1..99).flatMap { x -> (x..99).map { y -> Candidate(x, y) } }
repeat(7) {
cs = cs.filterCandidates(knows = false) { (x, y) -> x * y }
cs = cs.filterCandidates(knows = false) { (x, y) -> x + y }
}
cs = cs.filterCandidates(knows = true) { (x, y) -> x * y }
println(cs)
}