| 85 |      1 | 
 | 
|  |      2 | import scala.concurrent._
 | 
|  |      3 | import scala.concurrent.duration._
 | 
|  |      4 | import ExecutionContext.Implicits.global
 | 
|  |      5 | import scala.language.postfixOps 
 | 
|  |      6 | 
 | 
| 86 |      7 | 
 | 
|  |      8 | def count_all_tours(dim: Int) = {
 | 
|  |      9 |   for (i <- (0 until dim).toList; 
 | 
|  |     10 |        j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))
 | 
| 85 |     11 | }
 | 
|  |     12 | 
 | 
| 86 |     13 | def add_pair_urban(x: Pos)(y: Pos): Pos = 
 | 
|  |     14 |   (x._1 + y._1, x._2 + y._2)
 | 
|  |     15 | 
 | 
|  |     16 | def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = 
 | 
|  |     17 |   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
 | 
|  |     18 | 
 | 
|  |     19 | def moves_urban(x: Pos): List[Pos] = 
 | 
|  |     20 |   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
 | 
|  |     21 |        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair_urban(x))
 | 
|  |     22 | 
 | 
|  |     23 | def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = 
 | 
|  |     24 |   moves_urban(x).filter(is_legal_urban(dim, path))
 | 
|  |     25 | 
 | 
|  |     26 | def correct_urban(dim: Int)(p: Path): Boolean = p match {
 | 
|  |     27 |   case Nil => true
 | 
|  |     28 |   case x::Nil => true
 | 
|  |     29 |   case x::y::p => if (legal_moves_urban(dim, p, y).contains(x)) correct_urban(dim)(y::p) else false
 | 
|  |     30 | }
 | 
|  |     31 | 
 | 
|  |     32 | 
 | 
|  |     33 | lazy val f = Future {
 | 
|  |     34 |   assert(count_all_tours(1) == List(1))
 | 
|  |     35 |   assert(count_all_tours(2) == List(0, 0, 0, 0))
 | 
|  |     36 |   assert(count_all_tours(3) == List(0, 0, 0, 0, 0, 0, 0, 0, 0))
 | 
|  |     37 |   assert(count_all_tours(4) == List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
 | 
|  |     38 |   assert(count_all_tours(5) == List(304, 0, 56, 0, 304, 0, 56, 0, 56, 0, 56, 0, 64, 0, 56, 0, 56, 0, 56, 0, 304, 0, 56, 0, 304))
 | 
|  |     39 | 
 | 
|  |     40 |   val ts = enum_tours(5, List((0, 2)))
 | 
|  |     41 |   assert(ts.map(correct_urban(5)).forall(_ == true) == true)
 | 
|  |     42 |   assert(ts.length == 56)  
 | 
|  |     43 | }
 | 
|  |     44 | 
 | 
|  |     45 | Await.result(f, 360 second)
 |