Attic/knight3_sol.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 06 Jun 2024 19:14:33 +0100
changeset 489 d51cacc92480
parent 468 0587ef444547
permissions -rw-r--r--
updated to Scala 3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
     1
// Part 3 about finding a single tour using the Warnsdorf Rule
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
     2
//=============================================================
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     4
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     5
type Pos = (Int, Int)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     6
type Path = List[Pos]
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     7
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     8
def print_board(dim: Int, path: Path): Unit = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
     9
  println
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    10
  for (i <- 0 until dim) {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    11
    for (j <- 0 until dim) {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    12
      print(f"${path.reverse.indexOf((i, j))}%3.0f ")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    13
    }
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    14
    println
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    15
  } 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    16
}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    18
def add_pair(x: Pos)(y: Pos): Pos = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    19
  (x._1 + y._1, x._2 + y._2)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    20
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    21
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    22
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    24
def moves(x: Pos): List[Pos] = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    25
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    26
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    27
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    28
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    29
  moves(x).filter(is_legal(dim, path))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    30
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    31
def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    32
  legal_moves(dim, path, x).sortBy((x) => legal_moves(dim, path, x).length)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    33
66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    34
import scala.annotation.tailrec
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    36
/*
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    37
@tailrec
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    38
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    39
  case Nil => None
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    40
  case x::xs => {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    41
    val result = f(x)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    42
    if (result.isDefined) result else first(xs, f)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    43
  }
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    44
}
66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    45
*/
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    46
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    47
def first[A, B](xs: List[A], f: A => Option[B]): Option[B] =
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    48
  xs.flatMap(f(_)).headOption
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    50
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    51
def first_closed_tour_heuristics(dim: Int, path: Path): Option[Path] = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    52
  if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    53
  else
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    54
    first(ordered_moves(dim, path, path.head), (x: Pos) => first_closed_tour_heuristics(dim, x::path))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    55
}
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    57
/*
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    58
for (dim <- 1 to 6) {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    59
  val t = first_closed_tour_heuristics(dim, List((dim / 2, dim / 2)))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    60
  println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    61
}
66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    62
*/
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    63
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    64
//@tailrec
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    65
def first_tour_heuristics(dim: Int, path: Path): Option[Path] = {
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    67
  @tailrec
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    68
  def aux(dim: Int, path: Path, moves: List[Position]): Option[Path 
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    69
  if (path.length == dim * dim) Some(path)
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    70
  else
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    71
    moves match {
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    72
      case Nil => None
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    73
      case x::xs => {
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    74
        val r = first_tour_heuristics(dim, x::path)
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    75
        if (r.isDefined) Some(r) else aux(dim, path, xs)
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    76
    }    
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    77
  aux(dim, path, ordered_moves(dim, path, path.head)) 
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    78
}
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    79
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    81
/*
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    82
def first_tour_heuristics(dim: Int, path: Path): Option[Path] = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    83
  if (path.length == dim * dim) Some(path)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    84
  else
66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    85
    for (p <- ordered_moves(dim, path, path.head))
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    86
      val r = first_tour_heuristics(dim, x::path)
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    87
    //first(ordered_moves(dim, path, path.head), (x: Pos) => first_tour_heuristics(dim, x::path))
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    88
    ordered_moves(dim, path, path.head).view.flatMap((x: Pos) => first_tour_heuristics(dim, x::path)).headOption
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    89
}
66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    90
*/ 
50
9891c9fac37e updated
Christian Urban <urbanc@in.tum.de>
parents: 45
diff changeset
    91
66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    92
/*
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    93
for (dim <- 1 to 50) {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    94
  val t = first_tour_heuristics(dim, List((dim / 2, dim / 2)))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    95
  println(s"${dim} x ${dim}: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 50
diff changeset
    96
}
66
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    97
*/
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    98
3506b681c191 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
    99
print_board(50, first_tour_heuristics(50, (25,25)::Nil).get)