--- a/progs/cube.scala Sun Jan 15 10:58:13 2023 +0000
+++ b/progs/cube.scala Sat Mar 11 22:01:53 2023 +0000
@@ -1,7 +1,7 @@
// 2 x 2 x 2 Rubik's Cube
//========================
-// more memory for the JVM
+// for more memory for the JVM, call
//
// JAVA_OPTS="-Xmx2g" scala
@@ -94,7 +94,7 @@
Face(c.f1.c11, c.f1.c12, c.f6.c21, c.f6.c22))
-// simple bf-search without solution, just true
+// simple bf-search without generating a solution, just true
def actions(c: Cube) : List[Cube] =
List(up(c), clock(c), right(c))
@@ -108,7 +108,8 @@
search(List(init), 0)
-// Set version
+
+// set version
def actions_set(c: Cube) : Set[Cube] =
Set(up(c), clock(c), right(c))
@@ -122,7 +123,7 @@
search_set(Set(init), 0)
-// For generating list of actions
+// for generating list of actions
abstract class Action
case object Up extends Action
case object Right extends Action
@@ -130,7 +131,7 @@
type Actions = List[Action]
-// List version generating a list of actions
+// list version generating a list of actions
def actions_list(c: Cube, act: Actions) : List[(Cube, Actions)] =
List((up(c), Up::act),
(clock(c), Clock::act),
@@ -147,7 +148,7 @@
search_list(List((init, Nil)), 0)._2.reverse.mkString("\n")
-// Map version generating a list of actions
+// map version generating a list of actions
def actions_map(c: Cube, as: Actions) : Map[Cube, Actions] =
Map(up(c) -> (Up::as),
clock(c) -> (Clock::as),
@@ -166,7 +167,7 @@
-// Bidirectional breadth-first search
+// bidirectional breadth-first search
def bsearch(cs: Map[Cube, Actions],
bs: Map[Cube, Actions], d: Int) : Actions = {
println(s"Depth: $d Cands: ${cs.keySet.size}/${bs.keySet.size}")
@@ -180,6 +181,20 @@
+// bidirectional breadth-first search
+def bsearch(cs: Map[Cube, Actions],
+ bs: Map[Cube, Actions], d: Int) : Actions = {
+ println(s"Depth: $d Cands: ${cs.keySet.size}/${bs.keySet.size}")
+ val res = cs.keySet intersect bs.keySet
+ if (!res.isEmpty) (cs(res.head).reverse ::: bs(res.head))
+ else bsearch(cs.flatMap{ (c, as) => actions_map(c, as) },
+ bs.flatMap{ (c, as) => actions_map(c, as) }, d + 1)
+}
+
+bsearch(Map(init -> Nil), Map(solved -> Nil), 0).mkString("\n")
+
+
+