progs/k1_sol.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 16 Nov 2016 14:37:18 +0000
changeset 50 9891c9fac37e
parent 45 8399976b77fe
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// Part 1 about finding anod counting Knight's tours
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
//===================================================
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
type Pos = (Int, Int)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
type Path = List[Pos]
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
def print_board(dim: Int, path: Path): Unit = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
  println
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
  for (i <- 0 until dim) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
    for (j <- 0 until dim) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
      print(f"${path.reverse.indexOf((i, j))}%3.0f ")
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
    }
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
    println
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
  } 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
def add_pair(x: Pos)(y: Pos): Pos = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
  (x._1 + y._1, x._2 + y._2)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
def dist(dim: Int, y: Pos) = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
  (dim / 2 - y._1).abs + (dim / 2 - y._2).abs
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
def is_legal(dim: Int, path: Path)(x: Pos): Boolean = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
  0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
def moves(x: Pos): List[Pos] = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
  List(( 1,  2),( 2,  1),( 2, -1),( 1, -2),
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
       (-1, -2),(-2, -1),(-2,  1),(-1,  2)).map(add_pair(x))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
  moves(x).filter(is_legal(dim, path))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  legal_moves(dim, path, x).sortBy((x) => (legal_moves(dim, path, x).length, dist(dim, x)))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
//moves(8)(1,3)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
//ordered_moves(8)(Nil)(1,3)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
//ordered_moves(8)(List((2, 4), (2, 6)))(1,3)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
def count_tours(dim: Int, path: Path): Int = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
  if (path.length == dim * dim) 1
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
  else 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
    (for (x <- legal_moves(dim, path, path.head)) yield count_tours(dim, x::path)).sum
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
def enum_tours(dim: Int, path: Path): List[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  if (path.length == dim * dim) List(path)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  else 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
    (for (x <- legal_moves(dim, path, path.head)) yield enum_tours(dim, x::path)).flatten
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
def count_all_tours(dim: Int): Int = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
  (for (i <- (0 until dim).toList; 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
        j <- (0 until dim).toList) yield count_tours(dim, List((i, j)))).sum
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
def enum_all_tours(dim: Int): List[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  (for (i <- (0 until dim).toList; 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
        j <- (0 until dim).toList) yield enum_tours(dim, List((i, j)))).flatten
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
/*
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
for (dim <- 1 to 5) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  println(s"${dim} x ${dim} " + count_all_tours(dim))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
for (dim <- 1 to 5) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
  val ts = enum_all_tours(dim)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
  println(s"${dim} x ${dim} " + (if (ts == Nil) "" else { print_board(dim, ts.head) ; "" }))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
*/
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  case Nil => None
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  case x::xs => {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
    val result = f(x)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
    if (result.isDefined) result else first(xs, f)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
  }
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
def first_tour(dim: Int, path: Path): Option[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
  if (path.length == dim * dim) Some(path)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  else
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
    first(legal_moves(dim, path, path.head), (x: Pos) => first_tour(dim, x::path))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
for (dim <- 1 to 8) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  val t = first_tour(dim, List((0, 0)))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  println(s"${dim} x ${dim} " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
/*
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
def first2[A, B](xs: List[A], f: A => Option[B]): Option[B] =
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
  xs.par.flatMap(f(_)).headOption
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
*/
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
def first_closed_tour_heuristics(dim: Int, path: Path): Option[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
  if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
  else
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
    first(ordered_moves(dim, path, path.head), (x: Pos) => first_closed_tour_heuristics(dim, x::path))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
 
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
for (dim <- 1 to 6) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
  val t = first_closed_tour_heuristics(dim, List((dim / 2, dim / 2)))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
  println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   120
def first_tour_heuristics(dim: Int, path: Path): Option[Path] = {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
  if (path.length == dim * dim) Some(path)
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
  else
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
    first(ordered_moves(dim, path, path.head), (x: Pos) => first_tour_heuristics(dim, x::path))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
/*
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
for (dim <- 1 to 50) {
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
  val t = first_tour_heuristics(dim, List((dim / 2, dim / 2)))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
  println(s"${dim} x ${dim}: " + (if (t == None) "" else { print_board(dim, t.get) ; "" }))
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
}
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
*/
8399976b77fe updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132