progs/cube.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 11 Mar 2023 22:01:53 +0000
changeset 463 0315d9983cd0
parent 417 29fc780ca130
child 464 73ced118f73d
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
417
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// 2 x 2 x 2 Rubik's Cube
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
//========================
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
     4
// for more memory for the JVM, call
417
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
//
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
// JAVA_OPTS="-Xmx2g" scala
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
// for measuring memory usage
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
val mb = 1024*1024
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
val runtime = Runtime.getRuntime
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
def memory() = {
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
  println(s"  ** Used Memory:  ${(runtime.totalMemory - runtime.freeMemory) / mb}")
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
  println(s"  ** Free Memory:  ${runtime.freeMemory / mb}")
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
  println(s"  ** Total Memory: ${runtime.totalMemory / mb}")
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
  println(s"  ** Max Memory:   ${runtime.maxMemory / mb}")
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
}
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
abstract class Colour
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
case object White extends Colour
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
case object Yellow extends Colour
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
case object Orange extends Colour
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
case object Red extends Colour
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
case object Green extends Colour
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
case object Blue extends Colour
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
// Faces
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
//    -------
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
//   |c11 c12|
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
//   |c21 c22|
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
//    -------  
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
case class Face(c11: Colour, c12: Colour,
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
                c21: Colour, c22: Colour)
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
// Cubes
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
//     +--+
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
//     |f2|
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
//  +--+  +--+
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
//  |f5 f1 f6|
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
//  +--+  +--+
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
//     |f3|
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
//     |f4|
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
//     +--+ 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
case class Cube(f1: Face, f2: Face, f3: Face,
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
                f4: Face, f5: Face, f6: Face)
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
// starting cube
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
val init = 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
    Cube(Face(White, Green, White, White), 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
         Face(Blue, Yellow, Orange, Red), 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
         Face(Red, Blue, Red, Blue),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
         Face(White, Yellow, Red, Orange),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
         Face(Yellow, Green, Blue, Green),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
         Face(Yellow, Green, Orange, Orange))
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
// solved cube
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
val solved = 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
    Cube(Face(Yellow, Yellow, Yellow, Yellow), 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
         Face(Orange, Orange, Orange, Orange), 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
         Face(Red, Red, Red, Red),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
         Face(White, White, White, White),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
         Face(Blue, Blue, Blue, Blue),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
         Face(Green, Green, Green, Green))  
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
// actions
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
def up(c: Cube) : Cube =
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
    Cube(Face(c.f1.c11, c.f3.c12, c.f1.c21, c.f3.c22),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
         Face(c.f2.c11, c.f1.c12, c.f2.c21, c.f1.c22),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
         Face(c.f3.c11, c.f4.c12, c.f3.c21, c.f4.c22),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
         Face(c.f4.c11, c.f2.c12, c.f4.c21, c.f2.c22),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
         Face(c.f5.c11, c.f5.c12, c.f5.c21, c.f5.c22),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
         Face(c.f6.c21, c.f6.c11, c.f6.c22, c.f6.c12))
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
def clock(c: Cube) : Cube =
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
    Cube(Face(c.f1.c21, c.f1.c11, c.f1.c22, c.f1.c12),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
         Face(c.f2.c11, c.f2.c12, c.f5.c22, c.f5.c12),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
         Face(c.f6.c21, c.f6.c11, c.f3.c21, c.f3.c22),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
         Face(c.f4.c11, c.f4.c12, c.f4.c21, c.f4.c22),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
         Face(c.f5.c11, c.f3.c11, c.f5.c21, c.f3.c12),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
         Face(c.f2.c21, c.f6.c12, c.f2.c22, c.f6.c22))
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
def right(c: Cube) : Cube =
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
    Cube(Face(c.f5.c11, c.f5.c12, c.f1.c21, c.f1.c22),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
         Face(c.f2.c12, c.f2.c22, c.f2.c11, c.f2.c21),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
         Face(c.f3.c11, c.f3.c12, c.f3.c21, c.f3.c22),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
         Face(c.f4.c11, c.f4.c12, c.f6.c12, c.f6.c11),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
         Face(c.f4.c22, c.f4.c21, c.f5.c21, c.f5.c22),
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
         Face(c.f1.c11, c.f1.c12, c.f6.c21, c.f6.c22))         
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
    97
// simple bf-search without generating a solution, just true
417
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
def actions(c: Cube) : List[Cube] = 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
  List(up(c), clock(c), right(c)) 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
def search(cs: List[Cube], d: Int) : Boolean = { 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
  println(s"Depth: $d    Cands: ${cs.length}")  
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
  //memory()
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
  if (cs.exists(solved == _)) true
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  else search(cs.flatMap(actions), d + 1)
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
}  
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
search(List(init), 0)
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   111
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   112
// set version
417
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
def actions_set(c: Cube) : Set[Cube] =
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
  Set(up(c), clock(c), right(c))
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
def search_set(cs: Set[Cube], d: Int) : Boolean = {
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
  println(s"Depth: $d candidates: ${cs.size}")
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
  //memory()
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
  if (cs.contains(solved)) true
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
  else search_set(cs.flatMap(actions_set), d + 1)
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
}  
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
search_set(Set(init), 0)
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   126
// for generating list of actions
417
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
abstract class Action
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
case object Up extends Action
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
case object Right extends Action
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
case object Clock extends Action
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
type Actions = List[Action]
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   134
// list version generating a list of actions
417
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
def actions_list(c: Cube, act: Actions) : List[(Cube, Actions)] = 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
  List((up(c), Up::act), 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
       (clock(c), Clock::act), 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
       (right(c), Right::act)) 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
def search_list(cs: List[(Cube, Actions)], d: Int) : (Cube, Actions) = { 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
  println(s"Depth: $d    Cands: ${cs.length}")  
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
  val res = cs.find(_._1 == solved)
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
  if (res.isDefined) res.get
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
  else search_list(cs.flatMap{case (c, as) => actions_list(c, as) }, d + 1)
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
}  
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
search_list(List((init, Nil)), 0)._2.reverse.mkString("\n") 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   151
// map version generating a list of actions
417
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
def actions_map(c: Cube, as: Actions) : Map[Cube, Actions] = 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
  Map(up(c) -> (Up::as), 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
      clock(c) -> (Clock::as), 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
      right(c) -> (Right::as)) 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
def search_map(cs: Map[Cube, Actions], d: Int) : Actions = { 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
  println(s"Depth: $d    Cands: ${cs.keySet.size}")  
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
  val res = cs.keySet.find(_ == solved)
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
  if (res.isDefined) cs(res.get)
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
  else search_map(cs.flatMap{case (c, as) => actions_list(c, as) }, d + 1)
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
}  
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
search_map(Map(init -> Nil), 0).reverse.mkString("\n")
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   170
// bidirectional breadth-first search
417
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
def bsearch(cs: Map[Cube, Actions], 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
            bs: Map[Cube, Actions], d: Int) : Actions = { 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
  println(s"Depth: $d    Cands: ${cs.keySet.size}/${bs.keySet.size}") 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
  val res = cs.keySet intersect bs.keySet 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
  if (!res.isEmpty) (cs(res.head).reverse ::: bs(res.head))
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
  else bsearch(cs.flatMap{case (c, as) => actions_map(c, as) }, 
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
               bs.flatMap{case (c, as) => actions_map(c, as) }, d + 1)
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
}  
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
bsearch(Map(init ->  Nil), Map(solved -> Nil), 0).mkString("\n")
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   184
// bidirectional breadth-first search
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   185
def bsearch(cs: Map[Cube, Actions], 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   186
            bs: Map[Cube, Actions], d: Int) : Actions = { 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   187
  println(s"Depth: $d    Cands: ${cs.keySet.size}/${bs.keySet.size}") 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   188
  val res = cs.keySet intersect bs.keySet 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   189
  if (!res.isEmpty) (cs(res.head).reverse ::: bs(res.head))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   190
  else bsearch(cs.flatMap{ (c, as) => actions_map(c, as) }, 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   191
               bs.flatMap{ (c, as) => actions_map(c, as) }, d + 1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   192
}  
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   193
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   194
bsearch(Map(init ->  Nil), Map(solved -> Nil), 0).mkString("\n")
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   195
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   196
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 417
diff changeset
   197
417
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
29fc780ca130 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214