progs/sudoku.scala
changeset 465 8ce150207792
parent 450 61eb4f9b8d84
child 467 9b5165b8a762
--- a/progs/sudoku.scala	Sat Mar 11 22:42:09 2023 +0000
+++ b/progs/sudoku.scala	Sat Mar 11 23:09:51 2023 +0000
@@ -3,11 +3,11 @@
 
 // call parallel version with
 //
-// scala -cp scala-parallel-collections_2.13-0.2.0.jar sudoku.scala 
+// scala -cp scala-parallel-collections_3-1.0.4.jar sudoku.scala
 //
-// java -cp .:scala-library-2.13.0.jar:scala-parallel-collections_2.13-0.2.0.jar Sudoku
+// java -cp .:scala3-library_3-3.2.2.jar:scala-parallel-collections_3-1.0.4.jar Sudoku
 
-object Sudoku extends App { 
+
 
 import scala.collection.parallel.CollectionConverters._
 
@@ -52,16 +52,16 @@
 def candidates(game: String, pos: Pos): List[Char] = 
   allValues.diff(toAvoid(game, pos))
 
-
+// search for all solutions
 def search(game: String): List[String] = {
   if (isDone(game)) List(game)
   else 
-    candidates(game, emptyPosition(game)).par.
+    candidates(game, emptyPosition(game)).
       map(c => search(update(game, empty(game), c))).toList.flatten
 }
 
 
-
+// search for single solution
 def search1T(games: List[String]): Option[String] = games match {
   case Nil => None
   case game::rest => {
@@ -203,20 +203,23 @@
 }
 
 
-val total = 
-  (for ((game, i) <- hard_games.zipWithIndex) yield {
-    val secs = time_needed(1, search(game))
-    println(f"${i}%2.0f: ${game} |${secs}%2.3f secs")
-    secs
-  }).sum
+@main 
+def test() = {
+  val total = 
+    (for ((game, i) <- hard_games.zipWithIndex) yield {
+      val secs = time_needed(1, search(game))
+      println(f"${i}%2.0f: ${game} |${secs}%2.3f secs")
+      secs
+    }).sum
 
-println(f"\ntotal: ${total}%.3f secs")
-
+  println(f"\ntotal: ${total}%.3f secs")
 }
 
 
 
-// 1 single thread version 800 secs
+// some numbers:
+//
+// single thread version 800 secs
 //
 // 4 cores parallel version on a moderate laptop 400 secs
 // 8 cores: 290 secs