| 45 |      1 | // Part 1 about finding and counting Knight's tours
 | 
|  |      2 | //==================================================
 | 
|  |      3 | 
 | 
|  |      4 | type Pos = (Int, Int)    // a position on a chessboard 
 | 
|  |      5 | type Path = List[Pos]    // a path...a list of positions
 | 
|  |      6 | 
 | 
|  |      7 | def print_board(dim: Int, path: Path): Unit = {
 | 
|  |      8 |   println
 | 
|  |      9 |   for (i <- 0 until dim) {
 | 
|  |     10 |     for (j <- 0 until dim) {
 | 
|  |     11 |       print(f"${path.reverse.indexOf((j, dim - i - 1))}%3.0f ")
 | 
|  |     12 |     }
 | 
|  |     13 |     println
 | 
|  |     14 |   } 
 | 
|  |     15 | }
 | 
|  |     16 | 
 | 
|  |     17 | def add_pair(x: Pos)(y: Pos): Pos = 
 | 
|  |     18 |   (x._1 + y._1, x._2 + y._2)
 | 
|  |     19 | 
 | 
|  |     20 | def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
 | 
|  |     21 |   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
 | 
|  |     22 | 
 | 
| 86 |     23 | assert(is_legal(8, Nil)((3,4)) == true)
 | 
|  |     24 | assert(is_legal(8, List((4,1), (1,0)))((4,1)) == false)
 | 
|  |     25 | assert(is_legal(2, Nil)((0,0)) == true)
 | 
|  |     26 | 
 | 
| 45 |     27 | def moves(x: Pos): List[Pos] = 
 | 
|  |     28 |   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
 | 
|  |     29 |        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
 | 
|  |     30 | 
 | 
|  |     31 | def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
 | 
|  |     32 |   moves(x).filter(is_legal(dim, path))
 | 
|  |     33 | 
 | 
| 50 |     34 | assert(legal_moves(8, Nil, (2,2)) == 
 | 
|  |     35 |   List((3,4), (4,3), (4,1), (3,0), (1,0), (0,1), (0,3), (1,4)))
 | 
|  |     36 | assert(legal_moves(8, Nil, (7,7)) == List((6,5), (5,6)))
 | 
|  |     37 | assert(legal_moves(8, List((4,1), (1,0)), (2,2)) == 
 | 
|  |     38 |   List((3,4), (4,3), (3,0), (0,1), (0,3), (1,4)))
 | 
|  |     39 | assert(legal_moves(8, List((6,6)), (7,7)) == List((6,5), (5,6)))
 | 
| 85 |     40 | assert(legal_moves(1, Nil, (0,0)) == List())
 | 
|  |     41 | assert(legal_moves(2, Nil, (0,0)) == List())
 | 
|  |     42 | assert(legal_moves(3, Nil, (0,0)) == List((1,2), (2,1)))
 | 
|  |     43 | 
 | 
| 45 |     44 | 
 | 
|  |     45 | def count_tours(dim: Int, path: Path): Int = {
 | 
|  |     46 |   if (path.length == dim * dim) 1
 | 
|  |     47 |   else 
 | 
|  |     48 |     (for (x <- legal_moves(dim, path, path.head)) yield count_tours(dim, x::path)).sum
 | 
|  |     49 | }
 | 
|  |     50 | 
 | 
|  |     51 | def enum_tours(dim: Int, path: Path): List[Path] = {
 | 
|  |     52 |   if (path.length == dim * dim) List(path)
 | 
|  |     53 |   else 
 | 
|  |     54 |     (for (x <- legal_moves(dim, path, path.head)) yield enum_tours(dim, x::path)).flatten
 | 
|  |     55 | }
 | 
|  |     56 | 
 | 
| 86 |     57 | def count_all_tours(dim: Int) = {
 | 
|  |     58 |   for (i <- (0 until dim).toList; 
 | 
|  |     59 |        j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))
 | 
| 45 |     60 | }
 | 
|  |     61 | 
 | 
|  |     62 | def enum_all_tours(dim: Int): List[Path] = {
 | 
|  |     63 |   (for (i <- (0 until dim).toList; 
 | 
|  |     64 |         j <- (0 until dim).toList) yield enum_tours(dim, List((i, j)))).flatten
 | 
|  |     65 | }
 | 
|  |     66 | 
 | 
| 86 |     67 | 
 | 
|  |     68 | def add_pair_urban(x: Pos)(y: Pos): Pos = 
 | 
|  |     69 |   (x._1 + y._1, x._2 + y._2)
 | 
|  |     70 | 
 | 
|  |     71 | def is_legal_urban(dim: Int, path: Path)(x: Pos): Boolean = 
 | 
|  |     72 |   0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
 | 
|  |     73 | 
 | 
|  |     74 | def moves_urban(x: Pos): List[Pos] = 
 | 
|  |     75 |   List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
 | 
|  |     76 |        (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair_urban(x))
 | 
|  |     77 | 
 | 
|  |     78 | def legal_moves_urban(dim: Int, path: Path, x: Pos): List[Pos] = 
 | 
|  |     79 |   moves_urban(x).filter(is_legal_urban(dim, path))
 | 
|  |     80 | 
 | 
|  |     81 | def correct_urban(dim: Int)(p: Path): Boolean = p match {
 | 
|  |     82 |   case Nil => true
 | 
|  |     83 |   case x::Nil => true
 | 
|  |     84 |   case x::y::p => if (legal_moves_urban(dim, p, y).contains(x)) correct_urban(dim)(y::p) else false
 | 
|  |     85 | }
 | 
|  |     86 | 
 | 
|  |     87 | enum_tours(5, List((0, 2))).map(correct_urban(5)).forall(_ == true)
 | 
|  |     88 | 
 | 
|  |     89 | 
 | 
| 45 |     90 | for (dim <- 1 to 5) {
 | 
|  |     91 |   println(s"${dim} x ${dim} " + count_tours(dim, List((0, 0))))
 | 
|  |     92 | }
 | 
|  |     93 | 
 | 
|  |     94 | for (dim <- 1 to 5) {
 | 
|  |     95 |   println(s"${dim} x ${dim} " + count_all_tours(dim))
 | 
|  |     96 | }
 | 
|  |     97 | 
 | 
|  |     98 | for (dim <- 1 to 5) {
 | 
|  |     99 |   val ts = enum_tours(dim, List((0, 0)))
 | 
|  |    100 |   println(s"${dim} x ${dim} ")   
 | 
|  |    101 |   if (ts != Nil) {
 | 
|  |    102 |     print_board(dim, ts.head)
 | 
|  |    103 |     println(ts.head)
 | 
|  |    104 |   }
 | 
|  |    105 | }
 | 
|  |    106 | 
 | 
|  |    107 | 
 |