Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Last active August 29, 2015 14:04
Show Gist options
  • Save sungkmi/9a91ea44484539baaed0 to your computer and use it in GitHub Desktop.
Save sungkmi/9a91ea44484539baaed0 to your computer and use it in GitHub Desktop.
object CrossTheMaze extends App {
trait Direction {
def dx: Int
def dy: Int
}
case object North extends Direction {
val (dx, dy) = (0, -1)
}
case object East extends Direction {
val (dx, dy) = (1, 0)
}
case object South extends Direction {
val (dx, dy) = (0, 1)
}
case object West extends Direction {
val (dx, dy) = (-1, 0)
}
val Directions = Vector(North, East, South, West)
class Maze(mazeString: String) {
private val wall = {
val middles = for (line <- mazeString.lines.toVector) yield true +: (for (c <- line) yield c == '#') :+ true
val b = Vector.fill(middles.head.size)(true)
b +: middles :+ b
}
def step(x: Int, y: Int, face: Direction): (Int, Int, Direction) = {
val (left, right, back) = (North, South, West) //East
if (!isWall(x+left.dx, y+left.dy)) (x+left.dx, y+left.dy, left)
else if (!isWall(x+face.dx, y+face.dy))(x+face.dx, y+face.dy, face)
else if (!isWall(x+right.dx, y+right.dy))(x+right.dx, y+right.dy, right)
else (x+back.dx, y+back.dy, back)
}
def isWall(coord: (Int, Int)): Boolean = coord match { case (x, y) => wall(x)(y) }
}
def findPath(maze: Maze, sx: Int, sy: Int, ex: Int, ey: Int): Option[String] = None
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
val n = lineIn.next()
val ss = lineIn.next().split(' ').map(_.toInt)
// lineOut(s"Case #$i: ${sort(ss: _*) mkString " "}")
}
val writer = new java.io.PrintWriter("c.large.out")
try {
process(io.Source.fromFile("C-large-practice.in").getLines)(writer.println)
} finally {
writer.flush(); writer.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment