Permalink
Browse files

Minor cleanup

  • Loading branch information...
1 parent 963e885 commit 670c69819ba560fe11125550cfd5e7bcc2fe4ace @ebowman committed Apr 5, 2012
Showing with 10 additions and 13 deletions.
  1. +7 −10 src/main/scala/fast.scala
  2. +3 −3 src/main/scala/tiny.scala
View
@@ -22,11 +22,10 @@ object Fast extends App {
}
val queue = new LinkedBlockingQueue[String]
-
- val bits = Vector().par ++ io.Source.fromFile(args(0)).getLines().map(_.split("\t"))
- bits.map(pair => solveAnyShortestBFS(pair(0), pair(1))).seq.foreach {
+ val pairs = Vector().par ++ io.Source.fromFile(args(0)).getLines().map(_.split("\t"))
+ pairs.map(pair => solveAnyShortestBFS(pair(0), pair(1))).seq.foreach {
case (result: Seq[_]) =>
- queue.put(result.take(1) + "\t" + result.drop(1).take(1))
+ queue.put(result.mkString(" -> "))
}
queue.put("done")
val executor = Executors.newSingleThreadExecutor
@@ -54,11 +53,8 @@ object Fast extends App {
executor.shutdown()
def solveAnyShortestBFS(start: String, target: String): Seq[String] = {
- val visited = new java.util.HashSet[String]() {
- {
- add(start);
- }
- }
+ val visited = new java.util.HashSet[String]()
+ visited.add(start)
@tailrec
def recurse(paths: Seq[Seq[String]]): Seq[String] = {
val nextPaths = for {
@@ -82,5 +78,6 @@ object Fast extends App {
def diff(a: String, b: String): Int = (0 until a.length).map(i => if (a(i) == b(i)) 0 else 1).sum
- def pieces(word: String) = Seq("_" + word.drop(1), word.take(1) + "_" + word.drop(2), word.take(2) + "_" + word.drop(3), word.take(3) + "_")
+ def pieces(word: String) =
+ Seq("_" + word.drop(1), word.take(1) + "_" + word.drop(2), word.take(2) + "_" + word.drop(3), word.take(3) + "_")
}
@@ -2,13 +2,13 @@ object Tiny extends App {
val words = io.Source.fromFile("/usr/share/dict/words").getLines().filter(_.length == 4).map(_.toLowerCase).toSet
val lookup = (for (word <- words; other <- words if diff(word, other) == 1) yield (word, other)).groupBy(_._1).map(a => a._1 -> a._2.map(_._2))
val visited = collection.mutable.Set[String](args(0))
- println(recurse((args(0) :: Nil) :: Nil).reverse)
+ println(search(List(List(args(0)))).reverse)
- def recurse(paths: Seq[Seq[String]]): Seq[String] = {
+ def search(paths: Seq[Seq[String]]): Seq[String] = {
val nextPaths = for (path <- paths; next <- lookup(path.head) if !visited.contains(next)) yield {
visited += next; next +: path
}
- nextPaths.find(_.head == args(1)).getOrElse(if (nextPaths.isEmpty) Nil else recurse(nextPaths))
+ nextPaths.find(_.head == args(1)).getOrElse(if (nextPaths.isEmpty) Nil else search(nextPaths))
}
def diff(a: String, b: String): Int = (0 until a.length).map(i => if (a(i) == b(i)) 0 else 1).sum

0 comments on commit 670c698

Please sign in to comment.