Skip to content

Instantly share code, notes, and snippets.

@UberMouse
Created June 5, 2013 04:28
Show Gist options
  • Save UberMouse/5711602 to your computer and use it in GitHub Desktop.
Save UberMouse/5711602 to your computer and use it in GitHub Desktop.
Finished version of CodeEval Query Board problem
object Main {
def main(args:Array[String]) {
//val source = scala.io.Source.fromFile(args(0))
val matrix = List.fill[Int](256, 256)(0)
Array("SetCol 32 20",
"SetRow 15 7",
"SetRow 16 31",
"QueryCol 32",
"SetCol 2 14",
"QueryRow 10",
"SetCol 14 0",
"QueryRow 15",
"SetRow 10 1",
"QueryCol 2")
.filter(_.length > 0)
.map(process)
.foldLeft(matrix)((matrix, func) => func(matrix))
}
def process(line:String) = {
val cmdInfo = line split " "
cmdInfo(0) match {
case "SetCol" => setCol(cmdInfo(1), cmdInfo(2)) _
case "SetRow" => setRow(cmdInfo(1), cmdInfo(2)) _
case "QueryCol" => queryCol(cmdInfo(1)) _
case "QueryRow" => queryRow(cmdInfo(1)) _
}
}
def setCol(col:String, data:String)(grid:List[List[Int]]) = grid.updated(col.toInt, grid(col.toInt).map(x => data.toInt))
def setRow(row:String, data:String)(grid:List[List[Int]]) = grid.map(_.updated(row.toInt, data.toInt))
def queryCol(col:String)(grid:List[List[Int]]) = {
println(grid(col.toInt).sum)
grid
}
def queryRow(row:String)(grid:List[List[Int]]) = {
println(grid.foldLeft(0)((x, y) => x + y(row.toInt)))
grid
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment