Guest User

Untitled

a guest
Dec 5th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.45 KB | None | 0 0
  1. import kotlinx.coroutines.GlobalScope
  2. import kotlinx.coroutines.async
  3. import kotlin.system.measureTimeMillis
  4.  
  5. suspend fun main() {
  6.     repeat(5) {
  7.         measureTimeMillis {
  8.             val firstPolymer = input_5_test.reduce()
  9.             firstPolymer.count().print { "Length after reducing input: $it" }
  10.  
  11.             ('a'..'z')
  12.                 .map { unit ->
  13.                     GlobalScope.async {
  14.                         firstPolymer.reduce(unit).count()
  15.                     }
  16.                 }
  17.                 .map { it.await() }
  18.                 .min()
  19.                 .print { "Minimum length after removing problem units: $it" }
  20.  
  21.         }.print { "It took $it ms" }
  22.     }
  23. }
  24.  
  25. fun String.reduce(ignore: Char? = null): String {
  26.     return mutableListOf<Char>().apply {
  27.         this@reduce.forEach { char ->
  28.             when {
  29.                 char.toLowerCase() == ignore -> Unit
  30.                 isEmpty() -> add(char)
  31.                 char.implodes(last()) -> removeAt(size - 1)
  32.                 else -> add(char)
  33.             }
  34.         }
  35.     }.joinToString("")
  36.  
  37. }
  38.  
  39. fun Char.implodes(other: Char): Boolean {
  40.     val thisIsUppercase = (this.toUpperCase() == this)
  41.     val otherIsUppercase = (other.toUpperCase() == other)
  42.     return (thisIsUppercase == !otherIsUppercase) && this.toUpperCase() == other.toUpperCase()
  43. }
  44.  
  45. fun <T> T.print(msg: (T) -> Any? = { it }): T = this.apply { println(msg(this)) }
  46.  
  47.  
  48. val input_5_test = "dabAcCaCBAcCcaDA"
Advertisement
Add Comment
Please, Sign In to add comment