Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kotlinx.coroutines.GlobalScope
- import kotlinx.coroutines.async
- import kotlin.system.measureTimeMillis
- suspend fun main() {
- repeat(5) {
- measureTimeMillis {
- val firstPolymer = input_5_test.reduce()
- firstPolymer.count().print { "Length after reducing input: $it" }
- ('a'..'z')
- .map { unit ->
- GlobalScope.async {
- firstPolymer.reduce(unit).count()
- }
- }
- .map { it.await() }
- .min()
- .print { "Minimum length after removing problem units: $it" }
- }.print { "It took $it ms" }
- }
- }
- fun String.reduce(ignore: Char? = null): String {
- return mutableListOf<Char>().apply {
- this@reduce.forEach { char ->
- when {
- char.toLowerCase() == ignore -> Unit
- isEmpty() -> add(char)
- char.implodes(last()) -> removeAt(size - 1)
- else -> add(char)
- }
- }
- }.joinToString("")
- }
- fun Char.implodes(other: Char): Boolean {
- val thisIsUppercase = (this.toUpperCase() == this)
- val otherIsUppercase = (other.toUpperCase() == other)
- return (thisIsUppercase == !otherIsUppercase) && this.toUpperCase() == other.toUpperCase()
- }
- fun <T> T.print(msg: (T) -> Any? = { it }): T = this.apply { println(msg(this)) }
- val input_5_test = "dabAcCaCBAcCcaDA"
Advertisement
Add Comment
Please, Sign In to add comment