Advertisement
Guest User

Untitled

a guest
May 18th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.73 KB | None | 0 0
  1. import kotlinx.coroutines.experimental.async
  2. import kotlinx.coroutines.experimental.runBlocking
  3. import kotlin.system.measureTimeMillis
  4.  
  5. data class City(
  6.         val name: String
  7. ) {
  8.     val fligths: MutableSet<Flight> = mutableSetOf()
  9. }
  10.  
  11. data class Flight(
  12.         val from: City,
  13.         val destination: City
  14. )
  15.  
  16. suspend fun fly(deep: Int, visitedCities: MutableSet<City>, city:City, previousFlight: Flight?): List<Flight>? {
  17.     return city.fligths.filterNot {
  18.         visitedCities.contains(city)
  19.     }.flatMap {
  20.         val visitedCopy = visitedCities.toMutableSet().apply {
  21.             if(previousFlight!=null) add(it.from)
  22.         }
  23.         if(it.destination.name == "Vancouver") {
  24.             listOf(async {
  25.                 mutableListOf<Flight>().apply {
  26.                     if (previousFlight != null) add(previousFlight)
  27.                     add(it)
  28.                 }
  29.             })
  30.         }
  31.         else {
  32.             listOf(async { fly(deep+1, visitedCopy, it.destination, it) })
  33.         }
  34.     }.flatMap {
  35.         listOf(it.await())
  36.     }.filterNotNull().maxBy {
  37.         it.size
  38.     }?.toMutableList()?.apply {
  39.         previousFlight?.let {
  40.             add(it)
  41.         }
  42.     }
  43. }
  44.  
  45. fun main(args: Array<String>) = runBlocking {
  46.     val cities = mutableListOf<City>()
  47.     cities.apply {
  48.         add(City("Vancouver"))
  49.         add(City("Yellowknife"))
  50.         add(City("Edmonton"))
  51.         add(City("Calgary"))
  52.         add(City("Winnipeg"))
  53.         add(City("Toronto"))
  54.         add(City("Montreal"))
  55.         add(City("Halifax"))
  56.     }
  57.     cities[0].apply {
  58.         fligths.add(Flight(this, cities[2]))
  59.         fligths.add(Flight(this, cities[3]))
  60.     }
  61.     cities[2].apply {
  62.         fligths.add(Flight(this, cities[0]))
  63.         fligths.add(Flight(this, cities[6]))
  64.         fligths.add(Flight(this, cities[1]))
  65.         fligths.add(Flight(this, cities[3]))
  66.     }
  67.     cities[3].apply {
  68.         fligths.add(Flight(this, cities[0]))
  69.         fligths.add(Flight(this, cities[4]))
  70.     }
  71.     cities[4].apply {
  72.         fligths.add(Flight(this, cities[0]))
  73.         fligths.add(Flight(this, cities[5]))
  74.     }
  75.     cities[5].apply {
  76.         fligths.add(Flight(this, cities[0]))
  77.         fligths.add(Flight(this, cities[6]))
  78.     }
  79.     cities[6].apply {
  80.         fligths.add(Flight(this, cities[0]))
  81.         fligths.add(Flight(this, cities[7]))
  82.     }
  83.     cities[7].apply {
  84.         fligths.add(Flight(this, cities[0]))
  85.     }
  86.  
  87.     val time = measureTimeMillis {
  88.         val result = fly(0, mutableSetOf(), cities[0], null)
  89.         print(cities[0].name)
  90.         result?.forEach {
  91.             print(" -> ${it.destination.name}")
  92.         }
  93.         println(" (${result?.size} flights)")
  94.     }
  95.     println("$time ms")
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement