Is there a fancy collections helper method that can help me do this sort of nested for loop when I need to consider each item in a list with the others?
Copy code
for (firstIndex in (0..inputs.size - 2)) {
for (secondIndex in (firstIndex + 1..inputs.size - 1)) {
val firstInput = inputs[firstIndex]
val secondInput = inputs[secondIndex]
...
}
}
j
Joris PZ
12/02/2018, 9:07 AM
Are you doing the Advent of Code by any chance? This came up in today's puzzle.
I did it like this, not sure if there's more elegant ways:
Copy code
val inputs = listOf(3, 4, 5)
inputs.forEachIndexed { index, n ->
inputs.drop(index+1).forEach { m ->
println(n * m)
}
}
a
adam-mcneilly
12/02/2018, 5:54 PM
Yeah, that's where it came up. 🙂
r
robin
12/03/2018, 10:41 AM
I've written a
Combinatoric.kt
helper file with several functions because it comes up so often during AoC 😄
robin
12/03/2018, 10:41 AM
Btw, we're having discussions about Kotlin solutions over at #advent-of-code , come join us if you like!