Scala! I'm slowly falling in love with this language.
object Wires { case class Point(x: Int, y: Int) def closestInDistance (a: Seq[String], b: Seq[String]): Int = { val pathA = makePath(a, Point(0, 0)) val pathB = makePath(b, Point(0, 0)) val distances = pathA.intersect(pathB).tail map { case Point(x, y) => math.abs(x) + math.abs(y) } distances.min } def closestInSteps (a: Seq[String], b: Seq[String]): Int = { val pathA = makePath(a, Point(0, 0)) val pathB = makePath(b, Point(0, 0)) val steps = pathA.intersect(pathB).tail map { point => pathA.indexOf(point) + pathB.indexOf(point) } steps.min } private def makePath (xs: Seq[String], start: Point): Seq[Point] = { val vectors = for { x <- xs (dir :: rest) = x.toList length = rest.mkString.toInt i <- (1 to length) } yield dir match { case 'R' => Point(1, 0) case 'L' => Point(-1, 0) case 'U' => Point(0, 1) case 'D' => Point(0, -1) } vectors.foldLeft(Vector(start)) { case (result, Point(x2, y2)) => { val Point(x, y) = result.last val next = Point(x + x2, y + y2) result :+ next } } } }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Scala! I'm slowly falling in love with this language.