Attic/sudoku_test.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 26 Apr 2024 17:35:36 +0100
changeset 486 9c03b5e89a2a
parent 469 48de09728447
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
383
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Sudoku
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//========
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
// call parallel version with
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
//
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
// scala -cp scala-parallel-collections_2.13-0.2.0.jar sudoku_test.scala 
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
//
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
// or
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
//
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
// scalac -cp scala-parallel-collections_2.13-0.2.0.jar sudoku_test.scala
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
// java -cp .:scala-library-2.13.0.jar:scala-parallel-collections_2.13-0.2.0.jar Sudoku
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
object Sudoku extends App { 
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
import scala.collection.parallel.CollectionConverters._
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
type Pos = (Int, Int)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
val emptyValue = '.'
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
val maxValue = 9
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
val allValues = "123456789".toList
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
val indexes = (0 to 8).toList
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
def empty(game: String) = game.indexOf(emptyValue)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
def isDone(game: String) = empty(game) == -1 
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
def emptyPosition(game: String) = {
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
  val e = empty(game)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
  (e % maxValue, e / maxValue)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
}
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
def get_row(game: String, y: Int) = indexes.map(col => game(y * maxValue + col))
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
def get_col(game: String, x: Int) = indexes.map(row => game(x + row * maxValue))
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
def get_box(game: String, pos: Pos): List[Char] = {
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
    def base(p: Int): Int = (p / 3) * 3
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
    val x0 = base(pos._1)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
    val y0 = base(pos._2)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
    for (x <- (x0 until x0 + 3).toList;
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
         y <- (y0 until y0 + 3).toList) yield game(x + y * maxValue)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
}         
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
def update(game: String, pos: Int, value: Char): String = 
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
  game.updated(pos, value)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
def toAvoid(game: String, pos: Pos): List[Char] = 
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
  (get_col(game, pos._1) ++ get_row(game, pos._2) ++ get_box(game, pos))
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
def candidates(game: String, pos: Pos): List[Char] = 
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
  allValues.diff(toAvoid(game, pos))
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
def search(game: String): List[String] = {
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
  if (isDone(game)) List(game)
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
  else {
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
    val cs = candidates(game, emptyPosition(game))
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
    cs.par.map(c => search(update(game, empty(game), c))).toList.flatten
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
  }
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
}
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
def pretty(game: String): String = 
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
  "\n" + (game.grouped(maxValue).mkString(",\n"))
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
val game2 = """8........
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
              |..36.....
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
              |.7..9.2..
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
              |.5...7...
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
              |....457..
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
              |...1...3.
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
              |..1....68
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
              |..85...1.
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
              |.9....4..""".stripMargin.replaceAll("\\n", "")
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
// for measuring time
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
def time_needed[T](i: Int, code: => T) = {
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
  val start = System.nanoTime()
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
  for (j <- 1 to i) code
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
  val end = System.nanoTime()
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
  s"${(end - start) / 1.0e9} secs"
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
}
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
println(time_needed(10, search(game2)))
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
c02929f2647c updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
}